Exception handling examples

BadInputException exception example

The following sample code illustrates a class method that throws a bad input exception.

function startBatchProcess(body: JsonObject): Long {
  var processType = body.get("processType") as String
  var batchProcessNames = getDelegate().getValidBatchProcessTypes()
  if (processType == null || !batchProcessNames.contains(processType)) {
    throw new BadInputException("Bad process type " + processType)
  }
  .....
}

ConflictingResourceException exception example

The following sample code illustrates a class method that handles a conflicting resource exception.

function createContact(body : gw.pc.contact.v1_0.Contact, updateAB : Boolean) : gw.pc.contact.v1_0.Contact {
  if(contactAlreadyExists(body)){
    throw new ConflictingResourceException("Contact already exists", null, null)
  }
  ...
}
 
private function contactAlreadyExists(contactJson : gw.pc.contact.v1_0.Contact) : Boolean {
  // Business logic to look up contact
  ...
}
 
private class ConflictingResourceException extends RestExceptionWithErrorInfo {
  protected construct(userMessage: String, developerMessage: String, details: List<RequestErrorDetails>) {
    super(409, userMessage, developerMessage, details)
  }
}
Notice the following:
  • Method createContact throws a ConflictingResourceException exception if the contact to add already exists.
  • Private class ConflictingResourceException defines the message processing logic.