Skip to content

Commit 4777750

Browse files
authored
Divide custom exception into specific ones (#261)
1 parent 5a52e94 commit 4777750

18 files changed

Lines changed: 1725 additions & 1693 deletions

build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ jacocoTestReport {
4848
afterEvaluate {
4949
classDirectories = files(classDirectories.files.collect {
5050
fileTree(dir: it, exclude: ['**/MediaConstraints.class',
51+
'**/stickerify/exception/**',
5152
'**/stickerify/process/**',
5253
'**/stickerify/runner**',
5354
'**/stickerify/telegram/**'])

qodana.baseline.json

Lines changed: 1539 additions & 1596 deletions
Large diffs are not rendered by default.

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

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@
99
import static java.util.HashSet.newHashSet;
1010
import static java.util.concurrent.Executors.newFixedThreadPool;
1111

12+
import com.github.stickerifier.stickerify.exception.BaseException;
13+
import com.github.stickerifier.stickerify.exception.CorruptedVideoException;
14+
import com.github.stickerifier.stickerify.exception.FileOperationException;
15+
import com.github.stickerifier.stickerify.exception.MediaException;
16+
import com.github.stickerifier.stickerify.exception.TelegramApiException;
1217
import com.github.stickerifier.stickerify.media.MediaHelper;
1318
import com.github.stickerifier.stickerify.telegram.Answer;
14-
import com.github.stickerifier.stickerify.telegram.exception.TelegramApiException;
1519
import com.github.stickerifier.stickerify.telegram.model.TelegramFile;
1620
import com.github.stickerifier.stickerify.telegram.model.TelegramRequest;
1721
import com.pengrad.telegrambot.ExceptionHandler;
1822
import com.pengrad.telegrambot.TelegramBot;
1923
import com.pengrad.telegrambot.UpdatesListener;
24+
import com.pengrad.telegrambot.model.LinkPreviewOptions;
2025
import com.pengrad.telegrambot.model.Update;
2126
import com.pengrad.telegrambot.request.BaseRequest;
2227
import com.pengrad.telegrambot.request.GetFile;
@@ -117,6 +122,7 @@ private void answerFile(TelegramRequest request, String fileId) {
117122

118123
LOGGER.atTrace().log("Converting file {}", fileId);
119124
var outputFile = MediaHelper.convert(originalFile);
125+
LOGGER.atTrace().log("File converted successfully");
120126

121127
if (outputFile == null) {
122128
answerText(FILE_ALREADY_VALID, request);
@@ -130,14 +136,14 @@ private void answerFile(TelegramRequest request, String fileId) {
130136

131137
execute(answerWithFile);
132138
}
133-
} catch (TelegramApiException e) {
139+
} catch (TelegramApiException | MediaException e) {
134140
processFailure(request, e);
135141
} finally {
136142
deleteTempFiles(pathsToDelete);
137143
}
138144
}
139145

140-
private File retrieveFile(String fileId) throws TelegramApiException {
146+
private File retrieveFile(String fileId) throws TelegramApiException, FileOperationException {
141147
var file = execute(new GetFile(fileId)).file();
142148

143149
try {
@@ -147,14 +153,16 @@ private File retrieveFile(String fileId) throws TelegramApiException {
147153

148154
return downloadedFile;
149155
} catch (IOException e) {
150-
throw new TelegramApiException(e);
156+
throw new FileOperationException(e);
151157
}
152158
}
153159

154-
private void processFailure(TelegramRequest request, TelegramApiException e) {
155-
if (e.getMessage().endsWith("Bad Request: message to be replied not found")) {
156-
LOGGER.atInfo().log("Unable to reply to the {}: the message sent has been deleted", request.getDescription());
157-
} else if ("The video could not be processed successfully".equals(e.getMessage())) {
160+
private void processFailure(TelegramRequest request, BaseException e) {
161+
if (e instanceof TelegramApiException telegramException) {
162+
processTelegramFailure(request.getDescription(), telegramException, false);
163+
}
164+
165+
if (e instanceof CorruptedVideoException) {
158166
LOGGER.atInfo().log("Unable to reply to the {}: the file is corrupted", request.getDescription());
159167
answerText(CORRUPTED, request);
160168
} else {
@@ -163,6 +171,18 @@ private void processFailure(TelegramRequest request, TelegramApiException e) {
163171
}
164172
}
165173

174+
private void processTelegramFailure(String requestDescription, TelegramApiException e, boolean logUnmatchedFailure) {
175+
var exceptionMessage = e.getMessage();
176+
177+
if (exceptionMessage.endsWith("Bad Request: message to be replied not found")) {
178+
LOGGER.atInfo().log("Unable to reply to the {}: the message sent has been deleted", requestDescription);
179+
} else if (exceptionMessage.endsWith("Forbidden: bot was blocked by the user")) {
180+
LOGGER.atInfo().log("Unable to reply to the {}: the user blocked the bot", requestDescription);
181+
} else if (logUnmatchedFailure) {
182+
LOGGER.atError().setCause(e).log("Unable to reply to the {}", requestDescription);
183+
}
184+
}
185+
166186
private void answerText(TelegramRequest request) {
167187
var message = request.message();
168188
if (message.text() == null) {
@@ -173,19 +193,23 @@ private void answerText(TelegramRequest request) {
173193
}
174194

175195
private void answerText(Answer answer, TelegramRequest request) {
196+
var previewOptions = new LinkPreviewOptions().isDisabled(answer.isDisableLinkPreview());
197+
176198
var answerWithText = new SendMessage(request.getChatId(), answer.getText())
177199
.replyToMessageId(request.getMessageId())
178200
.parseMode(MarkdownV2)
179-
.disableWebPagePreview(answer.isDisableWebPreview());
201+
.linkPreviewOptions(previewOptions);
180202

181203
try {
182204
execute(answerWithText);
183205
} catch (TelegramApiException e) {
184-
LOGGER.atError().setCause(e).log("Unable to reply to the {}", request);
206+
processTelegramFailure(request.getDescription(), e, true);
185207
}
186208
}
187209

188210
private <T extends BaseRequest<T, R>, R extends BaseResponse> R execute(BaseRequest<T, R> request) throws TelegramApiException {
211+
LOGGER.atTrace().log("Sending {} request", request.getMethod());
212+
189213
var response = bot.execute(request);
190214

191215
if (response.isOk()) {
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.github.stickerifier.stickerify.exception;
2+
3+
import org.slf4j.helpers.MessageFormatter;
4+
5+
public class BaseException extends Exception {
6+
public BaseException(Throwable cause) {
7+
super(cause);
8+
}
9+
10+
/**
11+
* Creates an exception with a parameterized message: each {@code {}}
12+
* will be replaced with the corresponding element in {@code parameters}.
13+
*
14+
* @param message the exception message
15+
* @param parameters the parameters to insert into the message
16+
* @see MessageFormatter#basicArrayFormat(String, Object[])
17+
*/
18+
public BaseException(String message, Object... parameters) {
19+
super(MessageFormatter.basicArrayFormat(message, parameters));
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.stickerifier.stickerify.exception;
2+
3+
public class CorruptedVideoException extends MediaException {
4+
public CorruptedVideoException(String message, Throwable cause) {
5+
super(message, cause);
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.github.stickerifier.stickerify.exception;
2+
3+
public class FileOperationException extends MediaException {
4+
public FileOperationException(Throwable cause) {
5+
super(cause);
6+
}
7+
8+
public FileOperationException(String message, Throwable cause) {
9+
super(message, cause);
10+
}
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.github.stickerifier.stickerify.exception;
2+
3+
public class MediaException extends BaseException {
4+
public MediaException(String message) {
5+
super(message);
6+
}
7+
8+
public MediaException(Throwable cause) {
9+
super(cause);
10+
}
11+
12+
public MediaException(String message, Throwable cause) {
13+
super(message, cause);
14+
}
15+
16+
/**
17+
* @see BaseException#BaseException(String, Object...)
18+
*/
19+
public MediaException(String message, Object... parameters) {
20+
super(message, parameters);
21+
}
22+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.stickerifier.stickerify.exception;
2+
3+
public class MediaOptimizationException extends MediaException {
4+
public MediaOptimizationException(String message) {
5+
super(message);
6+
}
7+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.stickerifier.stickerify.exception;
2+
3+
public class ProcessException extends BaseException {
4+
public ProcessException(Throwable cause) {
5+
super(cause);
6+
}
7+
8+
/**
9+
* @see BaseException#BaseException(String, Object...)
10+
*/
11+
public ProcessException(String message, Object... parameters) {
12+
super(message, parameters);
13+
}
14+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.github.stickerifier.stickerify.exception;
2+
3+
public class TelegramApiException extends BaseException {
4+
/**
5+
* @see BaseException#BaseException(String, Object...)
6+
*/
7+
public TelegramApiException(String message, Object... parameters) {
8+
super(message, parameters);
9+
}
10+
}

0 commit comments

Comments
 (0)