/// <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 exception)
        {
            string defaultNs = GetDefaultNamespace();

            if (!string.IsNullOrEmpty(defaultNs) && exception.Detail != null)
            {
                // Extract the ApiExceptionFault node.
                XmlElement faultNode = GetFaultNode(exception, defaultNs, "ApiExceptionFault");

                if (faultNode != null)
                {
                    try {
                        AdWordsApiException awapiException = new AdWordsApiException(
                            SerializationUtilities.DeserializeFromXmlTextCustomRootNs(
                                faultNode.OuterXml, Assembly.GetExecutingAssembly().GetType(
                                    this.GetType().Namespace + ".ApiException"), defaultNs, "ApiExceptionFault"),
                            AdWordsErrorMessages.AnApiExceptionOccurred, exception);
                        if (AdWordsErrorHandler.IsOAuthTokenExpiredError(awapiException))
                        {
                            return(new AdWordsCredentialsExpiredException(
                                       (string)ContextStore.GetValue("OAuthHeader")));
                        }
                        else
                        {
                            return(awapiException);
                        }
                    } catch (Exception) {
                        // deserialization failed, but we can safely ignore it.
                    }
                }
            }
            return(new AdWordsApiException(null, AdWordsErrorMessages.AnApiExceptionOccurred, exception));
        }
 /// <summary>
 /// Determines whether the exception thrown by the server matches a known
 /// error.
 /// </summary>
 /// <param name="awapiException">The awapi exception.</param>
 /// <param name="errorMessage">The known error message.</param>
 /// <returns>True, if the server exception matches the known error, false
 /// otherwise.</returns>
 private static bool MatchesError(AdWordsApiException awapiException, string[] errorMessages)
 {
     object[] errors = (object[])awapiException.ApiException.GetType().
                       GetProperty("errors").GetValue(awapiException.ApiException, null);
     if (errors != null)
     {
         for (int i = 0; i < errors.Length; i++)
         {
             string errorString = (string)errors[i].GetType().GetProperty("errorString").
                                  GetValue(errors[i], null);
             foreach (String errorMessage in errorMessages)
             {
                 if (String.Compare(errorString, errorMessage, true) == 0)
                 {
                     return(true);
                 }
             }
         }
     }
     return(false);
 }
    /// <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();

      if (!string.IsNullOrEmpty(defaultNs) && ex.Detail != null) {
        // Extract the ApiExceptionFault node.
        XmlElement faultNode = GetFaultNode(ex, defaultNs, "ApiExceptionFault");

        if (faultNode != null) {
          try {
            AdWordsApiException awapiException = new AdWordsApiException(
                SerializationUtilities.DeserializeFromXmlTextCustomRootNs(
                    faultNode.OuterXml, Assembly.GetExecutingAssembly().GetType(
                    this.GetType().Namespace + ".ApiException"), defaultNs, "ApiExceptionFault"),
                    AdWordsErrorMessages.AnApiExceptionOccurred, ex);
            if (AdWordsErrorHandler.IsCookieInvalidError(awapiException)) {
              return new AdWordsCredentialsExpiredException(this.GetRequestHeader().authToken);
            } else if (AdWordsErrorHandler.IsOAuthTokenExpiredError(awapiException)) {
              return new AdWordsCredentialsExpiredException(
                  (string) ContextStore.GetValue("OAuthHeader"));
            } else {
              return awapiException;
            }
          } catch (Exception) {
            // deserialization failed, but we can safely ignore it.
          }
        }
      }
      return new AdWordsApiException(null, AdWordsErrorMessages.AnApiExceptionOccurred, ex);
    }
 /// <summary>
 /// Determines whether the exception thrown by the server matches a known
 /// error.
 /// </summary>
 /// <param name="awapiException">The awapi exception.</param>
 /// <param name="errorMessage">The known error message.</param>
 /// <returns>True, if the server exception matches the known error, false
 /// otherwise.</returns>
 private static bool MatchesError(AdWordsApiException awapiException, string[] errorMessages)
 {
     object[] errors = (object[]) awapiException.ApiException.GetType().
       GetProperty("errors").GetValue(awapiException.ApiException, null);
       if (errors != null) {
     for (int i = 0; i < errors.Length; i++) {
       string errorString = (string) errors[i].GetType().GetProperty("errorString").
       GetValue(errors[i], null);
       foreach (String errorMessage in errorMessages) {
     if (String.Compare(errorString, errorMessage, true) == 0) {
       return true;
     }
       }
     }
       }
       return false;
 }
 /// <summary>
 /// Invalidates the auth token cache if necessary.
 /// </summary>
 /// <param name="awapiException">The AdWords API exception from the server.
 /// </param>
 private void InvalidateAuthTokenCacheIfNecessary(AdWordsApiException awapiException) {
   RequestHeader header = (RequestHeader) this.GetType().GetProperty("RequestHeader").
       GetValue(this, null);
   if (IsCookieInvalidError(awapiException)) {
     AuthToken.Cache.InvalidateToken(header.authToken);
   }
 }
 /// <summary>
 /// Determines whether the exception thrown by the server is an AuthToken
 /// Invalid Error.
 /// </summary>
 /// <param name="awapiException">The awapi exception.</param>
 /// <returns>True, if the server exception is a AuthToken invalid error,
 /// false otherwise.</returns>
 private bool IsCookieInvalidError(AdWordsApiException awapiException) {
   object[] errors = (object[]) awapiException.ApiException.GetType().
       GetProperty("errors").GetValue(awapiException.ApiException, null);
   if (errors != null) {
     for (int i = 0; i < errors.Length; i++) {
       string errorString = (string) errors[i].GetType().GetProperty("errorString").
           GetValue(errors[i], null);
       if (errorString == COOKIE_INVALID_ERROR) {
         return true;
       }
     }
   }
   return false;
 }
    /// <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();

      if (!string.IsNullOrEmpty(defaultNs) && ex.Detail != null) {
        // Extract the ApiExceptionFault node.
        XmlElement faultNode = GetFaultNode(ex, defaultNs, "ApiExceptionFault");

        if (faultNode != null) {
          try {
            AdWordsApiException awapiException = new AdWordsApiException(
                SerializationUtilities.DeserializeFromXmlText(
                    faultNode.OuterXml, Assembly.GetExecutingAssembly().GetType(
                    this.GetType().Namespace + ".ApiException"), defaultNs, "ApiExceptionFault"),
                    AdWordsErrorMessages.AnApiExceptionOccurred, ex);

            InvalidateAuthTokenCacheIfNecessary(awapiException);
            return awapiException;
          } catch (Exception) {
            // deserialization failed, but we can safely ignore it.
          }
        }
      }
      return new AdWordsApiException(null, AdWordsErrorMessages.AnApiExceptionOccurred, ex);
    }