From 77a18aa5b1d6cd6cf4146d7541800ea0cb4904a5 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 16 Jun 2021 13:03:07 +0200 Subject: [PATCH] [New] ResponseUtil.toResponse to add i18n if available --- .../java/li/strolch/rest/helper/ResponseUtil.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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 0cfaebc1b..3c5adbb05 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 @@ -2,6 +2,7 @@ package li.strolch.rest.helper; import static li.strolch.rest.StrolchRestfulConstants.*; import static li.strolch.utils.helper.ExceptionHelper.getExceptionMessageWithCauses; +import static li.strolch.utils.helper.ExceptionHelper.getRootCause; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; @@ -15,6 +16,7 @@ import com.google.gson.JsonArray; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import li.strolch.exception.StrolchElementNotFoundException; +import li.strolch.exception.StrolchException; import li.strolch.model.i18n.I18nMessageToJsonVisitor; import li.strolch.privilege.base.AccessDeniedException; import li.strolch.privilege.base.PrivilegeException; @@ -176,6 +178,18 @@ public class ResponseUtil { public static Response toResponse(Status status, Throwable t) { JsonObject response = new JsonObject(); + + if (t instanceof StrolchException && ((StrolchException) t).hasI18n()) { + StrolchException ex = (StrolchException) t; + response.add("i18n", ex.getI18n().accept(new I18nMessageToJsonVisitor())); + } else { + Throwable rootCause = getRootCause(t); + if (rootCause instanceof StrolchException && ((StrolchException) rootCause).hasI18n()) { + StrolchException ex = (StrolchException) rootCause; + response.add("i18n", ex.getI18n().accept(new I18nMessageToJsonVisitor())); + } + } + response.addProperty(MSG, getExceptionMessageWithCauses(t, false)); String json = new Gson().toJson(response); return Response.status(status).entity(json).type(MediaType.APPLICATION_JSON).build();