Skip to content

Commit 534561d

Browse files
Properly use @NullMarked annotation, fix some possible NPE surfaces
1 parent b5d03ad commit 534561d

File tree

16 files changed

+88
-34
lines changed

16 files changed

+88
-34
lines changed

src/main/java/com/github/stickerifier/stickerify/bot/Stickerify.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ private void answerFile(TelegramRequest request, String fileId) {
136136
execute(answerWithFile);
137137
}
138138
} catch (TelegramApiException | MediaException e) {
139-
processFailure(request, e);
139+
processFailure(request, e, fileId);
140140
} catch (InterruptedException e) {
141141
Thread.currentThread().interrupt();
142142
} finally {
@@ -158,7 +158,7 @@ private File retrieveFile(String fileId) throws TelegramApiException, FileOperat
158158
}
159159
}
160160

161-
private void processFailure(TelegramRequest request, BaseException e) {
161+
private void processFailure(TelegramRequest request, BaseException e, String fileId) {
162162
if (e instanceof TelegramApiException telegramException) {
163163
processTelegramFailure(request.getDescription(), telegramException, false);
164164
}
@@ -167,20 +167,20 @@ private void processFailure(TelegramRequest request, BaseException e) {
167167
LOGGER.atInfo().log("Unable to reply to the {}: the file is corrupted", request.getDescription());
168168
answerText(CORRUPTED, request);
169169
} else {
170-
LOGGER.atWarn().setCause(e).log("Unable to process the file {}", request.getFile().id());
170+
LOGGER.atWarn().setCause(e).log("Unable to process the file {}", fileId);
171171
answerText(ERROR, request);
172172
}
173173
}
174174

175175
private void processTelegramFailure(String requestDescription, TelegramApiException e, boolean logUnmatchedFailure) {
176-
var exceptionMessage = e.getMessage();
177-
178-
if (exceptionMessage.endsWith("Bad Request: message to be replied not found")) {
179-
LOGGER.atInfo().log("Unable to reply to the {}: the message sent has been deleted", requestDescription);
180-
} else if (exceptionMessage.endsWith("Forbidden: bot was blocked by the user")) {
181-
LOGGER.atInfo().log("Unable to reply to the {}: the user blocked the bot", requestDescription);
182-
} else if (logUnmatchedFailure) {
183-
LOGGER.atError().setCause(e).log("Unable to reply to the {}", requestDescription);
176+
switch (e.getDescription()) {
177+
case "Bad Request: message to be replied not found" -> LOGGER.atInfo().log("Unable to reply to the {}: the message sent has been deleted", requestDescription);
178+
case "Forbidden: bot was blocked by the user" -> LOGGER.atInfo().log("Unable to reply to the {}: the user blocked the bot", requestDescription);
179+
default -> {
180+
if (logUnmatchedFailure) {
181+
LOGGER.atError().setCause(e).log("Unable to reply to the {}", requestDescription);
182+
}
183+
}
184184
}
185185
}
186186

@@ -215,7 +215,7 @@ private <T extends BaseRequest<T, R>, R extends BaseResponse> R execute(BaseRequ
215215
return response;
216216
}
217217

218-
throw new TelegramApiException("Telegram couldn't execute the {} request: {}", request.getMethod(), response.description());
218+
throw new TelegramApiException(request.getMethod(), response.description());
219219
}
220220

221221
private static void deleteTempFiles(Set<Path> pathsToDelete) {

src/main/java/com/github/stickerifier/stickerify/package-info.java renamed to src/main/java/com/github/stickerifier/stickerify/bot/package-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@NullMarked
2-
package com.github.stickerifier.stickerify;
2+
package com.github.stickerifier.stickerify.bot;
33

44
import org.jspecify.annotations.NullMarked;
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
package com.github.stickerifier.stickerify.exception;
22

33
public class TelegramApiException extends BaseException {
4+
private final String description;
5+
6+
public TelegramApiException(String requestMethod, String description) {
7+
super("Telegram couldn't execute the {} request: {}", requestMethod, description);
8+
this.description = description;
9+
}
10+
411
/**
5-
* @see BaseException#BaseException(String, Object...)
12+
* @return the description of the error received by the api call
613
*/
7-
public TelegramApiException(String message, Object... parameters) {
8-
super(message, parameters);
14+
public String getDescription() {
15+
return description;
916
}
1017
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package com.github.stickerifier.stickerify.exception;
3+
4+
import org.jspecify.annotations.NullMarked;

src/main/java/com/github/stickerifier/stickerify/logger/HighlightHelper.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.stickerifier.stickerify.logger;
22

3+
import org.jspecify.annotations.Nullable;
4+
35
import static ch.qos.logback.core.pattern.color.ANSIConstants.BOLD;
46
import static ch.qos.logback.core.pattern.color.ANSIConstants.ESC_END;
57
import static ch.qos.logback.core.pattern.color.ANSIConstants.ESC_START;
@@ -28,7 +30,7 @@ static String greenHighlight(final String message, String previousColor) {
2830
return START_GREEN + message + previousColor;
2931
}
3032

31-
static String retrieveMimeType(final String message) {
33+
static @Nullable String retrieveMimeType(final String message) {
3234
var matcher = MIME_TYPE_PATTERN.matcher(message);
3335

3436
return matcher.find() ? matcher.group(1) : null;

src/main/java/com/github/stickerifier/stickerify/logger/MessageHighlighter.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import ch.qos.logback.classic.pattern.MessageConverter;
1414
import ch.qos.logback.classic.spi.ILoggingEvent;
1515
import ch.qos.logback.core.pattern.Converter;
16+
import org.jspecify.annotations.Nullable;
1617

1718
/**
1819
* Custom converter class to be used by Logback to highlight important substrings.
@@ -26,7 +27,7 @@ public class MessageHighlighter extends MessageConverter {
2627
static final String HIGHLIGHTED_NEW_USER = " " + START_YELLOW + NEW_USER.substring(1) + CONTINUE_WHITE;
2728

2829
@Override
29-
public String convert(ILoggingEvent event) {
30+
public @Nullable String convert(ILoggingEvent event) {
3031
var message = event.getFormattedMessage();
3132

3233
if (message != null) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package com.github.stickerifier.stickerify.logger;
3+
4+
import org.jspecify.annotations.NullMarked;

src/main/java/com/github/stickerifier/stickerify/media/MediaHelper.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import com.google.gson.JsonSyntaxException;
2424
import com.google.gson.annotations.SerializedName;
2525
import org.apache.tika.Tika;
26-
import org.jspecify.annotations.NonNull;
2726
import org.jspecify.annotations.Nullable;
2827
import org.slf4j.Logger;
2928
import org.slf4j.LoggerFactory;
@@ -201,7 +200,6 @@ private float duration() {
201200
return (end - start) / frameRate;
202201
}
203202

204-
@NonNull
205203
@Override
206204
public String toString() {
207205
return "animated sticker [" +
@@ -220,7 +218,7 @@ public String toString() {
220218
* @param animation the animation to check
221219
* @return {@code true} if the animation is compliant
222220
*/
223-
private static boolean isAnimationCompliant(AnimationDetails animation) {
221+
private static boolean isAnimationCompliant(@Nullable AnimationDetails animation) {
224222
return animation != null
225223
&& animation.frameRate() <= MAX_ANIMATION_FRAME_RATE
226224
&& animation.duration() <= MAX_ANIMATION_DURATION_SECONDS
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NullMarked
2+
package com.github.stickerifier.stickerify.media;
3+
4+
import org.jspecify.annotations.NullMarked;

src/main/java/com/github/stickerifier/stickerify/process/PathLocator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import ws.schild.jave.process.ProcessWrapper;
77
import ws.schild.jave.process.ffmpeg.FFMPEGProcess;
88

9+
import java.util.Objects;
10+
911
/**
1012
* Custom locator class to be used by Jave to find the path where FFmpeg is installed at in the system.
1113
*
@@ -14,10 +16,11 @@
1416
public enum PathLocator implements ProcessLocator {
1517
INSTANCE;
1618

17-
private String ffmpegLocation = System.getenv("FFMPEG_PATH");
19+
private final String ffmpegLocation;
1820

1921
PathLocator() {
2022
var logger = LoggerFactory.getLogger(PathLocator.class);
23+
var ffmpegLocation = System.getenv("FFMPEG_PATH");
2124
try {
2225
if (ffmpegLocation == null || ffmpegLocation.isBlank()) {
2326
ffmpegLocation = ProcessHelper.executeCommand(OsConstants.FIND_EXECUTABLE, "ffmpeg").getFirst();
@@ -29,6 +32,7 @@ public enum PathLocator implements ProcessLocator {
2932
} catch (InterruptedException _) {
3033
Thread.currentThread().interrupt();
3134
}
35+
this.ffmpegLocation = Objects.requireNonNull(ffmpegLocation);
3236
}
3337

3438
@Override
@@ -38,6 +42,6 @@ public String getExecutablePath() {
3842

3943
@Override
4044
public ProcessWrapper createExecutor() {
41-
return new FFMPEGProcess(getExecutablePath());
45+
return new FFMPEGProcess(ffmpegLocation);
4246
}
4347
}

0 commit comments

Comments
 (0)