/*
* Copyright (C) 2006 http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*/
package org.chaidb.db.exception;
import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.AbstractList;
/**
* Provide the top level class representing the exception object.
* This class caters to the requirement of giving out a short error message, a
* long error message giving the details and the error code, in case of
* failures.
* <p/>
* The two products, IXD and IDC would subclass this class repsectively. For
* IXD the XDIException would subclass from this class.
*/
public class ChaiDBException extends Exception {
///////////////////////////////////
// Data members
// the error code
int m_code;
// the error message
String m_message;
// the details
String m_details;
//////////////////////////////////
// Constructors
////////////////////////////////////
// Group 1
public ChaiDBException(int code) {
m_code = code;
m_message = getMessage(code);
m_details = null;
}
public ChaiDBException(int code, AbstractList params) {
m_code = code;
m_message = getMessage(code, params);
m_details = null;
}
public ChaiDBException(int code, String[] params) {
m_code = code;
m_message = getMessage(code, params);
m_details = null;
}
///////////////////////////////////////////
// Group 2
public ChaiDBException(int code, String details) {
m_code = code;
m_message = getMessage(code);
m_details = details;
}
public ChaiDBException(int code, AbstractList params, String details) {
m_code = code;
m_message = getMessage(code, params);
m_details = details;
}
public ChaiDBException(int code, String[] params, String details) {
m_code = code;
m_message = getMessage(code, params);
m_details = details;
}
///////////////////////////////////////////
// Group 3
public ChaiDBException(int code, ChaiDBException ie) {
m_code = code;
m_message = getMessage(code);
// generate the detail message
if (ie != null) {
m_details = "[" + Integer.toString(ie.getErrorCode()) + "]" + ie.getMessage();
// if details are present
if (ie.getDetails() != null) {
if (ie.getDetails().length() != 0) m_details += ";" + ie.getDetails();
}
}
}
/**
* User the root exception stack as details.
*/
public ChaiDBException(int code, Throwable rootException) {
m_code = code;
m_message = getMessage(code);
m_details = getStack(rootException);
}
public ChaiDBException(int code, AbstractList params, ChaiDBException ie) {
m_code = code;
m_message = getMessage(code, params);
// generate the detail message
if (ie != null) {
m_details = "[" + Integer.toString(ie.getErrorCode()) + "]" + ie.getMessage();
// if details are present
if (ie.getDetails() != null) {
if (ie.getDetails().length() != 0) m_details += ";" + ie.getDetails();
}
}
}
//////////////////////////////////////////
// For the client side
public ChaiDBException(int code, String message, String details) {
m_code = code;
m_message = message;
m_details = details;
}
/////////////////////////////////
// Methods
/**
* Retrieve the error code stored in the object
*
* @return The error code.
*/
public int getErrorCode() {
return m_code;
}
protected String getStack(Throwable e) {
String stack = null;
try {
if (e != null) {
CharArrayWriter caw = new CharArrayWriter(1024 * 4);
PrintWriter pw = new PrintWriter(caw);
e.printStackTrace(pw);
stack = "------root cause:\n" + caw.toString();
}
} catch (Throwable t) {
stack = t.toString();
}
return stack;
}
/**
* Retrieve the error message stored in the object
*
* @return The error message.
*/
public String getMessage() {
return m_message;
}
/**
* Retrieve the details stored in the object
*
* @return The detail message.
*/
public String getDetails() {
return m_details;
}
/**
* Retrieve the exception object as string
*
* @return The string representation of the exception object.
*/
public String toString() {
// The format is:
// [error code] Error message; details (if not null)
String errString = "[" + Integer.toString(m_code) + "] " + m_message;
// if details are present
if (m_details != null) {
if (m_details.length() != 0) errString += m_details;
}
return errString;
}
//////////////////////////////////////////////////////////////////
// utility functions
public String getMessage(int errorCode) {
String errorCodeStr = Integer.toString(errorCode);
String message;
try {
ErrorMessages errorMessages = ErrorMessages.getInstance();
message = errorMessages.getMessage(errorCodeStr);
} catch (Exception e) {
message = "";
}
return message;
}
/**
* This method returns the error message corresponding to the error code
* and formatted with the parameters filled in.
*
* @param errorCode The error code.
* @param params The values for parameters in the error message
* @return The error message.
*/
public String getMessage(int errorCode, AbstractList params) {
String errorCodeStr = Integer.toString(errorCode);
String message;
try {
ErrorMessages errorMessages = ErrorMessages.getInstance();
message = errorMessages.getMessage(errorCodeStr, params);
} catch (Exception e) {
message = "";
}
return message;
}
/**
* This method returns the error message corresponding to the error code
* and formatted with the parameters filled in.
*
* @param errorCode The error code.
* @param params The values for parameters in the error message
* @return The error message.
*/
public String getMessage(int errorCode, String[] params) {
String errorCodeStr = Integer.toString(errorCode);
String message;
try {
ErrorMessages errorMessages = ErrorMessages.getInstance();
message = errorMessages.getMessage(errorCodeStr, params);
} catch (Exception e) {
message = "";
}
return message;
}
}