001: /*
002: * ExceptionUtil.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.util;
013:
014: import java.io.PrintWriter;
015: import java.io.StringWriter;
016: import java.sql.SQLException;
017:
018: import workbench.log.LogMgr;
019:
020: /**
021: *
022: * @author support@sql-workbench.net
023: */
024: public class ExceptionUtil {
025:
026: private ExceptionUtil() {
027: }
028:
029: public static StringBuilder getSqlStateString(SQLException se) {
030: StringBuilder result = new StringBuilder(30);
031: try {
032: String state = se.getSQLState();
033: if (state != null && state.length() > 0) {
034: result.append("SQL State=");
035: result.append(state);
036: }
037: int error = se.getErrorCode();
038: if (error != 0) {
039: if (result.length() > 0)
040: result.append(", ");
041: result.append("DB Errorcode=");
042: result.append(Integer.toString(error));
043: }
044: } catch (Throwable th) {
045: //result.append("(unknown)");
046: }
047: return result;
048: }
049:
050: public static StringBuilder getAllExceptions(Throwable th) {
051: if (th instanceof SQLException) {
052: return getAllExceptions((SQLException) th);
053: }
054: StringBuilder result = new StringBuilder(100);
055: getDisplayBuffer(result, th, false);
056: Throwable cause = th.getCause();
057: while (cause != null) {
058: result.append("\nCaused by: ");
059: getDisplayBuffer(result, cause, false);
060: cause = cause.getCause();
061: }
062: return result;
063: }
064:
065: public static StringBuilder getAllExceptions(SQLException th) {
066: StringBuilder result = new StringBuilder(100);
067: getDisplayBuffer(result, th, false);
068: SQLException next = th.getNextException();
069: while (next != null) {
070: result.append("\nNext: ");
071: getDisplayBuffer(result, next, false);
072: next = next.getNextException();
073: }
074: return result;
075: }
076:
077: public static String getDisplay(Throwable th) {
078: if (th instanceof SQLException) {
079: return getAllExceptions((SQLException) th).toString();
080: } else {
081: return getDisplay(th, false);
082: }
083: }
084:
085: public static String getDisplay(Throwable th,
086: boolean includeStackTrace) {
087: return getDisplayBuffer(null, th, includeStackTrace).toString();
088: }
089:
090: public static StringBuilder getDisplayBuffer(StringBuilder result,
091: Throwable th, boolean includeStackTrace) {
092: if (result == null)
093: result = new StringBuilder(50);
094: try {
095: if (th.getMessage() == null) {
096: result.append(th.getClass().getName());
097: if (!includeStackTrace) {
098: // always include Stacktrace for NPE
099: // sometimes these are not properly logged, and this way
100: // the stacktrace does at least show up in the front end
101: // which should not happen anyway, but if it does,
102: // we have at least proper error information
103: includeStackTrace = (th instanceof NullPointerException);
104: }
105: } else {
106: result.append(th.getMessage().trim());
107: }
108:
109: if (th instanceof SQLException) {
110: SQLException se = (SQLException) th;
111: StringBuilder state = getSqlStateString(se);
112: if (state.length() > 0) {
113: result.append(" [");
114: result.append(state);
115: result.append("] ");
116: }
117: }
118:
119: if (includeStackTrace) {
120: StringWriter sw = new StringWriter();
121: PrintWriter pw = new PrintWriter(sw);
122: th.printStackTrace(pw);
123: result.append("\r\n");
124: result.append(sw.getBuffer());
125: }
126: } catch (Throwable th1) {
127: LogMgr.logError("ExceptionUtil.getDisplay()",
128: "Error while creating display string", th1);
129: result.append("Exception: " + th.getClass().getName());
130: }
131: return result;
132: }
133:
134: }
|