Exception handling

The QuickBooks Online Java SDK uses the fault types supported by QuickBooks Online APIs to return exceptions in the Intuit web responses. FMSException is the super class of all exceptions defined in the SDK. The SDK throws the instances of specific exceptions, which you can catch through FMSException. Then, depending on the instance of the exception class, you can implement a logic to handle it.

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 the appropriate IDS exception. For example, the SDK returns “InvalidTokenExeption” for a 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, the SDK returns an exception based on the fault type.
Exceptions supported by the SDK

Exceptions thrown by the SDK in response to the presence of a Fault element in IntuitResponse:

Exception For Fault Type
ValidationException Validation
ServiceException Service
AuthenticationException Authentication
AuthorizationException Authorization

Exceptions thrown by the SDK in response to an HTTP response status code other than 200 OK:

Exception For Status Code
BadRequestException 400
InvalidTokenException 401
InvalidRequestException 403 and 404
InternalServiceException 500
ServiceUnavailableException 503

Exceptions thrown by the SDK in response to a fault within the SDK:

Exception Explanation
CompressionException When the SDK detects a fault in the data compression process.
ConfigurationException When the SDK detects invalid content within the configuration file.
SerializationException When the SDK detects a fault in the serialization or de-serialization process.

Following is sample code that shows how to implement exception handling while calling a QuickBooks Online API endpoint. For instructions on creating a DataService object, see Data Service APIs.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
try {
   service.findAll(customer);
} catch (FMSException e) {
   if (e instanceof AuthenticationException){
      System.out.println("Authentication Exception occurred");
      //Perform required operation when this exception occurs
   } else if (e instanceof ServiceException){
      System.out.println("Service Exception occurred");
      //Perform required operation when this exception occurs
   } else if (e instanceof AuthenticationException){
      System.out.println("Authenticatin Exception occurred");
      //Perform required operation when this exception occurs
   } else if (e instanceof AuthorizationException){
      System.out.println("Authorization Exception occurred");
      //Perform required operation when this exception occurs
   } else {
      System.out.println("Some other Exception in SDK or network happened");
      //Perform required operation when this exception occurs
   }
}