package com.fujitsu.iflowqa.testframe;

import java.io.PrintStream;


/**
* This class provides a simple wrapper for exceptions, with additional information
* to explain the context of the exception caught and rethrown.  It reports the
* original stack trace (and not the trace to the spot this class thrown).
*
* This class is for use with non-translatable TESTING code, do NOT use this for
* production PRODUCT code.
*/
class ExceptionWrapper
 extends Exception
{
    protected Exception cause;
    protected String    msg;

    public ExceptionWrapper(String newMsg, Exception newCause)
    {
        super(newMsg);
        msg = newMsg;
        cause = newCause;
    }

    public
    String
    getMessage()
    {
        StringBuffer result = new StringBuffer();
        result.append(msg);
        result.append(" because: {");
        result.append(cause.toString());
        result.append("}");
        return result.toString();
    }

    public
    String
    toString()
    {
        return getMessage();
    }


    /**************************************************************************
    * This prints the stack trace of the 'causing' exception, if there is
    * one.  If not, then it prints the stack trace of this exception.
    */
    public void printStackTrace(PrintStream s)
    {
        if (cause != null) {
            // consider whether we should print both this stack trace
            // as well as the causing stack trace.  This could be a lot of
            // redundant stack traces in some situations.
            cause.printStackTrace(s);
        }
        else {
            super.printStackTrace(s);
        }
    }
}

