KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > client > am > ExceptionFormatter


1 /*
2
3    Derby - Class org.apache.derby.client.am.ExceptionFormatter
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20 */

21 package org.apache.derby.client.am;
22
23 public class ExceptionFormatter {
24     // returnTokensOnly is true only when exception tracing is enabled so
25
// that we don't try to go to the server for a message while we're in
26
// the middle of parsing an Sqlca reply.
27
// Without this, if e.getMessage() fails, we would have infinite recursion
28
// when TRACE_DIAGNOSTICS is on because tracing occurs within the exception constructor.
29
static public void printTrace(SqlException e,
30                                   java.io.PrintWriter JavaDoc printWriter,
31                                   String JavaDoc messageHeader,
32                                   boolean returnTokensOnly) {
33         String JavaDoc 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 JavaDoc throwable = null;
40                 try {
41                     throwable = ((Diagnosable) e).getThrowable();
42                 } catch (java.lang.NoSuchMethodError JavaDoc 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                     // JDK stack trace calls e.getMessage(), so we must set some state on the sqlca that says return tokens only.
51
((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) { // Too much has changed, so escape out here.
57
printWriter.println(header + " Message = " + e.getMessage());
58                 } else { // This is server-side error.
59
sqlca = ((Diagnosable) e).getSqlca();
60                     if (returnTokensOnly) {
61                         // print message tokens directly.
62
printWriter.println(header + " Tokens = " + sqlca.getSqlErrmc()); // a string containing error tokens only
63
} else {
64                         // Try to get message text from server.
65
String JavaDoc message = e.getMessage();
66                         if (!sqlca.messageTextRetrievedContainsTokensOnly_) { // got the message text.
67
printWriter.println(header + " Message = " + message);
68                         } else { // got only message tokens.
69
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                         // JDK stack trace calls e.getMessage(), now that it is finished,
92
// we can reset the state on the sqlca that says return tokens only.
93
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 JavaDoc e,
105                                   java.io.PrintWriter JavaDoc printWriter,
106                                   String JavaDoc messageHeader,
107                                   boolean returnTokensOnly) {
108         String JavaDoc header;
109         synchronized (printWriter) {
110             while (e != null) {
111                 if (e instanceof java.sql.DataTruncation JavaDoc) {
112                     header = messageHeader + "[" + "DataTruncation@" + Integer.toHexString(e.hashCode()) + "]";
113                     printWriter.println(header + " java.sql.DataTruncation");
114                 } else if (e instanceof java.sql.SQLWarning JavaDoc) {
115                     header = messageHeader + "[" + "SQLWarning@" + Integer.toHexString(e.hashCode()) + "]";
116                     printWriter.println(header + " java.sql.SQLWarning");
117                 } else if (e instanceof java.sql.BatchUpdateException JavaDoc) {
118                     header = messageHeader + "[" + "BatchUpdateException@" + Integer.toHexString(e.hashCode()) + "]";
119                     printWriter.println(header + " java.sql.BatchUpdateException");
120                 } else { // e instanceof java.sql.SQLException
121
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 JavaDoc) {
130                     printWriter.println(header + " Index = " + ((java.sql.DataTruncation JavaDoc) e).getIndex());
131                     printWriter.println(header + " Parameter = " + ((java.sql.DataTruncation JavaDoc) e).getParameter());
132                     printWriter.println(header + " Read = " + ((java.sql.DataTruncation JavaDoc) e).getRead());
133                     printWriter.println(header + " Data size = " + ((java.sql.DataTruncation JavaDoc) e).getDataSize());
134                     printWriter.println(header + " Transfer size = " + ((java.sql.DataTruncation JavaDoc) e).getTransferSize());
135                 }
136
137                 if (e instanceof java.sql.BatchUpdateException JavaDoc) {
138                     printWriter.println(header + " Update counts = " + Utils.getStringFromInts(((java.sql.BatchUpdateException JavaDoc) 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 JavaDoc printWriter,
153                                   String JavaDoc messageHeader) {
154         String JavaDoc 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 JavaDoc(sqlca.getSqlWarn()));
164         }
165     }
166
167     static public void printTrace(java.lang.Throwable JavaDoc e,
168                                   java.io.PrintWriter JavaDoc printWriter,
169                                   String JavaDoc messageHeader) {
170         String JavaDoc 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 JavaDoc e,
180                                   java.io.PrintWriter JavaDoc printWriter,
181                                   String JavaDoc messageHeader) {
182         String JavaDoc 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 not jre 1.4 or above, we need to print the cause if there is one
193
// For jre 1.4 or above, e.printStackTrace() will print the cause automatically
194
if (e instanceof Diagnosable) {
195                     java.lang.Throwable JavaDoc throwable = null;
196                     try {
197                         throwable = ((Diagnosable) e).getThrowable();
198                     } catch (java.lang.NoSuchMethodError JavaDoc doNothing) {
199                     }
200                     if (throwable != null) {
201                         printWriter.print("Caused by: ");
202                         if (throwable instanceof java.sql.SQLException JavaDoc) {
203                             throwable.printStackTrace(printWriter);
204                         } else {
205                             printTrace(throwable, printWriter, header);
206                         }
207                     }
208                 }
209             }
210         }
211     }
212 }
213
Popular Tags