From 6fcd4d85c10c75c926b02dceb27243554c96c1a6 Mon Sep 17 00:00:00 2001 From: Mouamle Date: Sat, 5 Sep 2020 03:53:42 +0300 Subject: [PATCH 1/2] Made webhook bots print exceptions stacktrace to stderr instead of silently swallowing them. --- .../DefaultExceptionMapper.java | 20 +++++++++++++++++++ .../updatesreceivers/DefaultWebhook.java | 1 + 2 files changed, 21 insertions(+) create mode 100644 telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultExceptionMapper.java diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultExceptionMapper.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultExceptionMapper.java new file mode 100644 index 00000000..1732fa53 --- /dev/null +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultExceptionMapper.java @@ -0,0 +1,20 @@ +package org.telegram.telegrambots.updatesreceivers; + +import javax.ws.rs.core.Response; +import javax.ws.rs.ext.ExceptionMapper; + +/** + * Prints exceptions in webhook bots to stderr + * + * @author Mouamle + * @version 1.0 + */ +public class DefaultExceptionMapper implements ExceptionMapper { + + @Override + public Response toResponse(Throwable exception) { + exception.printStackTrace(); + return Response.serverError().build(); + } + +} diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java index 4a8c4d31..c27806f4 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultWebhook.java @@ -52,6 +52,7 @@ public class DefaultWebhook implements Webhook { ResourceConfig rc = new ResourceConfig(); rc.register(restApi); rc.register(JacksonFeature.class); + rc.register(DefaultExceptionMapper.class); final HttpServer grizzlyServer; if (keystoreServerFile != null && keystoreServerPwd != null) { From 13f6b1f336a402d50f443127dbcb3113cc5afb28 Mon Sep 17 00:00:00 2001 From: Mouamle Date: Tue, 8 Sep 2020 01:18:18 +0300 Subject: [PATCH 2/2] used slf4j for logging instead of `exception.printStackTrace()` --- .../updatesreceivers/DefaultExceptionMapper.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultExceptionMapper.java b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultExceptionMapper.java index 1732fa53..7fa69be3 100644 --- a/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultExceptionMapper.java +++ b/telegrambots/src/main/java/org/telegram/telegrambots/updatesreceivers/DefaultExceptionMapper.java @@ -1,5 +1,8 @@ package org.telegram.telegrambots.updatesreceivers; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import javax.ws.rs.core.Response; import javax.ws.rs.ext.ExceptionMapper; @@ -10,11 +13,11 @@ import javax.ws.rs.ext.ExceptionMapper; * @version 1.0 */ public class DefaultExceptionMapper implements ExceptionMapper { + private static final Logger log = LoggerFactory.getLogger(DefaultExceptionMapper.class); @Override public Response toResponse(Throwable exception) { - exception.printStackTrace(); + log.error("Exception caught: ", exception); return Response.serverError().build(); } - }