using System;
using System.Security.Permissions;
namespace Csla
{
  /// 
  /// This exception is returned for any errors occuring
  /// during the server-side DataPortal invocation.
  /// 
  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1032:ImplementStandardExceptionConstructors")]
  [Serializable()]
  public class DataPortalException : Exception
  {
    private object _businessObject;
    private string _innerStackTrace;
    /// 
    /// Returns a reference to the business object
    /// from the server-side DataPortal.
    /// 
    /// 
    /// Remember that this object may be in an invalid
    /// or undefined state. This is the business object
    /// (and any child objects) as it existed when the
    /// exception occured on the server. Thus the object
    /// state may have been altered by the server and
    /// may no longer reflect data in the database.
    /// 
    public object BusinessObject
    {
      get { return _businessObject; }
    }
    /// 
    /// Gets the original server-side exception.
    /// 
    /// An exception object.
    /// 
    /// When an exception occurs in business code behind
    /// the data portal, it is wrapped in a 
    /// , which 
    /// is then wrapped in a 
    /// . This property
    /// unwraps and returns the original exception 
    /// thrown by the business code on the server.
    /// 
    public Exception BusinessException
    {
      get
      {
        return this.InnerException.InnerException;
      }
    }
    /// 
    /// Get the combined stack trace from the server
    /// and client.
    /// 
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object,System.Object,System.Object)")]
    public override string StackTrace
    {
      get { return String.Format("{0}{1}{2}", _innerStackTrace, Environment.NewLine, base.StackTrace); }
    }
    /// 
    /// Creates an instance of the object.
    /// 
    /// Text describing the exception.
    /// The business object
    /// as it was at the time of the exception.
    public DataPortalException(string message, object businessObject)
      : base(message)
    {
      _innerStackTrace = String.Empty;
      _businessObject = businessObject;
    }
    /// 
    /// Creates an instance of the object.
    /// 
    /// Text describing the exception.
    /// Inner exception.
    /// The business object
    /// as it was at the time of the exception.
    public DataPortalException(string message, Exception ex, object businessObject)
      : base(message, ex)
    {
      _innerStackTrace = ex.StackTrace;
      _businessObject = businessObject;
    }
    /// 
    /// Creates an instance of the object for serialization.
    /// 
    /// Serialiation info object.
    /// Serialization context object.
    protected DataPortalException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
      : base(info, context)
    {
      _businessObject = info.GetValue("_businessObject", typeof(object));
      _innerStackTrace = info.GetString("_innerStackTrace");
    }
    /// 
    /// Serializes the object.
    /// 
    /// Serialiation info object.
    /// Serialization context object.
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods")]
    [SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.SerializationFormatter)]
    [SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.SerializationFormatter)]
    public override void GetObjectData(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context)
    {
      base.GetObjectData(info, context);
      info.AddValue("_businessObject", _businessObject);
      info.AddValue("_innerStackTrace", _innerStackTrace);
    }
  }
}