View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.lang3.exception;
18  
19  import java.util.List;
20  import java.util.Set;
21  
22  import org.apache.commons.lang3.tuple.Pair;
23  
24  /**
25   * An exception that provides an easy and safe way to add contextual information.
26   * <p>
27   * An exception trace itself is often insufficient to provide rapid diagnosis of the issue.
28   * Frequently what is needed is a select few pieces of local contextual data.
29   * Providing this data is tricky however, due to concerns over formatting and nulls.
30   * </p><p>
31   * The contexted exception approach allows the exception to be created together with a
32   * list of context label-value pairs. This additional information is automatically included in
33   * the message and printed stack trace.
34   * </p><p>
35   * An unchecked version of this exception is provided by ContextedRuntimeException.
36   * </p>
37   * <p>
38   * To use this class write code as follows:
39   * </p>
40   * <pre>
41   *   try {
42   *     ...
43   *   } catch (Exception e) {
44   *     throw new ContextedException("Error posting account transaction", e)
45   *          .addContextValue("Account Number", accountNumber)
46   *          .addContextValue("Amount Posted", amountPosted)
47   *          .addContextValue("Previous Balance", previousBalance);
48   *   }
49   * }
50   * </pre>
51   * <p>
52   * or improve diagnose data at a higher level:
53   * </p>
54   * <pre>
55   *   try {
56   *     ...
57   *   } catch (ContextedException e) {
58   *     throw e.setContextValue("Transaction Id", transactionId);
59   *   } catch (Exception e) {
60   *     if (e instanceof ExceptionContext) {
61   *       e.setContextValue("Transaction Id", transactionId);
62   *     }
63   *     throw e;
64   *   }
65   * }
66   * </pre>
67   * <p>
68   * The output in a printStacktrace() (which often is written to a log) would look something like the following:
69   * </p>
70   * <pre>
71   * org.apache.commons.lang3.exception.ContextedException: java.lang.Exception: Error posting account transaction
72   *  Exception Context:
73   *  [1:Account Number=null]
74   *  [2:Amount Posted=100.00]
75   *  [3:Previous Balance=-2.17]
76   *  [4:Transaction Id=94ef1d15-d443-46c4-822b-637f26244899]
77   *
78   *  ---------------------------------
79   *  at org.apache.commons.lang3.exception.ContextedExceptionTest.testAddValue(ContextedExceptionTest.java:88)
80   *  ..... (rest of trace)
81   * </pre>
82   *
83   * @see ContextedRuntimeException
84   * @since 3.0
85   */
86  public class ContextedException extends Exception implements ExceptionContext {
87  
88      /** The serialization version. */
89      private static final long serialVersionUID = 20110706L;
90  
91      /** The context where the data is stored. */
92      private final ExceptionContext exceptionContext;
93  
94      /**
95       * Instantiates ContextedException without message or cause.
96       * <p>
97       * The context information is stored using a default implementation.
98       */
99      public ContextedException() {
100         exceptionContext = new DefaultExceptionContext();
101     }
102 
103     /**
104      * Instantiates ContextedException with message, but without cause.
105      * <p>
106      * The context information is stored using a default implementation.
107      *
108      * @param message  the exception message, may be null
109      */
110     public ContextedException(final String message) {
111         super(message);
112         exceptionContext = new DefaultExceptionContext();
113     }
114 
115     /**
116      * Instantiates ContextedException with cause and message.
117      * <p>
118      * The context information is stored using a default implementation.
119      *
120      * @param message  the exception message, may be null
121      * @param cause  the underlying cause of the exception, may be null
122      */
123     public ContextedException(final String message, final Throwable cause) {
124         super(message, cause);
125         exceptionContext = new DefaultExceptionContext();
126     }
127 
128     /**
129      * Instantiates ContextedException with cause, message, and ExceptionContext.
130      *
131      * @param message  the exception message, may be null
132      * @param cause  the underlying cause of the exception, may be null
133      * @param context  the context used to store the additional information, null uses default implementation
134      */
135     public ContextedException(final String message, final Throwable cause, ExceptionContext context) {
136         super(message, cause);
137         if (context == null) {
138             context = new DefaultExceptionContext();
139         }
140         exceptionContext = context;
141     }
142 
143     /**
144      * Instantiates ContextedException with cause, but without message.
145      * <p>
146      * The context information is stored using a default implementation.
147      *
148      * @param cause  the underlying cause of the exception, may be null
149      */
150     public ContextedException(final Throwable cause) {
151         super(cause);
152         exceptionContext = new DefaultExceptionContext();
153     }
154 
155     /**
156      * Adds information helpful to a developer in diagnosing and correcting the problem.
157      * For the information to be meaningful, the value passed should have a reasonable
158      * toString() implementation.
159      * Different values can be added with the same label multiple times.
160      * <p>
161      * Note: This exception is only serializable if the object added is serializable.
162      * </p>
163      *
164      * @param label  a textual label associated with information, {@code null} not recommended
165      * @param value  information needed to understand exception, may be {@code null}
166      * @return {@code this}, for method chaining, not {@code null}
167      */
168     @Override
169     public ContextedException addContextValue(final String label, final Object value) {
170         exceptionContext.addContextValue(label, value);
171         return this;
172     }
173 
174     /**
175      * {@inheritDoc}
176      */
177     @Override
178     public List<Pair<String, Object>> getContextEntries() {
179         return this.exceptionContext.getContextEntries();
180     }
181 
182     /**
183      * {@inheritDoc}
184      */
185     @Override
186     public Set<String> getContextLabels() {
187         return exceptionContext.getContextLabels();
188     }
189 
190     /**
191      * {@inheritDoc}
192      */
193     @Override
194     public List<Object> getContextValues(final String label) {
195         return this.exceptionContext.getContextValues(label);
196     }
197 
198     /**
199      * {@inheritDoc}
200      */
201     @Override
202     public Object getFirstContextValue(final String label) {
203         return this.exceptionContext.getFirstContextValue(label);
204     }
205 
206     /**
207      * {@inheritDoc}
208      */
209     @Override
210     public String getFormattedExceptionMessage(final String baseMessage) {
211         return exceptionContext.getFormattedExceptionMessage(baseMessage);
212     }
213 
214     /**
215      * Provides the message explaining the exception, including the contextual data.
216      *
217      * @see Throwable#getMessage()
218      * @return the message, never null
219      */
220     @Override
221     public String getMessage() {
222         return getFormattedExceptionMessage(super.getMessage());
223     }
224 
225     /**
226      * Provides the message explaining the exception without the contextual data.
227      *
228      * @see Throwable#getMessage()
229      * @return the message
230      * @since 3.0.1
231      */
232     public String getRawMessage() {
233         return super.getMessage();
234     }
235 
236     /**
237      * Sets information helpful to a developer in diagnosing and correcting the problem.
238      * For the information to be meaningful, the value passed should have a reasonable
239      * toString() implementation.
240      * Any existing values with the same labels are removed before the new one is added.
241      * <p>
242      * Note: This exception is only serializable if the object added as value is serializable.
243      * </p>
244      *
245      * @param label  a textual label associated with information, {@code null} not recommended
246      * @param value  information needed to understand exception, may be {@code null}
247      * @return {@code this}, for method chaining, not {@code null}
248      */
249     @Override
250     public ContextedException setContextValue(final String label, final Object value) {
251         exceptionContext.setContextValue(label, value);
252         return this;
253     }
254 }