Export (0) Print
Expand All

Handling Exceptions

banner art

[Applies to: Microsoft Dynamics CRM 4.0]

The common exception that you receive when calling a Microsoft Dynamics CRM Web service method with a Request parameter is System.Web.Services.Protocols.SoapException. The Message property of the SoapException is always "Server was unable to process request."

The Detail property of the SoapException will have more information about the exact error that occurred in the platform. The Detail property type is an XmlNode, of which the InnerXml property value has the following format:

<error>
   <description>Error description goes here.</description>
   <code>0xABCD1234</code>
   <type>Platform</type>
</error>

The number value is a hexadecimal error code. For example, if the caller tries to retrieve a fax that does not exist in the database, the error XML string is returned as follows:

<error>
   <description>
   The fax with ID = a2a9545d-a0c2-42b4-9708-a0b2badd77c8 does not exist.
   </description>
   <code>0x80040217</code>
   <type>Platform</type>
</error>

For a complete list of Microsoft Dynamics CRM specific error codes, see Error Codes. The error codes are provided in an XML format so that you can read them into an XmlDocument and perform a search for the description associated with a particular error code.

You can find helper code for using error codes in the SDK download package in the folder SDK\Server\Helpers\CS\CrmHelpers\ErrorCodes.cs.

Example

The following code shows an example of writing out the error information in a catch statement for debugging.

try 
{
   queues = service.RetrieveMultiple(query);
} 
catch(System.Web.Services.Protocols.SoapException ex)
{
   Console.WriteLine(ex.Message + ". " + ex.Detail.InnerText);
}

A more robust error handling method is to extract the error code from the exception.

Example

The following code uses a custom GetErrorCode method.

static private string ExpiredAuthTicket = "8004A101";

try 
{
   // Try some Microsoft Dynamics CRM method where authentication occurs.
} 
// Handle any Web service exceptions that might be thrown.
catch (SoapException ex)
{
   // Handle the exception thrown from an expired ticket condition.
   if (GetErrorCode(ex.Detail).Equals(ExpiredAuthTicket))
   {
      // One possible action is to retrieve a new CrmTicket, set 
      // CrmAuthenticationToken.CrmTicket to the new ticket value,
      // then call a CrmService Web Service Method again.
      //
      // For this sample, merely throw the exception again.
      throw new Exception("The Microsoft Dynamics CRM ticket has expired.", ex);
   }
   else
   {
      // Handle other Soap exceptions.
      throw new Exception("An error occurred while trying to authenticate.", ex);
   }
}

The following code example returns the error code from a SoapException.

[C#]
/// <summary>
/// Returns the error code that is contained in SoapException.Detail.
/// </summary>
/// <param name="errorInfo">An XmlNode that contains application specific error information.</param>
/// <returns>Error code or zero.</returns>
private static string GetErrorCode(XmlNode errorInfo)
{
    XmlNode code = errorInfo.SelectSingleNode("//code");

    if (code != null)
        return code.InnerText;
    else
        return "";
}

For the complete code sample, see the PassportAuthentication.cs sample code at SDK\Walkthroughs\Authentication\CS\Passport.

See Also

Concepts

Other Resources


© 2010 Microsoft Corporation. All rights reserved.


Show:
© 2014 Microsoft