]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.ee/src/org/argeo/cms/integration/CmsExceptionsChain.java
Clarify system roles
[lgpl/argeo-commons.git] / org.argeo.cms.ee / src / org / argeo / cms / integration / CmsExceptionsChain.java
1 package org.argeo.cms.integration;
2
3 import java.io.IOException;
4 import java.io.Writer;
5 import java.util.ArrayList;
6 import java.util.List;
7
8 import javax.servlet.http.HttpServletResponse;
9
10 import org.argeo.api.cms.CmsLog;
11
12 import com.fasterxml.jackson.core.JsonGenerator;
13 import com.fasterxml.jackson.core.JsonProcessingException;
14 import com.fasterxml.jackson.databind.ObjectMapper;
15
16 /** Serialisable wrapper of a {@link Throwable}. */
17 public class CmsExceptionsChain {
18 public final static CmsLog log = CmsLog.getLog(CmsExceptionsChain.class);
19
20 private List<SystemException> exceptions = new ArrayList<>();
21
22 public CmsExceptionsChain() {
23 super();
24 }
25
26 public CmsExceptionsChain(Throwable exception) {
27 writeException(exception);
28 if (log.isDebugEnabled())
29 log.error("Exception chain", exception);
30 }
31
32 public String toJsonString(ObjectMapper objectMapper) {
33 try {
34 return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);
35 } catch (JsonProcessingException e) {
36 throw new IllegalStateException("Cannot write system exceptions " + toString(), e);
37 }
38 }
39
40 public void writeAsJson(ObjectMapper objectMapper, Writer writer) {
41 try {
42 JsonGenerator jg = objectMapper.writerWithDefaultPrettyPrinter().getFactory().createGenerator(writer);
43 jg.writeObject(this);
44 } catch (IOException e) {
45 throw new IllegalStateException("Cannot write system exceptions " + toString(), e);
46 }
47 }
48
49 public void writeAsJson(ObjectMapper objectMapper, HttpServletResponse resp) {
50 try {
51 resp.setContentType("application/json");
52 resp.setStatus(500);
53 writeAsJson(objectMapper, resp.getWriter());
54 } catch (IOException e) {
55 throw new IllegalStateException("Cannot write system exceptions " + toString(), e);
56 }
57 }
58
59 /** recursive */
60 protected void writeException(Throwable exception) {
61 SystemException systemException = new SystemException(exception);
62 exceptions.add(systemException);
63 Throwable cause = exception.getCause();
64 if (cause != null)
65 writeException(cause);
66 }
67
68 public List<SystemException> getExceptions() {
69 return exceptions;
70 }
71
72 public void setExceptions(List<SystemException> exceptions) {
73 this.exceptions = exceptions;
74 }
75
76 /** An exception in the chain. */
77 public static class SystemException {
78 private String type;
79 private String message;
80 private List<String> stackTrace;
81
82 public SystemException() {
83 }
84
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());
92 }
93
94 public String getType() {
95 return type;
96 }
97
98 public void setType(String type) {
99 this.type = type;
100 }
101
102 public String getMessage() {
103 return message;
104 }
105
106 public void setMessage(String message) {
107 this.message = message;
108 }
109
110 public List<String> getStackTrace() {
111 return stackTrace;
112 }
113
114 public void setStackTrace(List<String> stackTrace) {
115 this.stackTrace = stackTrace;
116 }
117
118 @Override
119 public String toString() {
120 return "System exception: " + type + ", " + message + ", " + stackTrace;
121 }
122
123 }
124
125 @Override
126 public String toString() {
127 return exceptions.toString();
128 }
129
130 // public static void main(String[] args) throws Exception {
131 // try {
132 // try {
133 // try {
134 // testDeeper();
135 // } catch (Exception e) {
136 // throw new Exception("Less deep exception", e);
137 // }
138 // } catch (Exception e) {
139 // throw new RuntimeException("Top exception", e);
140 // }
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();
147 // }
148 // }
149 //
150 // static void testDeeper() throws Exception {
151 // throw new IllegalStateException("Deep exception");
152 // }
153
154 }