X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Fintegration%2FCmsExceptionsChain.java;h=bbac176a5fd7c916b8ea686e5c06b342197ce0f4;hb=08490f85954fc85940d1182c12a825b33491c3ba;hp=a659a7e9cc5c2a59fe249fca4cd4ca3bb21707df;hpb=1ffd6c22683f3c03e4c1193ac6bc33c77e155d2b;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/integration/CmsExceptionsChain.java b/org.argeo.cms/src/org/argeo/cms/integration/CmsExceptionsChain.java index a659a7e9c..bbac176a5 100644 --- a/org.argeo.cms/src/org/argeo/cms/integration/CmsExceptionsChain.java +++ b/org.argeo.cms/src/org/argeo/cms/integration/CmsExceptionsChain.java @@ -2,34 +2,37 @@ package org.argeo.cms.integration; import java.io.IOException; import java.io.Writer; +import java.util.ArrayList; +import java.util.List; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.argeo.util.ExceptionsChain; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; /** Serialisable wrapper of a {@link Throwable}. */ -public class CmsExceptionsChain extends ExceptionsChain { +public class CmsExceptionsChain { public final static Log log = LogFactory.getLog(CmsExceptionsChain.class); + private List exceptions = new ArrayList<>(); + public CmsExceptionsChain() { super(); } public CmsExceptionsChain(Throwable exception) { - super(exception); + writeException(exception); if (log.isDebugEnabled()) log.error("Exception chain", exception); } public String toJsonString(ObjectMapper objectMapper) { try { - return objectMapper.writeValueAsString(this); + return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this); } catch (JsonProcessingException e) { throw new IllegalStateException("Cannot write system exceptions " + toString(), e); } @@ -37,7 +40,7 @@ public class CmsExceptionsChain extends ExceptionsChain { public void writeAsJson(ObjectMapper objectMapper, Writer writer) { try { - JsonGenerator jg = objectMapper.getFactory().createGenerator(writer); + JsonGenerator jg = objectMapper.writerWithDefaultPrettyPrinter().getFactory().createGenerator(writer); jg.writeObject(this); } catch (IOException e) { throw new IllegalStateException("Cannot write system exceptions " + toString(), e); @@ -54,27 +57,99 @@ public class CmsExceptionsChain extends ExceptionsChain { } } - public static void main(String[] args) throws Exception { - try { - try { - try { - testDeeper(); - } catch (Exception e) { - throw new Exception("Less deep exception", e); - } - } catch (Exception e) { - throw new RuntimeException("Top exception", e); - } - } catch (Exception e) { - CmsExceptionsChain vjeSystemErrors = new CmsExceptionsChain(e); - ObjectMapper objectMapper = new ObjectMapper(); - System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(vjeSystemErrors)); - e.printStackTrace(); + /** recursive */ + protected void writeException(Throwable exception) { + SystemException systemException = new SystemException(exception); + exceptions.add(systemException); + Throwable cause = exception.getCause(); + if (cause != null) + writeException(cause); + } + + public List getExceptions() { + return exceptions; + } + + public void setExceptions(List exceptions) { + this.exceptions = exceptions; + } + + /** An exception in the chain. */ + public static class SystemException { + private String type; + private String message; + private List stackTrace; + + public SystemException() { + } + + public SystemException(Throwable exception) { + this.type = exception.getClass().getName(); + this.message = exception.getMessage(); + this.stackTrace = new ArrayList<>(); + StackTraceElement[] elems = exception.getStackTrace(); + for (int i = 0; i < elems.length; i++) + stackTrace.add("at " + elems[i].toString()); } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getMessage() { + return message; + } + + public void setMessage(String message) { + this.message = message; + } + + public List getStackTrace() { + return stackTrace; + } + + public void setStackTrace(List stackTrace) { + this.stackTrace = stackTrace; + } + + @Override + public String toString() { + return "System exception: " + type + ", " + message + ", " + stackTrace; + } + } - static void testDeeper() throws Exception { - throw new IllegalStateException("Deep exception"); + @Override + public String toString() { + return exceptions.toString(); } +// public static void main(String[] args) throws Exception { +// try { +// try { +// try { +// testDeeper(); +// } catch (Exception e) { +// throw new Exception("Less deep exception", e); +// } +// } catch (Exception e) { +// throw new RuntimeException("Top exception", e); +// } +// } catch (Exception e) { +// CmsExceptionsChain vjeSystemErrors = new CmsExceptionsChain(e); +// ObjectMapper objectMapper = new ObjectMapper(); +// System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(vjeSystemErrors)); +// System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(e)); +// e.printStackTrace(); +// } +// } +// +// static void testDeeper() throws Exception { +// throw new IllegalStateException("Deep exception"); +// } + }