View Javadoc
1   /*
2    * Copyright 2004-2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package com.ibatis.sqlmap.engine.scope;
17  
18  /**
19   * An error context to help us create meaningful error messages.
20   */
21  public class ErrorContext {
22  
23    /** The resource. */
24    private String resource;
25  
26    /** The activity. */
27    private String activity;
28  
29    /** The object id. */
30    private String objectId;
31  
32    /** The more info. */
33    private String moreInfo;
34  
35    /** The cause. */
36    private Throwable cause;
37  
38    /**
39     * Getter for the resource causing the problem.
40     *
41     * @return - the resource
42     */
43    public String getResource() {
44      return resource;
45    }
46  
47    /**
48     * Setter for the resource causing the problem.
49     *
50     * @param resource
51     *          - the resource
52     */
53    public void setResource(String resource) {
54      this.resource = resource;
55    }
56  
57    /**
58     * Getter for the activity that was happening when the error happened.
59     *
60     * @return - the activity
61     */
62    public String getActivity() {
63      return activity;
64    }
65  
66    /**
67     * Getter for the activity that was happening when the error happened.
68     *
69     * @param activity
70     *          - the activity
71     */
72    public void setActivity(String activity) {
73      this.activity = activity;
74    }
75  
76    /**
77     * Getter for the object ID where the problem happened.
78     *
79     * @return - the object id
80     */
81    public String getObjectId() {
82      return objectId;
83    }
84  
85    /**
86     * Setter for the object ID where the problem happened.
87     *
88     * @param objectId
89     *          - the object id
90     */
91    public void setObjectId(String objectId) {
92      this.objectId = objectId;
93    }
94  
95    /**
96     * Getter for more information about the error.
97     *
98     * @return - more information
99     */
100   public String getMoreInfo() {
101     return moreInfo;
102   }
103 
104   /**
105    * Setter for more information about the error.
106    *
107    * @param moreInfo
108    *          - more information
109    */
110   public void setMoreInfo(String moreInfo) {
111     this.moreInfo = moreInfo;
112   }
113 
114   /**
115    * Getter for the cause of the error.
116    *
117    * @return - the cause
118    */
119   public Throwable getCause() {
120     return cause;
121   }
122 
123   /**
124    * Setter for the cause of the error.
125    *
126    * @param cause
127    *          - the cause
128    */
129   public void setCause(Throwable cause) {
130     this.cause = cause;
131   }
132 
133   @Override
134   public String toString() {
135     StringBuilder message = new StringBuilder();
136 
137     // resource
138     if (resource != null) {
139       message.append("  \n--- The error occurred in ");
140       message.append(resource);
141       message.append(".");
142     }
143 
144     // activity
145     if (activity != null) {
146       message.append("  \n--- The error occurred while ");
147       message.append(activity);
148       message.append(".");
149     }
150 
151     // object
152     if (objectId != null) {
153       message.append("  \n--- Check the ");
154       message.append(objectId);
155       message.append(".");
156     }
157 
158     // more info
159     if (moreInfo != null) {
160       message.append("  \n--- ");
161       message.append(moreInfo);
162     }
163 
164     // cause
165     if (cause != null) {
166       message.append("  \n--- Cause: ");
167       message.append(cause.toString());
168     }
169 
170     return message.toString();
171   }
172 
173   /**
174    * Clear the error context.
175    */
176   public void reset() {
177     resource = null;
178     activity = null;
179     objectId = null;
180     moreInfo = null;
181     cause = null;
182   }
183 
184 }