From c76696c0e9025dd9e65af83b2a4d487343b6584b Mon Sep 17 00:00:00 2001 From: Matthias Ronge Date: Wed, 28 Oct 2020 15:36:16 +0100 Subject: [PATCH] Display root cause of error with its message Fixes #4073 --- .../org/kitodo/production/helper/Helper.java | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/Kitodo/src/main/java/org/kitodo/production/helper/Helper.java b/Kitodo/src/main/java/org/kitodo/production/helper/Helper.java index 0b197e2bec5..ff09debbfc6 100644 --- a/Kitodo/src/main/java/org/kitodo/production/helper/Helper.java +++ b/Kitodo/src/main/java/org/kitodo/production/helper/Helper.java @@ -33,6 +33,7 @@ import javax.faces.application.FacesMessage; import javax.faces.context.FacesContext; +import org.apache.commons.lang.StringUtils; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -161,12 +162,23 @@ public static void setErrorMessage(String title, final Object[] parameters) { public static void setErrorMessage(String title, Logger logger, Exception exception) { logger.error(title, exception); if (Objects.isNull(exception.getMessage()) || exception.getMessage().equals(title)) { - setErrorMessage(title); + setErrorMessage(getRootCause(exception)); } else { setErrorMessage(title, exception.getMessage()); } } + private static String getRootCause(Throwable problem) { + Throwable cause = problem.getCause(); + String className = problem.getClass().getSimpleName(); + if (Objects.nonNull(cause)) { + return className + " / " + getRootCause(cause); + } else { + String message = problem.getLocalizedMessage(); + return StringUtils.isEmpty(message) ? className : className + ": " + message; + } + } + /** * Set error message to message tag with given name 'title'. Substitute all * placeholders in message tag with elements of given array 'parameters'. @@ -272,12 +284,6 @@ public static void setMessage(String control, String message, String description * übergeben. */ private static void setMessage(String control, String message, String description, MessageLevel level) { - // Never forget: Strings are immutable - message = Objects.toString(message).replaceAll("<", "<"); - message = message.replaceAll(">", ">"); - description = Objects.toString(description).replaceAll("<", "<"); - description = description.replaceAll(">", ">"); - String msg = getTranslation(message); String descript = getTranslation(description);