Error handling examples
IBM Industry Solutions Workbench offers the handling of these main types of errors:
Business errors which can occur if business conditions are not met but are expected to happen.
Validation errors.
General errors (all others).
The error handling logic is usually implemented within the API operation class.
Throw business errors
Inside a domain namespace a business error can be thrown whenever a domain service or a command can/should not be executed due to conditions that are not fulfilled, e.g., minimumAmountOrdered should be 5 but customer inputs 4.
You can create as many business errors as needed.
Within the implementation file of a domain service or a command you can paste the following code to throw a business error:
throw this.factory.error.nsacrnm.MyBusinessError();
where nsacronym
must get replaced with the acronym of the domain namespace in which you created the business error
and MyBusinessError
gets replaced with the actual name of the business error you want to throw (and have defined
previously in the design model).
Every error thrown in an implementation file should be handled latest in the errorHandler
method of the API operation.
API Operation error handling
Operation xyzErrorHandler()
method can be used to implement error handling logic for the errors that might occur within an
API operation's execution.
The xyzErrorHandler()
method is executed automatically if an error occurs and it gets the error that occurred as an argument.
The method will have access to the responses modelled in the operation. The passed error can then be checked using the isInstanceOf
utility and the operation response (both status code and body) can be set accordingly.
See below example (v1
is the name of the API Namespace and getPizza
is the name of the API operation):
/**
* GetPizzaApi
*/
export class GetPizzaApi extends V1BaseControllers.GetPizzaApiBase {
constructor(requestContext: RequestContext) {
super(requestContext);
}
/**
* @param request Request.GetPizzaRequest
* getPizza logic
*/
getPizza = async (request: Request.GetPizzaRequest) => {
// TODO: add your implementation logic
this.util.log.info('start getPizza execution');
// this.response.statusCode = 200;
}
/**
* @param request Request.GetPizzaRequest
* @param error TypeError
* getPizzaErrorHandler logic
*/
getPizzaErrorHandler = async (error: TypeError, request: Request.GetPizzaRequest) => {
this.util.log.info('start getPizzaErrorHandler execution');
// Add Error handling logic below and set this.response that will be returned as operation v1_getPizza response
if (this.isInstanceOf.error.GeneralError(error)) {
this.response.status = 500;
this.response.body = {
errorCode: 'GE101',
errorMessage: 'General Error occurred, original Error message: ' + error.message
};
}
if (this.isInstanceOf.error.ValidationError(error)) {
this.response.status = 400;
this.response.body = {
errorCode: 'VE101',
errorMessage: 'Validation Error occurred, original Error message: ' + error.message
};
}
if (this.isInstanceOf.error.AggregateNotFoundError(error)) {
this.response.status = 404;
this.response.body = {
errorCode: 'AG101',
errorMessage: 'Aggregate cannot be found: ' + error.message
};
}
if (this.isInstanceOf.businessError.xyzDomainNamespace.BusinessError(error)) {
this.response.status = 400;
this.response.body = {
errorCode: 'BE101',
errorMessage: 'Business Error occurred, original Error message: ' + error.message
};
}
}
}
If you need to check for other error types (e.g., validation errors, etc.) please see TypeScript Solution Framework (SDK) - isInstanceOf checks for further details.