From a63a1adafbca2dc7a1005b575b7aace07e3267ba Mon Sep 17 00:00:00 2001 From: bko Date: Tue, 2 Jul 2024 14:07:48 +0200 Subject: [PATCH] Shows more information for wrapped exceptions Sometimes we have to handle exceptions that are wrapped in other exceptions. Hopefully these wrapper-exceptions give us information about its wrapped exceptions in the message. Unfortunately, some of them does not. So we can try to catch the missing information from the overriden toString()-method that includes this information e.g. com.lowagie.text.ExceptionConverter Fixes: SIRI-985 --- src/main/java/sirius/biz/protocol/Protocols.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/sirius/biz/protocol/Protocols.java b/src/main/java/sirius/biz/protocol/Protocols.java index 6d11a96b3..83ed3f192 100644 --- a/src/main/java/sirius/biz/protocol/Protocols.java +++ b/src/main/java/sirius/biz/protocol/Protocols.java @@ -156,7 +156,7 @@ private String buildErrorMessages(Throwable throwable) { // Therefore, the first element is skipped. Throwables.getCausalChain(throwable).stream().skip(1).forEach(cause -> { stringBuilder.append("\n").append("Caused by: ").append(cause.getClass().getName()).append(":").append("\n"); - stringBuilder.append(truncateErrorMessage(cause.getMessage(), numberOfCharactersPerMessage)).append("\n\n"); + stringBuilder.append(truncateErrorMessage(determineErrorMessage(cause), numberOfCharactersPerMessage)).append("\n\n"); }); } catch (IllegalArgumentException exception) { // This happens if the causal chain has a circular reference. @@ -167,6 +167,16 @@ private String buildErrorMessages(Throwable throwable) { return stringBuilder.toString(); } + private String determineErrorMessage(Throwable throwable) { + if (Strings.isFilled(throwable.getMessage())) { + return throwable.getMessage(); + } + if (Strings.areEqual(throwable.getClass().getName(), throwable.toString())) { + return ""; + } + return throwable.toString(); + } + private String truncateErrorMessage(String errorMessage, int length) { int charsToPreserveFromStart = Math.max(0, length - NUMBER_OF_CHARS_TO_PRESERVE_AT_THE_END_OF_AN_ERROR_MESSAGE); return Strings.truncateMiddle(errorMessage, charsToPreserveFromStart, NUMBER_OF_CHARS_TO_PRESERVE_AT_THE_END_OF_AN_ERROR_MESSAGE);