diff --git a/li.strolch.agent/src/main/java/li/strolch/service/api/ServiceResult.java b/li.strolch.agent/src/main/java/li/strolch/service/api/ServiceResult.java index 6e0753cd7..478b5e221 100644 --- a/li.strolch.agent/src/main/java/li/strolch/service/api/ServiceResult.java +++ b/li.strolch.agent/src/main/java/li/strolch/service/api/ServiceResult.java @@ -16,6 +16,7 @@ package li.strolch.service.api; import static li.strolch.utils.helper.ExceptionHelper.formatException; +import static li.strolch.utils.helper.ExceptionHelper.getExceptionMessageWithCauses; import static li.strolch.utils.helper.StringHelper.isEmpty; import java.util.ResourceBundle; @@ -23,7 +24,6 @@ import java.util.ResourceBundle; import com.google.gson.JsonObject; import li.strolch.model.i18n.I18nMessageToJsonVisitor; import li.strolch.utils.I18nMessage; -import li.strolch.utils.helper.ExceptionHelper; /** * @author Robert von Burg @@ -198,7 +198,7 @@ public class ServiceResult { json.addProperty("msg", isEmpty(this.message) ? "-" : this.message); if (this.throwable != null) { - json.addProperty("exceptionMsg", ExceptionHelper.getExceptionMessageWithCauses(this.throwable)); + json.addProperty("exceptionMsg", getExceptionMessageWithCauses(this.throwable, false)); json.addProperty("throwable", formatException(this.throwable)); } diff --git a/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java b/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java index 3139ddbe1..e5fe58c0f 100644 --- a/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java +++ b/li.strolch.rest/src/main/java/li/strolch/rest/helper/ResponseUtil.java @@ -1,6 +1,7 @@ package li.strolch.rest.helper; import static li.strolch.rest.StrolchRestfulConstants.*; +import static li.strolch.utils.helper.ExceptionHelper.getExceptionMessageWithCauses; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -20,7 +21,6 @@ import li.strolch.privilege.base.PrivilegeModelException; import li.strolch.service.JsonServiceResult; import li.strolch.service.api.ServiceResult; import li.strolch.utils.collections.Paging; -import li.strolch.utils.helper.ExceptionHelper; import li.strolch.utils.helper.StringHelper; /** @@ -174,7 +174,7 @@ public class ResponseUtil { public static Response toResponse(Status status, Throwable t) { JsonObject response = new JsonObject(); - response.addProperty(MSG, ExceptionHelper.getExceptionMessageWithCauses(t)); + response.addProperty(MSG, getExceptionMessageWithCauses(t, false)); String json = new Gson().toJson(response); return Response.status(status).entity(json).type(MediaType.APPLICATION_JSON).build(); diff --git a/li.strolch.utils/src/main/java/li/strolch/utils/helper/ExceptionHelper.java b/li.strolch.utils/src/main/java/li/strolch/utils/helper/ExceptionHelper.java index 5da62203d..aa5812202 100644 --- a/li.strolch.utils/src/main/java/li/strolch/utils/helper/ExceptionHelper.java +++ b/li.strolch.utils/src/main/java/li/strolch/utils/helper/ExceptionHelper.java @@ -15,6 +15,7 @@ */ package li.strolch.utils.helper; +import static li.strolch.utils.helper.StringHelper.isEmpty; import static li.strolch.utils.helper.XmlHelper.PROP_LINE_SEPARATOR; import static li.strolch.utils.helper.XmlHelper.UNIX_LINE_SEP; @@ -44,7 +45,31 @@ public class ExceptionHelper { * @return the exception as string */ public static String getExceptionMessage(Throwable t) { - return t.getClass().getName() + ": " + t.getMessage(); + return getExceptionMessage(t, true); + } + + /** + *

+ * Returns a message for the given {@link Throwable} + *

+ * + *

+ * A {@link NullPointerException} only has null as the message so this methods returns the class name + * in such a case + *

+ * + * @param t + * the {@link Throwable} + * @param withClassName + * if true, then exception class name is prepended to the exception message, if the exception message is null, + * then this param is ignored + * + * @return the exception as string + */ + public static String getExceptionMessage(Throwable t, boolean withClassName) { + if (withClassName || isEmpty(t.getMessage())) + return t.getClass().getName() + ": " + t.getMessage(); + return t.getMessage(); } /** @@ -63,11 +88,33 @@ public class ExceptionHelper { * @return the exception as string */ public static String getExceptionMessageWithCauses(Throwable t) { - if (t.getCause() == null) - return getExceptionMessage(t); + return getExceptionMessageWithCauses(t, true); + } - String root = getExceptionMessageWithCauses(t.getCause()); - return getExceptionMessage(t) + "\n" + root; + /** + *

+ * Returns a message for the given {@link Throwable} + *

+ * + *

+ * A {@link NullPointerException} only has null as the message so this methods returns the class name + * in such a case + *

+ * + * @param t + * the {@link Throwable} + * @param withClassName + * if true, then exception class name is prepended to the exception message, if the exception message is null, + * then this param is ignored + * + * @return the exception as string + */ + public static String getExceptionMessageWithCauses(Throwable t, boolean withClassName) { + if (t.getCause() == null) + return getExceptionMessage(t, withClassName); + + String root = getExceptionMessageWithCauses(t.getCause(), withClassName); + return getExceptionMessage(t, withClassName) + "\n" + root; } /**