Skip to content

Commit bb154f4

Browse files
committed
ensure webhook errors are properly logged
1 parent b6981ab commit bb154f4

File tree

5 files changed

+30
-6
lines changed

5 files changed

+30
-6
lines changed

checkstyle/checkstyle.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,5 +96,6 @@ See https://checkstyle.org/ for more information.
9696
<module name="UnusedImports">
9797
<property name="processJavadoc" value="false"/>
9898
</module>
99+
<module name="SuppressionCommentFilter"/>
99100
</module>
100101
</module>

src/main/java/net/javadiscord/javabot/listener/HugListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import net.dv8tion.jda.api.hooks.ListenerAdapter;
1111
import net.javadiscord.javabot.data.config.BotConfig;
1212
import net.javadiscord.javabot.systems.moderation.AutoMod;
13+
import net.javadiscord.javabot.util.ExceptionLogger;
1314
import net.javadiscord.javabot.util.WebhookUtil;
1415

1516
import javax.annotation.Nonnull;
@@ -77,7 +78,7 @@ public void onMessageReceived(@Nonnull MessageReceivedEvent event) {
7778
sb.append(content.substring(indexBkp));
7879
WebhookUtil.ensureWebhookExists(textChannel,
7980
wh -> sendWebhookMessage(wh, event.getMessage(), sb.toString(), threadId),
80-
e -> log.error("Webhook lookup/creation failed", e));
81+
e -> ExceptionLogger.capture(e, getClass().getSimpleName()));
8182
}
8283
}
8384

@@ -98,7 +99,7 @@ private char copyCase(String original, int index, char newChar) {
9899
private void sendWebhookMessage(Webhook webhook, Message originalMessage, String newMessageContent, long threadId) {
99100
WebhookUtil.mirrorMessageToWebhook(webhook, originalMessage, newMessageContent, threadId, null, null)
100101
.thenAccept(unused -> originalMessage.delete().queue()).exceptionally(e -> {
101-
log.error("replacing the content 'fuck' with 'hug' in an incoming message failed", e);
102+
ExceptionLogger.capture(e, getClass().getSimpleName());
102103
return null;
103104
});
104105
}

src/main/java/net/javadiscord/javabot/systems/qotw/submissions/SubmissionManager.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ public void acceptSubmission(@NotNull ThreadChannel thread, @NotNull User author
212212
WebhookUtil.mirrorMessageToWebhook(wh, message, message.getContentRaw(), newestPost.getIdLong(), null, lastMessage ? List.of(buildAuthorEmbed(author, bestAnswer)) : null).join();
213213
}
214214
}
215+
}).exceptionally(err->{
216+
ExceptionLogger.capture(err,getClass().getSimpleName());
217+
return null;
215218
}));
216219
}
217220
thread.getManager().setLocked(true).setArchived(true).queue();

src/main/java/net/javadiscord/javabot/util/MessageActionUtils.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@ public static void copyMessagesToNewThread(StandardGuildMessageChannel targetCha
9292
thread -> WebhookUtil.ensureWebhookExists(targetChannel, wh->{
9393
CompletableFuture<ReadonlyMessage> future = CompletableFuture.completedFuture(null);
9494
for (Message m : messages) {
95-
future = future.thenCompose(unused -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), thread.getIdLong(), null, null));
95+
future = future
96+
.thenCompose(unused -> WebhookUtil.mirrorMessageToWebhook(wh, m, m.getContentRaw(), thread.getIdLong(), null, null))
97+
.exceptionally(err -> {
98+
ExceptionLogger.capture(err, MessageActionUtils.class.getSimpleName());
99+
return null;
100+
});
96101
}
97102
future.thenAccept(unused -> onFinish.accept(thread));
98103
})

src/main/java/net/javadiscord/javabot/util/WebhookUtil.java

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,23 @@ public static void ensureWebhookExists(@NotNull IWebhookContainer channel, @NotN
5151
*/
5252
public static void ensureWebhookExists(@NotNull IWebhookContainer channel, @NotNull Consumer<? super Webhook> callback, @NotNull Consumer<? super Throwable> failureCallback) {
5353

54+
Consumer<? super Webhook> safeCallback = wh -> {
55+
try {
56+
callback.accept(wh);
57+
//CHECKSTYLE:OFF: IllegalCatch - This should make sure it is properly logged if anything bad happens
58+
} catch (Exception e) {
59+
//CHECKSTYLE:ON: IllegalCatch
60+
failureCallback.accept(e);
61+
}
62+
};
5463
channel.retrieveWebhooks().queue(webhooks -> {
5564
Optional<Webhook> hook = webhooks.stream()
5665
.filter(webhook -> webhook.getChannel().getIdLong() == channel.getIdLong())
5766
.filter(wh -> wh.getToken() != null).findAny();
5867
if (hook.isPresent()) {
59-
callback.accept(hook.get());
68+
safeCallback.accept(hook.get());
6069
} else {
61-
channel.createWebhook("JavaBot-webhook").queue(callback, failureCallback);
70+
channel.createWebhook("JavaBot-webhook").queue(safeCallback, failureCallback);
6271
}
6372
}, failureCallback);
6473
}
@@ -98,6 +107,11 @@ public static CompletableFuture<ReadonlyMessage> mirrorMessageToWebhook(@NotNull
98107
is -> message.addFile((attachment.isSpoiler() ? "SPOILER_" : "") + attachment.getFileName(), is));
99108
}
100109
return CompletableFuture.allOf(futures).thenCompose(unused -> client.send(message.build()))
101-
.whenComplete((result, err) -> client.close());
110+
.whenComplete((result, err) -> {
111+
client.close();
112+
if( err != null) {
113+
ExceptionLogger.capture(err, WebhookUtil.class.getSimpleName());
114+
}
115+
});
102116
}
103117
}

0 commit comments

Comments
 (0)