/// <summary>
 /// Protected constructor. Used by serialization frameworks while
 /// deserializing an exception object.
 /// </summary>
 /// <param name="info">Info about the serialization context.</param>
 /// <param name="context">A streaming context that represents the
 /// serialization stream.</param>
 protected DfaApiException(SerializationInfo info, StreamingContext context)
     : base(info, context) {
   if (info == null) {
     throw new ArgumentNullException("info");
   }
   errorCode = GetValue<ErrorCode>(info, "errorCode");
 }
 /// <summary>
 /// Public constructor.
 /// </summary>
 /// <param name="errorCode">The parsed error code from the server.</param>
 /// <param name="message">Error message for this API exception.</param>
 /// <param name="innerException">Inner exception, if any.</param>
 public DfaApiException(ErrorCode errorCode, string message, Exception innerException)
     : base(message, innerException) {
   this.errorCode = errorCode;
 }
 /// <summary>
 /// Public constructor.
 /// </summary>
 /// <param name="errorCode">The parsed error code from the server.</param>
 public DfaApiException(ErrorCode errorCode) : base() {
   this.errorCode = errorCode;
 }
 /// <summary>
 /// Public constructor.
 /// </summary>
 /// <param name="errorCode">The parsed error code from the server.</param>
 /// <param name="message">Error message for this API exception.</param>
 public DfaApiException(ErrorCode errorCode, string message) : base(message) {
   this.errorCode = errorCode;
 }
    /// <summary>
    /// Gets a custom exception that wraps the SOAP exception thrown
    /// by the server.
    /// </summary>
    /// <param name="ex">SOAPException that was thrown by the server.</param>
    /// <returns>A custom exception object that wraps the SOAP exception.
    /// </returns>
    protected override Exception GetCustomException(SoapException ex) {
      string defaultNs = GetDefaultNamespace();

      object apiException = Activator.CreateInstance(Type.GetType(
          this.GetType().Namespace + ".ApiException"));
      XmlNode faultNode = GetDetailsNode(ex);
      ErrorCode errorCode = null;

      if (faultNode != null) {
        errorCode = new ErrorCode();
        foreach (XmlElement xNode in faultNode.SelectNodes("*")) {
          switch (xNode.Name) {
            case "errorCode":
              errorCode.Code = int.Parse(xNode.InnerText);
              break;

            case "errorMessage":
              errorCode.Description = xNode.InnerText;
              break;
          }
        }
      }
      DfaApiException dfaApiException = new DfaApiException(errorCode, ex.Message, ex);
      if (DfaErrorHandler.IsTokenExpiredError(dfaApiException)) {
        return new DfaCredentialsExpiredException(this.Token);
      } else {
        return dfaApiException;
      }
    }