From 818c0e595f3e8d954c6e683356e918d8777f85d8 Mon Sep 17 00:00:00 2001 From: Struchkov Mark Date: Wed, 15 Mar 2023 21:28:54 +0300 Subject: [PATCH] ErrorHandler --- .../bot/conf/ErrorTelegramHandler.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/main/java/dev/struchkov/example/bot/conf/ErrorTelegramHandler.java diff --git a/src/main/java/dev/struchkov/example/bot/conf/ErrorTelegramHandler.java b/src/main/java/dev/struchkov/example/bot/conf/ErrorTelegramHandler.java new file mode 100644 index 0000000..89ad7cc --- /dev/null +++ b/src/main/java/dev/struchkov/example/bot/conf/ErrorTelegramHandler.java @@ -0,0 +1,33 @@ +package dev.struchkov.example.bot.conf; + +import dev.struchkov.godfather.main.domain.content.Message; +import dev.struchkov.godfather.simple.context.service.ErrorHandler; +import dev.struchkov.godfather.simple.domain.BoxAnswer; +import dev.struchkov.godfather.telegram.simple.context.service.TelegramSending; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import static dev.struchkov.haiti.utils.Strings.escapeMarkdown; + +@Slf4j +@Component +@RequiredArgsConstructor +public class ErrorTelegramHandler implements ErrorHandler { + + private final AppProperty appProperty; + private final TelegramSending sending; + + @Override + public void handle(Message message, Exception e) { + log.error(e.getMessage(), e); + final String errorMessage = escapeMarkdown(e.getMessage()); + sending.send( + BoxAnswer.builder() + .message("Error message:\n\n" + errorMessage) + .recipientPersonId(appProperty.getTelegramId()) + .build() + ); + } + +}