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
.apache
.commons
.logging
.Log
;
11 import org
.apache
.commons
.logging
.LogFactory
;
13 import com
.fasterxml
.jackson
.core
.JsonGenerator
;
14 import com
.fasterxml
.jackson
.core
.JsonProcessingException
;
15 import com
.fasterxml
.jackson
.databind
.ObjectMapper
;
17 /** Serialisable wrapper of a {@link Throwable}. */
18 public class CmsExceptionsChain
{
19 public final static Log log
= LogFactory
.getLog(CmsExceptionsChain
.class);
21 private List
<SystemException
> exceptions
= new ArrayList
<>();
23 public CmsExceptionsChain() {
27 public CmsExceptionsChain(Throwable exception
) {
28 writeException(exception
);
29 if (log
.isDebugEnabled())
30 log
.error("Exception chain", exception
);
33 public String
toJsonString(ObjectMapper objectMapper
) {
35 return objectMapper
.writerWithDefaultPrettyPrinter().writeValueAsString(this);
36 } catch (JsonProcessingException e
) {
37 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
41 public void writeAsJson(ObjectMapper objectMapper
, Writer writer
) {
43 JsonGenerator jg
= objectMapper
.writerWithDefaultPrettyPrinter().getFactory().createGenerator(writer
);
45 } catch (IOException e
) {
46 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
50 public void writeAsJson(ObjectMapper objectMapper
, HttpServletResponse resp
) {
52 resp
.setContentType("application/json");
54 writeAsJson(objectMapper
, resp
.getWriter());
55 } catch (IOException e
) {
56 throw new IllegalStateException("Cannot write system exceptions " + toString(), e
);
61 protected void writeException(Throwable exception
) {
62 SystemException systemException
= new SystemException(exception
);
63 exceptions
.add(systemException
);
64 Throwable cause
= exception
.getCause();
66 writeException(cause
);
69 public List
<SystemException
> getExceptions() {
73 public void setExceptions(List
<SystemException
> exceptions
) {
74 this.exceptions
= exceptions
;
77 /** An exception in the chain. */
78 public static class SystemException
{
80 private String message
;
81 private List
<String
> stackTrace
;
83 public SystemException() {
86 public SystemException(Throwable exception
) {
87 this.type
= exception
.getClass().getName();
88 this.message
= exception
.getMessage();
89 this.stackTrace
= new ArrayList
<>();
90 StackTraceElement
[] elems
= exception
.getStackTrace();
91 for (int i
= 0; i
< elems
.length
; i
++)
92 stackTrace
.add("at " + elems
[i
].toString());
95 public String
getType() {
99 public void setType(String type
) {
103 public String
getMessage() {
107 public void setMessage(String message
) {
108 this.message
= message
;
111 public List
<String
> getStackTrace() {
115 public void setStackTrace(List
<String
> stackTrace
) {
116 this.stackTrace
= stackTrace
;
120 public String
toString() {
121 return "System exception: " + type
+ ", " + message
+ ", " + stackTrace
;
127 public String
toString() {
128 return exceptions
.toString();
131 // public static void main(String[] args) throws Exception {
136 // } catch (Exception e) {
137 // throw new Exception("Less deep exception", e);
139 // } catch (Exception e) {
140 // throw new RuntimeException("Top exception", e);
142 // } catch (Exception e) {
143 // CmsExceptionsChain vjeSystemErrors = new CmsExceptionsChain(e);
144 // ObjectMapper objectMapper = new ObjectMapper();
145 // System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(vjeSystemErrors));
146 // System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(e));
147 // e.printStackTrace();
151 // static void testDeeper() throws Exception {
152 // throw new IllegalStateException("Deep exception");