diff --git a/application/src/main/java/org/togetherjava/tjbot/commands/filesharing/FileSharingMessageListener.java b/application/src/main/java/org/togetherjava/tjbot/commands/filesharing/FileSharingMessageListener.java index 1e1c0a3639..0d78bb1036 100644 --- a/application/src/main/java/org/togetherjava/tjbot/commands/filesharing/FileSharingMessageListener.java +++ b/application/src/main/java/org/togetherjava/tjbot/commands/filesharing/FileSharingMessageListener.java @@ -78,11 +78,17 @@ public void onMessageReceived(@NotNull MessageReceivedEvent event) { .filter(this::isAttachmentRelevant) .toList(); + if (attachments.isEmpty()) { + return; + } + CompletableFuture.runAsync(() -> { try { processAttachments(event, attachments); } catch (Exception e) { - LOGGER.error("Unknown error while processing attachments", e); + LOGGER.error( + "Unknown error while processing attachments. Channel: {}, Author: {}, Message ID: {}.", + event.getChannel().getName(), author.getId(), event.getMessageId(), e); } }); } @@ -120,7 +126,7 @@ private void processAttachments(@NotNull MessageReceivedEvent event, } private @NotNull String readAttachment(@NotNull InputStream stream) { - try { + try (stream) { return new String(stream.readAllBytes(), StandardCharsets.UTF_8); } catch (IOException e) { throw new UncheckedIOException(e); @@ -179,7 +185,8 @@ private void processAttachments(@NotNull MessageReceivedEvent event, if (statusCode < HttpURLConnection.HTTP_OK || statusCode >= HttpURLConnection.HTTP_MULT_CHOICE) { - throw new IllegalStateException("Gist API unexpected response: " + apiResponse.body()); + throw new IllegalStateException("Gist API unexpected response: %s. Request JSON: %s" + .formatted(apiResponse.body(), body)); } GistResponse gistResponse;