/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2001-2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.except;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * The root exception of all exceptions thrown by the Venice core code.  It is capable of "wrapping"
 * another exception within it.  (Predating the support for this in JDK 1.4, of course.)
 *
 * @author Eric J. Bowersox &lt;erbo@ricochet.com&gt;
 * @version $Id$
 */
public class VeniceException extends Exception implements SupplyFaultCode
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  /** Internal "root cause" exception for this exception. */
  private final Throwable inner;

  /*--------------------------------------------------------------------------------
   * Constructors
   *--------------------------------------------------------------------------------
   */

  /**
   * Constructs a new <code>VeniceException</code>.
   */
  public VeniceException()
  {
    super();
    this.inner = null;

  } // end constructor

  /**
   * Constructs a new <code>VeniceException</code> with a text message.
   *
   * @param msg The message to set in this exception.
   */
  public VeniceException(String msg)
  {
    super(msg);
    this.inner = null;

  } // end constructor

  /**
   * Constructs a new <code>VeniceException</code> wrapping another exception.
   *
   * @param inner The exception wrapped by this one.
   */
  public VeniceException(Throwable inner)
  {
    super(inner.getMessage());
    this.inner = inner;

  } // end constructor

  /**
   * Constructs a new <code>VeniceException</code> wrapping another exception.
   *
   * @param msg The message to set in this exception.
   * @param inner The exception wrapped by this one.
   */
  public VeniceException(String msg, Throwable inner)
  {
    super(msg);
    this.inner = inner;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Overrides from class Throwable
   *--------------------------------------------------------------------------------
   */

  /**
   * Prints this exception and its backtrace to the standard error stream.  Also prints the backtrace
   * of any "wrapped" exception.
   *
   * @see java.lang.System#err
   */
  public void printStackTrace()
  {
    this.printStackTrace(System.err);

  } // end printStackTrace

  /**
   * Prints this exception and its backtrace to the specified {@link java.io.PrintStream PrintStream}.
   * Also prints the backtrace of any "wrapped" exception.
   *
   * @param s <code>PrintStream</code> to use for output.
   */
  public void printStackTrace(PrintStream s)
  {
    super.printStackTrace(s);
    printRootCause(s,inner);

  } // end printStackTrace

  /**
   * Prints this exception and its backtrace to the specified {@link java.io.PrintWriter PrintWriter}.
   * Also prints the backtrace of any "wrapped" exception.
   *
   * @param s <code>PrintWriter</code> to use for output.
   */
  public void printStackTrace(PrintWriter s)
  {
    super.printStackTrace(s);
    printRootCause(s,inner);

  } // end printStackTrace

  /*--------------------------------------------------------------------------------
   * Implementations from interface SupplyFaultCode
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns the XML-RPC "fault code" associated with this exception.
   *
   * @return See above.
   */
  public int getFaultCode()
  {
    return 0;  // default return

  } // end getFaultCode

  /*--------------------------------------------------------------------------------
   * External static operations
   *--------------------------------------------------------------------------------
   */

  /**
   * Prints the supplied exception as a "root cause" to the given {@link java.io.PrintStream PrintStream}.
   *
   * @param s <code>PrintStream</code> to print the exception to.
   * @param t The {@link java.lang.Throwable Throwable} to be printed.  If this parameter is
   *          <code>null</code>, this function is a no-op.
   */
  public static void printRootCause(PrintStream s, Throwable t)
  {
    if (t!=null)
    { // print the inner stack trace
      s.print("Root cause: ");
      t.printStackTrace(s);

    } // end if

  } // end printRootCause

  /**
   * Prints the supplied exception as a "root cause" to the given {@link java.io.PrintWriter PrintWriter}.
   *
   * @param s <code>PrintWriter</code> to print the exception to.
   * @param t The {@link java.lang.Throwable Throwable} to be printed.  If this parameter is
   *          <code>null</code>, this function is a no-op.
   */
  public static void printRootCause(PrintWriter s, Throwable t)
  {
    if (t!=null)
    { // print the inner stack trace
      s.print("Root cause: ");
      t.printStackTrace(s);

    } // end if

  } // end printRootCause

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  /**
   * Returns the exception wrapped by this exception, or <code>null</code> if there is none.
   *
   * @return See above.
   */
  public Throwable getException()
  {
    return inner;

  } // end getException

} // end class VeniceException
