ErrorContext.java

  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.  * @author Clinton Begin
  19.  */
  20. public class ErrorContext {

  21.   private static final String LINE_SEPARATOR = System.lineSeparator();
  22.   private static final ThreadLocal<ErrorContext> LOCAL = ThreadLocal.withInitial(ErrorContext::new);

  23.   private ErrorContext stored;
  24.   private String resource;
  25.   private String activity;
  26.   private String object;
  27.   private String message;
  28.   private String sql;
  29.   private Throwable cause;

  30.   private ErrorContext() {
  31.   }

  32.   public static ErrorContext instance() {
  33.     return LOCAL.get();
  34.   }

  35.   public ErrorContext store() {
  36.     ErrorContext newContext = new ErrorContext();
  37.     newContext.stored = this;
  38.     LOCAL.set(newContext);
  39.     return LOCAL.get();
  40.   }

  41.   public ErrorContext recall() {
  42.     if (stored != null) {
  43.       LOCAL.set(stored);
  44.       stored = null;
  45.     }
  46.     return LOCAL.get();
  47.   }

  48.   public ErrorContext resource(String resource) {
  49.     this.resource = resource;
  50.     return this;
  51.   }

  52.   public ErrorContext activity(String activity) {
  53.     this.activity = activity;
  54.     return this;
  55.   }

  56.   public ErrorContext object(String object) {
  57.     this.object = object;
  58.     return this;
  59.   }

  60.   public ErrorContext message(String message) {
  61.     this.message = message;
  62.     return this;
  63.   }

  64.   public ErrorContext sql(String sql) {
  65.     this.sql = sql;
  66.     return this;
  67.   }

  68.   public ErrorContext cause(Throwable cause) {
  69.     this.cause = cause;
  70.     return this;
  71.   }

  72.   public ErrorContext reset() {
  73.     resource = null;
  74.     activity = null;
  75.     object = null;
  76.     message = null;
  77.     sql = null;
  78.     cause = null;
  79.     LOCAL.remove();
  80.     return this;
  81.   }

  82.   @Override
  83.   public String toString() {
  84.     StringBuilder description = new StringBuilder();

  85.     // message
  86.     if (this.message != null) {
  87.       description.append(LINE_SEPARATOR);
  88.       description.append("### ");
  89.       description.append(this.message);
  90.     }

  91.     // resource
  92.     if (resource != null) {
  93.       description.append(LINE_SEPARATOR);
  94.       description.append("### The error may exist in ");
  95.       description.append(resource);
  96.     }

  97.     // object
  98.     if (object != null) {
  99.       description.append(LINE_SEPARATOR);
  100.       description.append("### The error may involve ");
  101.       description.append(object);
  102.     }

  103.     // activity
  104.     if (activity != null) {
  105.       description.append(LINE_SEPARATOR);
  106.       description.append("### The error occurred while ");
  107.       description.append(activity);
  108.     }

  109.     // sql
  110.     if (sql != null) {
  111.       description.append(LINE_SEPARATOR);
  112.       description.append("### SQL: ");
  113.       description.append(sql.replace('\n', ' ').replace('\r', ' ').replace('\t', ' ').trim());
  114.     }

  115.     // cause
  116.     if (cause != null) {
  117.       description.append(LINE_SEPARATOR);
  118.       description.append("### Cause: ");
  119.       description.append(cause.toString());
  120.     }

  121.     return description.toString();
  122.   }

  123. }