1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
/*-------------------------------------------------------------------------
*
* Copyright (c) 2003-2005, PostgreSQL Global Development Group
*
* IDENTIFICATION
* $PostgreSQL: pgjdbc/org/postgresql/util/PSQLException.java.in,v 1.11 2005/04/10 16:44:13 jurka Exp $
*
*-------------------------------------------------------------------------
*/
package org.postgresql.util;
import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.sql.SQLException;
import org.postgresql.Driver;
public class PSQLException extends SQLException
{
private ServerErrorMessage _serverError;
public PSQLException(String msg, PSQLState state, Throwable cause)
{
super(addCauseToMessage(msg, cause), state == null ? null : state.getState());
initCause(cause); // Calls either the 1.4 Throwable impl or our impl below.
if (Driver.logDebug)
Driver.debug("Exception: " + this);
}
public PSQLException(String msg, PSQLState state)
{
this(msg, state, null);
}
public PSQLException(ServerErrorMessage serverError)
{
this(serverError.toString(), new PSQLState(serverError.getSQLState()));
_serverError = serverError;
}
public ServerErrorMessage getServerErrorMessage()
{
return _serverError;
}
private static String addCauseToMessage(String msg, Throwable cause) {
boolean hasInitCause = true;
@NOINITCAUSE@ hasInitCause = false;
if (!hasInitCause && cause != null)
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintWriter pw = new PrintWriter(baos);
pw.println();
pw.println(GT.tr("Exception: {0}", cause.toString()));
pw.println(GT.tr("Stack Trace:"));
cause.printStackTrace(pw);
pw.println(GT.tr("End of Stack Trace"));
pw.flush();
msg = msg + baos.toString();
pw.close();
baos.close();
}
catch (IOException ioe)
{
msg = msg + GT.tr("Exception generating stacktrace for: {0} encountered: {1}", new Object[] { cause.toString(), ioe.toString() });
}
}
return msg;
}
// Dummy initCause() for pre-1.4 JVMs
@NOINITCAUSE@ public void initCause(Throwable cause) {
@NOINITCAUSE@ }
}
|