View Javadoc

1   package org.apache.commons.digester3.binder;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import static java.lang.String.format;
23  
24  /**
25   * An error message and the context in which it occurred. Messages are usually created internally by
26   * {@code Digester} and its extensions. Messages can be created explicitly in a module using
27   * {@link org.apache.commons.digester3.binder.RulesBinder#addError(Throwable) addError()} statement:
28   *
29   * <pre>try {
30   *   bindRulesFromFile();
31   * } catch (IOException e) {
32   *   addError(e);
33   * }</pre>
34   */
35  final class ErrorMessage
36  {
37  
38      /**
39       * The error message text.
40       */
41      private final String message;
42  
43      /**
44       * The throwable that caused this message.
45       */
46      /* @Nullable */private final Throwable cause;
47  
48      /**
49       * Create a new {@link ErrorMessage} instance from the error message text.
50       *
51       * @param messagePattern The error message text pattern
52       * @param arguments Arguments referenced by the format specifiers in the format string
53       */
54      public ErrorMessage( String messagePattern, Object... arguments )
55      {
56          this( format( messagePattern, arguments ), (Throwable) null );
57      }
58  
59      /**
60       * Create a new {@link ErrorMessage} instance from the error message text and the related cause.
61       *
62       * @param message The error message text
63       * @param cause The throwable that caused this message
64       */
65      public ErrorMessage( String message, Throwable cause )
66      {
67          this.message = message;
68          this.cause = cause;
69      }
70  
71      /**
72       * Gets the error message text.
73       * 
74       * @return The error message text
75       */
76      public String getMessage()
77      {
78          return message;
79      }
80  
81      /**
82       * Returns the Throwable that caused this message, or {@code null} if this message was not caused by a Throwable.
83       *
84       * @return The Throwable that caused this message, or {@code null} if this message was not caused by a Throwable
85       */
86      public Throwable getCause()
87      {
88          return cause;
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public String toString()
96      {
97          return message;
98      }
99  
100 }