1 21 package org.apache.derby.client.am; 22 23 public class ExceptionFormatter { 24 static public void printTrace(SqlException e, 30 java.io.PrintWriter printWriter, 31 String messageHeader, 32 boolean returnTokensOnly) { 33 String header; 34 synchronized (printWriter) { 35 while (e != null) { 36 header = messageHeader + "[" + "SQLException@" + Integer.toHexString(e.hashCode()) + "]"; 37 printWriter.println(header + " java.sql.SQLException"); 38 39 java.lang.Throwable throwable = null; 40 try { 41 throwable = ((Diagnosable) e).getThrowable(); 42 } catch (java.lang.NoSuchMethodError doNothing) { 43 } 44 if (throwable != null) { 45 printTrace(throwable, printWriter, header); 46 } 47 Sqlca sqlca = ((Diagnosable) e).getSqlca(); 48 if (sqlca != null) { 49 printTrace(sqlca, printWriter, header); 50 ((Sqlca) sqlca).returnTokensOnlyInMessageText(returnTokensOnly); 52 } 53 54 printWriter.println(header + " SQL state = " + e.getSQLState()); 55 printWriter.println(header + " Error code = " + String.valueOf(e.getErrorCode())); 56 if (((Diagnosable) e).getSqlca() == null) { printWriter.println(header + " Message = " + e.getMessage()); 58 } else { sqlca = ((Diagnosable) e).getSqlca(); 60 if (returnTokensOnly) { 61 printWriter.println(header + " Tokens = " + sqlca.getSqlErrmc()); } else { 64 String message = e.getMessage(); 66 if (!sqlca.messageTextRetrievedContainsTokensOnly_) { printWriter.println(header + " Message = " + message); 68 } else { SqlException mysteryException = sqlca.exceptionThrownOnStoredProcInvocation_; 70 if (mysteryException != null && 71 (mysteryException.getErrorCode() == -440 || mysteryException.getErrorCode() == -444)) { 72 printWriter.println(header + " Unable to obtain message text from server." + 73 " Only message tokens are available." + 74 " The stored procedure SYSIBM.SQLCAMESSAGE is not installed on server." + 75 " Contact your DBA."); 76 } else { 77 printWriter.println(header + " Error occurred while trying to obtain message text from server. " + 78 "Only message tokens are available."); 79 } 80 printWriter.println(header + " Tokens = " + message); 81 } 82 } 83 } 84 85 printWriter.println(header + " Stack trace follows"); 86 e.printStackTrace(printWriter); 87 88 if (e instanceof Diagnosable) { 89 sqlca = (Sqlca) ((Diagnosable) e).getSqlca(); 90 if (sqlca != null) { 91 sqlca.returnTokensOnlyInMessageText(false); 94 } 95 } 96 97 e = e.getNextException(); 98 } 99 100 printWriter.flush(); 101 } 102 } 103 104 static public void printTrace(java.sql.SQLException e, 105 java.io.PrintWriter printWriter, 106 String messageHeader, 107 boolean returnTokensOnly) { 108 String header; 109 synchronized (printWriter) { 110 while (e != null) { 111 if (e instanceof java.sql.DataTruncation ) { 112 header = messageHeader + "[" + "DataTruncation@" + Integer.toHexString(e.hashCode()) + "]"; 113 printWriter.println(header + " java.sql.DataTruncation"); 114 } else if (e instanceof java.sql.SQLWarning ) { 115 header = messageHeader + "[" + "SQLWarning@" + Integer.toHexString(e.hashCode()) + "]"; 116 printWriter.println(header + " java.sql.SQLWarning"); 117 } else if (e instanceof java.sql.BatchUpdateException ) { 118 header = messageHeader + "[" + "BatchUpdateException@" + Integer.toHexString(e.hashCode()) + "]"; 119 printWriter.println(header + " java.sql.BatchUpdateException"); 120 } else { header = messageHeader + "[" + "SQLException@" + Integer.toHexString(e.hashCode()) + "]"; 122 printWriter.println(header + " java.sql.SQLException"); 123 } 124 125 printWriter.println(header + " SQL state = " + e.getSQLState()); 126 printWriter.println(header + " Error code = " + String.valueOf(e.getErrorCode())); 127 printWriter.println(header + " Message = " + e.getMessage()); 128 129 if (e instanceof java.sql.DataTruncation ) { 130 printWriter.println(header + " Index = " + ((java.sql.DataTruncation ) e).getIndex()); 131 printWriter.println(header + " Parameter = " + ((java.sql.DataTruncation ) e).getParameter()); 132 printWriter.println(header + " Read = " + ((java.sql.DataTruncation ) e).getRead()); 133 printWriter.println(header + " Data size = " + ((java.sql.DataTruncation ) e).getDataSize()); 134 printWriter.println(header + " Transfer size = " + ((java.sql.DataTruncation ) e).getTransferSize()); 135 } 136 137 if (e instanceof java.sql.BatchUpdateException ) { 138 printWriter.println(header + " Update counts = " + Utils.getStringFromInts(((java.sql.BatchUpdateException ) e).getUpdateCounts())); 139 } 140 141 printWriter.println(header + " Stack trace follows"); 142 e.printStackTrace(printWriter); 143 144 e = e.getNextException(); 145 } 146 147 printWriter.flush(); 148 } 149 } 150 151 static public void printTrace(Sqlca sqlca, 152 java.io.PrintWriter printWriter, 153 String messageHeader) { 154 String header = messageHeader + "[" + "Sqlca@" + Integer.toHexString(sqlca.hashCode()) + "]"; 155 synchronized (printWriter) { 156 printWriter.println(header + " DERBY SQLCA from server"); 157 printWriter.println(header + " SqlCode = " + sqlca.getSqlCode()); 158 printWriter.println(header + " SqlErrd = " + Utils.getStringFromInts(sqlca.getSqlErrd())); 159 printWriter.println(header + " SqlErrmc = " + sqlca.getSqlErrmc()); 160 printWriter.println(header + " SqlErrmcTokens = " + Utils.getStringFromStrings(sqlca.getSqlErrmcTokens())); 161 printWriter.println(header + " SqlErrp = " + sqlca.getSqlErrp()); 162 printWriter.println(header + " SqlState = " + sqlca.getSqlState()); 163 printWriter.println(header + " SqlWarn = " + new String (sqlca.getSqlWarn())); 164 } 165 } 166 167 static public void printTrace(java.lang.Throwable e, 168 java.io.PrintWriter printWriter, 169 String messageHeader) { 170 String header = messageHeader + "[" + "Throwable@" + Integer.toHexString(e.hashCode()) + "]"; 171 synchronized (printWriter) { 172 printWriter.println(header + " " + e.getClass().getName()); 173 printWriter.println(header + " Message = " + e.getMessage()); 174 printWriter.println(header + " Stack trace follows"); 175 e.printStackTrace(printWriter); 176 } 177 } 178 179 static public void printTrace(javax.transaction.xa.XAException e, 180 java.io.PrintWriter printWriter, 181 String messageHeader) { 182 String header = messageHeader + "[" + "XAException@" + Integer.toHexString(e.hashCode()) + "]"; 183 synchronized (printWriter) { 184 printWriter.println(header + " javax.transaction.xa.XAException"); 185 printWriter.println(header + " Message = " + e.getMessage()); 186 printWriter.println(header + " Stack trace follows"); 187 188 e.printStackTrace(printWriter); 189 190 if (!((org.apache.derby.client.am.Configuration.jreLevelMajor == 1) && 191 (org.apache.derby.client.am.Configuration.jreLevelMinor >= 4)) || 192 (org.apache.derby.client.am.Configuration.jreLevelMajor > 1)) { if (e instanceof Diagnosable) { 195 java.lang.Throwable throwable = null; 196 try { 197 throwable = ((Diagnosable) e).getThrowable(); 198 } catch (java.lang.NoSuchMethodError doNothing) { 199 } 200 if (throwable != null) { 201 printWriter.print("Caused by: "); 202 if (throwable instanceof java.sql.SQLException ) { 203 throwable.printStackTrace(printWriter); 204 } else { 205 printTrace(throwable, printWriter, header); 206 } 207 } 208 } 209 } 210 } 211 } 212 } 213 | Popular Tags |