Exception handling

The QuickBooks Online .NETSDK uses the fault types supported by the QuickBooks Online APIs to return the specific exceptions in the Intuit web responses. The SDK supports the followingfaulttypes:

The flow in which the SDK handles exceptions is as follows:

  1. If the WebResponse has an HTTP status code other than 200 OK, the SDK checks the code and returns appropriate exception. For example, it returns “InvalidTokenException” for status code 401.
  2. If the WebResponse has an HTTP Status Code 200 OK, the SDK looks for a fault element within the IntuitResponse.
  3. If the WebResponse has a fault element, based on the fault type, the SDK returns an exception with appropriate error codes.

The following are the code samples that show how to implement exception handling for different fault types. For instructions on creating a DataService object, see Data Services API.

ValidationException

The ValidationException class handles Validation errors. The following example code shows how to handle a Validation error:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
try
{
   // Include the service call code here. The following code is for an add operation.
   Customer resultCustomer = service.Add(customer) as Customer;
}

catch (ValidationException exception)
{
   // Include the logic for how you want your app to handle the exception. This code writes a fault messages to a console.
   Console.WriteLine("Message:{0}, ErrorCode:{1}", exception.Message, exception.ErrorCode);
   foreach (IdsError idsError in exception.InnerExceptions)
   {
      Console.WriteLine("ErrorCode:{0}, Message:{1}, Element:{2}, Detail:{3}", idsError.ErrorCode, idsError.Message,
      idsError.Element, idsError.Detail);
   }
}
ServiceException

The ServiceException class handles Service errors. The following example code shows how to handle a Service error:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
try
{
   // Include the service call code here. The following code is for an add operation.
   Customer resultCustomer = service.Add(customer) as Customer;
}

catch (ServiceException exception)
{
   // Include the logic for how you want your app to handle the exception. This code writes a fault messages to a console.
   Console.WriteLine("Message:{0}, ErrorCode:{1}", exception.Message, exception.ErrorCode);
   foreach (IdsError idsError in exception.InnerExceptions)
   {
      Console.WriteLine("ErrorCode:{0}, Message:{1}, Element:{2}, Detail:{3}", idsError.ErrorCode, idsError.Message,
      idsError.Element, idsError.Detail);
   }
}
SecurityException

The SecurityException class handles Authentication and Authorization errors. The following example code shows how to handle an Authentication or Authorization error:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
try
{
   // Include the service call code here. The following code is for an add operation.
   Customer resultCustomer = service.Add(customer) as Customer;
}

catch (SecurityException exception)
{
   // Include the logic for how you want your app to handle the exception. This code writes a fault messages to a console.
   Console.WriteLine("Message:{0}, ErrorCode:{1}", exception.Message, exception.ErrorCode);
   foreach (IdsError idsError in exception.InnerExceptions)
   {
      Console.WriteLine("ErrorCode:{0}, Message:{1}, Element:{2}, Detail:{3}", idsError.ErrorCode, idsError.Message,
      idsError.Element, idsError.Detail);
   }
}
IdsException

The IdsException class handles Authentication and Authorization errors. The following example code shows how to handle an Authentication or Authorization error:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
try
{
   // Include the service call code here. The following code is for an add operation.
   Customer resultCustomer = service.Add(customer) as Customer;
}

catch (IdsException exception)
{
   // Include the logic for how you want your app to handle the exception. This code writes a fault messages to a console.
   Console.WriteLine("Message:{0}, ErrorCode:{1}", exception.Message, exception.ErrorCode);
   foreach (IdsError idsError in exception.InnerExceptions)
   {
      Console.WriteLine("ErrorCode:{0}, Message:{1}, Element:{2}, Detail:{3}", idsError.ErrorCode, idsError.Message,
      idsError.Element, idsError.Detail);
   }
}