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