Implement Error handling
When implementing domain logic, there are three types of errors which can happen and need to be handled:
- Business errors which can occur if business conditions are not met, but are expected to happen
- Validation errors
- General errors (all others)
Throw business errors
For every modelled business error, the SDK offers functionality to throw it.
Within the implementation file of a service, command or agent the following code can be used to throw a business error:
throw this.factory.error.nsacrnm.MyBusinessError();
Every error thrown in an implementation file should be handled latest in the errorHandler method of the API operation.
Handle errors
In the implementation file of a service, command, agent and operation error instance check can be done by using isInstanceOf functionality, thus enable to code handling logic for different error instances.
Handling business errors
To safely handle a business error that was modelled in the Solution Designer use the following code:
try {
  // call a service which might throw a business error
  await this.services.nsacrnm.ServiceIdentifier(input);
} catch (e) {
  // check whether the error matches any of the errors modelled in Solution Designer      
  if (this.isInstanceOf.businessError.ns1.NoBalanceAvailable(e)) {
    // now can handle this specific business error
  }
  if (this.isInstanceOf.error.ValidationError(e)) {
    // now can handle this validation error
  }
  if (this.isInstanceOf.businessError.ns2.NoCustomerFound(e)) {
    // now can handle this specific business error
  }
}Handling general errors
These are the general errors that can occur. To safely handle an error, use the following code:
try {
  // execute some logic
} catch (e) {
  // check whether the validation of an entity (used as input or instance) fails
  if (this.isInstanceOf.error.ValidationError(e)) {
    // now can handle this validation error
  }
  
  // check for a child of abstract BusinessError
  if (this.isInstanceOf.error.BusinessError(e)) {
    // now can handle this Business error
  }
  // check whether an  action (e.g. a command) is not currently available
  if (this.isInstanceOf.error.ActionNotAvailableError(e)) {
    // now can handle this ActionNotAvailable error
  }
  // check whether an aggregate is found in the datastore
  if (this.isInstanceOf.error.AggregateNotFound(e)) {
    // now can handle this AggregateNotFound error
  }
  // check whether an external request (i.e. this.util.request) caused an error
  if (this.isInstanceOf.error.ExternalRequestError(e)) {
    // now can handle this ExternalRequest error
  }
  
  // check whether an internal request caused an error
  if (this.isInstanceOf.error.InternalRequestError(e)) {
    // now can handle this InternalRequest error
  }
  // check for GeneralError
  if (this.isInstanceOf.error.GeneralError(e)) {
    // now can handle this general error
  }
}The hierarchy of error consists of GeneralError, ValidationError and BusinessError, where
ValidationError and BusinessError extend GeneralError.