]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/util/ExceptionsChain.java
Prepare next development cycle
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / util / ExceptionsChain.java
1 package org.argeo.cms.util;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 /**
7 * Serialisable wrapper of a {@link Throwable}. typically to be written as XML
8 * or JSON in a server error response.
9 */
10 public class ExceptionsChain {
11 private List<SystemException> exceptions = new ArrayList<>();
12
13 public ExceptionsChain() {
14 }
15
16 public ExceptionsChain(Throwable exception) {
17 writeException(exception);
18 }
19
20 /** recursive */
21 protected void writeException(Throwable exception) {
22 SystemException systemException = new SystemException(exception);
23 exceptions.add(systemException);
24 Throwable cause = exception.getCause();
25 if (cause != null)
26 writeException(cause);
27 }
28
29 public List<SystemException> getExceptions() {
30 return exceptions;
31 }
32
33 public void setExceptions(List<SystemException> exceptions) {
34 this.exceptions = exceptions;
35 }
36
37 /** An exception in the chain. */
38 public static class SystemException {
39 private String type;
40 private String message;
41 private List<String> stackTrace;
42
43 public SystemException() {
44 }
45
46 public SystemException(Throwable exception) {
47 this.type = exception.getClass().getName();
48 this.message = exception.getMessage();
49 this.stackTrace = new ArrayList<>();
50 StackTraceElement[] elems = exception.getStackTrace();
51 for (int i = 0; i < elems.length; i++)
52 stackTrace.add("at " + elems[i].toString());
53 }
54
55 public String getType() {
56 return type;
57 }
58
59 public void setType(String type) {
60 this.type = type;
61 }
62
63 public String getMessage() {
64 return message;
65 }
66
67 public void setMessage(String message) {
68 this.message = message;
69 }
70
71 public List<String> getStackTrace() {
72 return stackTrace;
73 }
74
75 public void setStackTrace(List<String> stackTrace) {
76 this.stackTrace = stackTrace;
77 }
78
79 @Override
80 public String toString() {
81 return "System exception: " + type + ", " + message + ", " + stackTrace;
82 }
83
84 }
85
86 @Override
87 public String toString() {
88 return exceptions.toString();
89 }
90 }