1 package org
.argeo
.cms
.integration
;
3 import java
.io
.IOException
;
5 import java
.util
.ArrayList
;
8 import javax
.servlet
.http
.HttpServletResponse
;
10 import org
.argeo
.api
.cms
.CmsLog
;
12 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
13 import com
.fasterxml
.jackson
.core
.JsonProcessingException
;
14 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
16 /** Serialisable wrapper of a {@link Throwable}. */
17 public class CmsExceptionsChain
{
18 public final static CmsLog log
= CmsLog
.getLog(CmsExceptionsChain
.class);
20 private List
<SystemException
> exceptions
= new ArrayList
<>();
22 public CmsExceptionsChain() {
26 public CmsExceptionsChain(Throwable exception
) {
27 writeException(exception
);
28 if (log
.isDebugEnabled())
29 log
.error("Exception chain", exception
);
32 public String
toJsonString(ObjectMapper objectMapper
) {
34 return objectMapper
.writerWithDefaultPrettyPrinter().writeValueAsString(this);
35 } catch (JsonProcessingException e
) {
36 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
40 public void writeAsJson(ObjectMapper objectMapper
, Writer writer
) {
42 JsonGenerator jg
= objectMapper
.writerWithDefaultPrettyPrinter().getFactory().createGenerator(writer
);
44 } catch (IOException e
) {
45 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
49 public void writeAsJson(ObjectMapper objectMapper
, HttpServletResponse resp
) {
51 resp
.setContentType("application/json");
53 writeAsJson(objectMapper
, resp
.getWriter());
54 } catch (IOException e
) {
55 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
60 protected void writeException(Throwable exception
) {
61 SystemException systemException
= new SystemException(exception
);
62 exceptions
.add(systemException
);
63 Throwable cause
= exception
.getCause();
65 writeException(cause
);
68 public List
<SystemException
> getExceptions() {
72 public void setExceptions(List
<SystemException
> exceptions
) {
73 this.exceptions
= exceptions
;
76 /** An exception in the chain. */
77 public static class SystemException
{
79 private String message
;
80 private List
<String
> stackTrace
;
82 public SystemException() {
85 public SystemException(Throwable exception
) {
86 this.type
= exception
.getClass().getName();
87 this.message
= exception
.getMessage();
88 this.stackTrace
= new ArrayList
<>();
89 StackTraceElement
[] elems
= exception
.getStackTrace();
90 for (int i
= 0; i
< elems
.length
; i
++)
91 stackTrace
.add("at " + elems
[i
].toString());
94 public String
getType() {
98 public void setType(String type
) {
102 public String
getMessage() {
106 public void setMessage(String message
) {
107 this.message
= message
;
110 public List
<String
> getStackTrace() {
114 public void setStackTrace(List
<String
> stackTrace
) {
115 this.stackTrace
= stackTrace
;
119 public String
toString() {
120 return "System exception: " + type
+ ", " + message
+ ", " + stackTrace
;
126 public String
toString() {
127 return exceptions
.toString();
130 // public static void main(String[] args) throws Exception {
135 // } catch (Exception e) {
136 // throw new Exception("Less deep exception", e);
138 // } catch (Exception e) {
139 // throw new RuntimeException("Top exception", e);
141 // } catch (Exception e) {
142 // CmsExceptionsChain vjeSystemErrors = new CmsExceptionsChain(e);
143 // ObjectMapper objectMapper = new ObjectMapper();
144 // System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(vjeSystemErrors));
145 // System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(e));
146 // e.printStackTrace();
150 // static void testDeeper() throws Exception {
151 // throw new IllegalStateException("Deep exception");