View Javadoc
1   /*
2    *    Copyright 2009-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 org.apache.ibatis.executor;
17  
18  /**
19   * @author Clinton Begin
20   */
21  public class ErrorContext {
22  
23    private static final String LINE_SEPARATOR = System.lineSeparator();
24    private static final ThreadLocal<ErrorContext> LOCAL = ThreadLocal.withInitial(ErrorContext::new);
25  
26    private ErrorContext stored;
27    private String resource;
28    private String activity;
29    private String object;
30    private String message;
31    private String sql;
32    private Throwable cause;
33  
34    private ErrorContext() {
35    }
36  
37    public static ErrorContext instance() {
38      return LOCAL.get();
39    }
40  
41    public ErrorContext store() {
42      ErrorContext newContext = new ErrorContext();
43      newContext.stored = this;
44      LOCAL.set(newContext);
45      return LOCAL.get();
46    }
47  
48    public ErrorContext recall() {
49      if (stored != null) {
50        LOCAL.set(stored);
51        stored = null;
52      }
53      return LOCAL.get();
54    }
55  
56    public ErrorContext resource(String resource) {
57      this.resource = resource;
58      return this;
59    }
60  
61    public ErrorContext activity(String activity) {
62      this.activity = activity;
63      return this;
64    }
65  
66    public ErrorContext object(String object) {
67      this.object = object;
68      return this;
69    }
70  
71    public ErrorContext message(String message) {
72      this.message = message;
73      return this;
74    }
75  
76    public ErrorContext sql(String sql) {
77      this.sql = sql;
78      return this;
79    }
80  
81    public ErrorContext cause(Throwable cause) {
82      this.cause = cause;
83      return this;
84    }
85  
86    public ErrorContext reset() {
87      resource = null;
88      activity = null;
89      object = null;
90      message = null;
91      sql = null;
92      cause = null;
93      LOCAL.remove();
94      return this;
95    }
96  
97    @Override
98    public String toString() {
99      StringBuilder description = new StringBuilder();
100 
101     // message
102     if (this.message != null) {
103       description.append(LINE_SEPARATOR);
104       description.append("### ");
105       description.append(this.message);
106     }
107 
108     // resource
109     if (resource != null) {
110       description.append(LINE_SEPARATOR);
111       description.append("### The error may exist in ");
112       description.append(resource);
113     }
114 
115     // object
116     if (object != null) {
117       description.append(LINE_SEPARATOR);
118       description.append("### The error may involve ");
119       description.append(object);
120     }
121 
122     // activity
123     if (activity != null) {
124       description.append(LINE_SEPARATOR);
125       description.append("### The error occurred while ");
126       description.append(activity);
127     }
128 
129     // sql
130     if (sql != null) {
131       description.append(LINE_SEPARATOR);
132       description.append("### SQL: ");
133       description.append(sql.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ').trim());
134     }
135 
136     // cause
137     if (cause != null) {
138       description.append(LINE_SEPARATOR);
139       description.append("### Cause: ");
140       description.append(cause.toString());
141     }
142 
143     return description.toString();
144   }
145 
146 }