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