[New] Only add Exception class name in messages if message is null

This commit is contained in:
Robert von Burg 2019-12-11 18:27:50 +01:00
parent f95f3a5318
commit 793435454b
3 changed files with 56 additions and 9 deletions

View File

@ -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 <eitch@eitchnet.ch>
@ -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));
}

View File

@ -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();

View File

@ -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);
}
/**
* <p>
* Returns a message for the given {@link Throwable}
* </p>
*
* <p>
* A {@link NullPointerException} only has <code>null</code> as the message so this methods returns the class name
* in such a case
* </p>
*
* @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;
/**
* <p>
* Returns a message for the given {@link Throwable}
* </p>
*
* <p>
* A {@link NullPointerException} only has <code>null</code> as the message so this methods returns the class name
* in such a case
* </p>
*
* @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;
}
/**