-
Notifications
You must be signed in to change notification settings - Fork 0
/
ContextedRuntimeException.java
159 lines (145 loc) · 5.16 KB
/
ContextedRuntimeException.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.leoniedermeier.utils.excecption;
import java.util.ArrayList;
import java.util.List;
/**
* <p>
* A runtime exception that provides an easy and safe way to add contextual
* information.
* </p>
* <p>
* An exception trace itself is often insufficient to provide rapid diagnosis of
* the issue. Frequently what is needed is a select few pieces of local
* contextual data. Providing this data is tricky however, due to concerns over
* formatting and nulls.
* </p>
* <p>
* The contexted exception approach allows the exception to be created together
* with a list of context label-value pairs. This additional information is
* automatically included in the message and printed stack trace.
* </p>
* <p>
* To use this class write code as follows:
* </p>
*
* <pre>
* try {
* ...
* } catch (Exception e) {
* throw new ContextedRuntimeException(SomeErrorId, e)
* .addContextValue("Account Number", accountNumber)
* .addContextValue("Amount Posted", amountPosted)
* .addContextValue("Previous Balance", previousBalance)
* }
* }
* </pre>
* <p>
* or improve diagnose data at a higher level:
* </p>
*
* <pre>
* try {
* ...
* } catch (ContextedRuntimeException e) {
* throw e.setContextValue("Transaction Id", transactionId);
* }
* }
* </pre>
* <p>
* The output in a printStacktrace() (which often is written to a log) would
* look something like the following:
* </p>
*
* <pre>
* io.github.leoniedermeier.utils.excecption.ContextedRuntimeException:
* ErrorCodes=CODE_1, CODE_2
* CID=C7BWD6
* ---------------------------------
* Exception Context:
* [1:ErrorCode=CODE_1]
* [2:CID=C7BWD6]
* [3:label=MyValue]
* [4:ErrorCode=CODE_2]
* ---------------------------------
* ..... (rest of trace)
* </pre>
*
*/
public class ContextedRuntimeException extends RuntimeException
implements ExtendedExceptionContext<ContextedRuntimeException> {
@SuppressWarnings("squid:S4926")
private static final long serialVersionUID = 1667933980175471053L;
/** Where the data is stored. */
private final List<Entry> entries = new ArrayList<>();
public ContextedRuntimeException(ErrorCode errorCode) {
super();
init(errorCode);
}
public ContextedRuntimeException(ErrorCode errorCode, final String message) {
super(message);
init(errorCode);
}
public ContextedRuntimeException(ErrorCode errorCode, final String message, final Throwable cause) {
super(message, cause);
init(errorCode);
}
public ContextedRuntimeException(ErrorCode errorCode, final Throwable cause) {
super(cause);
init(errorCode);
}
@Override
public List<Entry> getContextEntries() {
return entries;
}
/**
* Provides the message explaining the exception, including the contextual data.
*
* @see java.lang.Throwable#getMessage()
* @return the message, never {@code null}.
*/
@Override
public String getMessage() {
return getFormattedExceptionMessage(super.getMessage());
}
/**
* Provides the message explaining the exception without the contextual data.
*
* @see java.lang.Throwable#getMessage()
* @return the message
*/
public String getRawMessage() {
return super.getMessage();
}
private void init(final ErrorCode errorCode) {
addContextValue(ErrorCode.class.getSimpleName(), errorCode);
addCID();
}
/**
* Casts the given {@link Throwable} to a {@link ContextedRuntimeException} if the given {@link Throwable}
* is an instance of {@link ContextedRuntimeException}, otherwise casts the given {@link Throwable} to a {@link ContextedRuntimeException}:
* @param errorCode The {@link ErrorCode} to use.
* @param throwable The {@link Throwable} to check.
* @return A {@link ContextedRuntimeException}.
*/
public static ContextedRuntimeException castOrCreate(ErrorCode errorCode, Throwable throwable){
if(throwable instanceof ContextedRuntimeException){
return ((ContextedRuntimeException)throwable).addErrorCode(errorCode);
}
return new ContextedRuntimeException(errorCode, throwable);
}
}