1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.github.tonywasher.joceanus.metis.viewer;
18
19 import io.github.tonywasher.joceanus.oceanus.base.OceanusException;
20 import io.github.tonywasher.joceanus.oceanus.format.OceanusDataFormatter;
21 import io.github.tonywasher.joceanus.metis.field.MetisFieldItem;
22 import io.github.tonywasher.joceanus.metis.field.MetisFieldSet;
23
24
25
26
27 public class MetisViewerExceptionWrapper
28 implements MetisFieldItem {
29
30
31
32 private static final MetisFieldSet<MetisViewerExceptionWrapper> FIELD_DEFS = MetisFieldSet.newFieldSet(MetisViewerExceptionWrapper.class);
33
34
35
36
37 static {
38 FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_CLASS, MetisViewerExceptionWrapper::getClassName);
39 FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_MESSAGE, MetisViewerExceptionWrapper::getWrappedMessage);
40 FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_ORIGIN, MetisViewerExceptionWrapper::getWrappedCause);
41 FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_CAUSE, MetisViewerExceptionWrapper::getWrappedOrigin);
42 FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_OBJECT, MetisViewerExceptionWrapper::getWrappedObject);
43 FIELD_DEFS.declareLocalField(MetisViewerResource.VIEWER_ERROR_STACK, MetisViewerExceptionWrapper::getWrappedStack);
44 }
45
46
47
48
49 private final Throwable theWrapped;
50
51
52
53
54 private final String theClass;
55
56
57
58
59
60
61 public MetisViewerExceptionWrapper(final Throwable e) {
62
63 theWrapped = e;
64 theClass = theWrapped.getClass().getSimpleName();
65 }
66
67 @Override
68 public MetisFieldSet<MetisViewerExceptionWrapper> getDataFieldSet() {
69 return FIELD_DEFS;
70 }
71
72 @Override
73 public String formatObject(final OceanusDataFormatter pFormatter) {
74 return getMessage();
75 }
76
77
78
79
80
81
82 public String getMessage() {
83 return theClass
84 + ": "
85 + theWrapped.getMessage();
86 }
87
88
89
90
91
92
93 private String getClassName() {
94 return theClass;
95 }
96
97
98
99
100
101
102 private Throwable getWrappedCause() {
103 return theWrapped.getCause();
104 }
105
106
107
108
109
110
111 private String getWrappedMessage() {
112 return theWrapped.getMessage();
113 }
114
115
116
117
118
119
120 private StackTraceElement[] getWrappedStack() {
121 return theWrapped.getStackTrace();
122 }
123
124
125
126
127
128
129 private Object getWrappedObject() {
130 return theWrapped instanceof OceanusException myExc
131 ? myExc.getObject()
132 : null;
133 }
134
135
136
137
138
139
140 private Throwable getWrappedOrigin() {
141 Throwable myResult = theWrapped;
142 if (myResult.getCause() == null) {
143 return null;
144 }
145 while (myResult.getCause() != null) {
146 myResult = myResult.getCause();
147 }
148 return myResult;
149 }
150 }