From 97022daf85b14fe34b6ef6fdcd24327608947306 Mon Sep 17 00:00:00 2001 From: SquidXTV Date: Wed, 24 Aug 2022 16:51:15 +0200 Subject: [PATCH 1/3] Fixed attachment error + close input stream correctly --- .../filesharing/FileSharingMessageListener.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) 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..751116306a 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,6 +185,7 @@ private void processAttachments(@NotNull MessageReceivedEvent event, if (statusCode < HttpURLConnection.HTTP_OK || statusCode >= HttpURLConnection.HTTP_MULT_CHOICE) { + LOGGER.warn("Request JSON: {}", body); throw new IllegalStateException("Gist API unexpected response: " + apiResponse.body()); } From 7ef887a27530bd2cd24097a7a951d46b77e0299a Mon Sep 17 00:00:00 2001 From: SquidXTV Date: Thu, 25 Aug 2022 08:59:33 +0200 Subject: [PATCH 2/3] Fixed logging issue --- .../commands/filesharing/FileSharingMessageListener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 751116306a..4c035a406a 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 @@ -185,8 +185,8 @@ private void processAttachments(@NotNull MessageReceivedEvent event, if (statusCode < HttpURLConnection.HTTP_OK || statusCode >= HttpURLConnection.HTTP_MULT_CHOICE) { - LOGGER.warn("Request JSON: {}", body); - throw new IllegalStateException("Gist API unexpected response: " + apiResponse.body()); + throw new IllegalStateException("Gist API unexpected response: " + apiResponse.body() + + ". Request JSON: " + body); } GistResponse gistResponse; From dac107c9c78f352d52f5fc170031bf6619b327c8 Mon Sep 17 00:00:00 2001 From: SquidXTV Date: Thu, 25 Aug 2022 09:01:33 +0200 Subject: [PATCH 3/3] Fixed logging issue --- .../commands/filesharing/FileSharingMessageListener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 4c035a406a..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 @@ -185,8 +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() - + ". Request JSON: " + body); + throw new IllegalStateException("Gist API unexpected response: %s. Request JSON: %s" + .formatted(apiResponse.body(), body)); } GistResponse gistResponse;