/*
 * Copyright (c) 2003 by Hyperwave AG. All rights reserved.
 * Exception.js,v 1.9 2003/05/05 12:26:20 sraubal Exp
 */

/**
 * Encapsulates both the description and (if available) a cause
 * for an error situation. It forms the base class for all other
 * exceptions.
 *
 * @author sraubal
 */
defineClass ( "ojs.Exception",
              function ( class$ )
{
  /**
   * Constructs simple exceptions by giving a text (mandatory) and if
   * available a cause (that is defined by another exception).
   * 
   * @signature ( string aMessage )
   * @signature ( Object theParams )
   * 
   * @param aMessage message text describing the exception
   * @param theParams a parameter value object for the exception's
   *   constructor
   * @param theParams.message_: string: message text describing the
   *   exception
   * @param theParams.cause_: Object: if given this parameter
   *   defines the exception that was the reason for this one
   *   (can be an <code>ojs.Exception</code>, a subclass of it or browser
   *   error object) - interesting for descriptive <code>toString</code> output 
   */
  class$.constructor = function ( theParams )
  {
    if ( theParams == "__proto__")
      return;
  
    // the message text describing the exception
    if ( (typeof theParams).toLowerCase() == "string" )
      this.message_ = theParams;
    else
      this.message_ = theParams.message_;
      
    if ( !this.message_ )
      this.message_ = "(No exception message)";
    
    // nested exception, if there exists one
    this.cause_ = 
      (theParams && theParams.cause_) ? theParams.cause_ : null;
    
    this.stackTrace_ = getStackTrace().slice(1);
  }
  
  //-----------------------------------------------------------
  /**
   * Returns the message text of this exception. 
   *
   * @return string: a message text describing the exception.
   */
  class$.getMessage = function()
  {
    return this.message_;
  }
  
  //-----------------------------------------------------------
  /**
   * Returns the cause of this exception.
   *
   * @return Object: the nested exception that lead to this one (can be 
   *   an <code>ojs.Exception</code>, a subclass of it or browser error object)
   *   or <code>null</code> if no cause was defined during the exception.
   */
  class$.getCause = function()
  {
    return this.cause_;
  }
  
  //-----------------------------------------------------------
  /**
   * Returns a string representation of this exception including all
   * causes.<p>
   * This implementation will call the <code>toString</code> method of
   * the causes recursively.
   *
   * @return string: a text describing the exception.
   */
  class$.toString = function()
  {
    return this.static_.getClassName() + ": " 
           + this.message_ 
           + (this.cause_ 
              ? ", because of: " + (this.cause_.toString ? 
                this.cause_.toString() : this.cause_)   
              : ".") 
           + "\nStacktrace: " + this.stackTrace_.join("\n");
  }

} );

