Error Handling Examples

Error handling logic can be implemented in two places:

  • API Operation class within its handleError() method.
  • API ErrorMiddleware class within its handleError() method.

ErrorMiddleware

ErrorMiddleware provides a centralized place for common error handling logic independent from API operation context.

Path of this file will be src-impl/api/API-namespace-prefix/middleware/ErrorMiddleware.

Within ErrorMiddleware handleError() general error handling logic can be implemented by checking the passed operation execution error that occurred and setting response accordingly.

ErrorMiddleware handleError() method will be executed only if API Operation handleError() method is not implemented / did not set a proper response.

If ErrorMiddleware handleError() is not implemented / did not set a proper API operation response, then original error will be returned as API operation response with a 400 / 500 status code depending on error type (System Error / Business Error / Validation Error).

Attention: API operation response set within this class via (this.response) might end up being undocumented API response for the executed operation. Please make sure the response status code and schema you set have been modeled and added to all of the API namespace operations.
Attention: The class hierarchy of errors consists of GeneralError, ValidationError and BusinessError, Where ValidationError and BusinessError extend GeneralError.
See below Example:

/**
* This class is responsible for providing a centralized place for common 
* error handling logic independent from API operation context, 
* API operation response set within this class via (this.response) 
* might end up being not documented API response for the executed operation.
*/
export default class extends middleware.apitest_ErrorMiddleware {

  // This script gets executed if any error happens throughout the operation execution
  public async handleError(error: Error): Promise<void> {
    // Handle errors that may occurs throughout operation execution
      if (this.isInstanceOf.error.BusinessError(error)) {

            this.response = new namespace_SomeOperationResponse();
            this.response.status = 400;
            this.response.body = this.factory.schema.v1.BadRequestError();
            this.response.body.message = 'BadRequestError';

        } else {

            this.response = new namespace_AnotherOperationResponse();
            this.response.status = 500;
            this.response.body = this.factory.schema.v1.SystemError();
            this.response.body.message = 'SystemError';
      }

  }
}

Operation Error Handling

Operation handleError() method can be used to implement error handling logic for the errors that might occurs within operation execution.

The handleError() method is executed automatically if an error occurs, it gets the error that occurred as an argument.

The handleError() method will have access to operation modeled responses.

The passed error can then be checked using isInstanceOf utility and operation response can be set accordingly.

If handleError() method on operation level is not implemented / did not set a proper response, then ErrorMiddleware handleError() will be called.

See below example:

export default class extends operations.ehns_getPizza {

  public async execute(): Promise<void> {
    const log = this.util.log;
    log.debug('ehns_getPizza.execute()');
    // Execute some logic below that might result in an error
  }

  /**
  * This function is automatically called when an error occurs within the execution flow
  * of this operation ehns_getPizza @param error Operation execution error that occurred
  */
  public async handleError(error: Error): Promise<void> {
    const log = this.util.log;
    log.info('ehns_getPizza.handleError()');
    // Add Error handling logic below and set this.response that will be returned as operation ehns_getPizza response

    if (this.isInstanceOf.error.GeneralError(error)) {
      this.response.status = 500;
      this.response.body = this.factory.schema.ehns.GeneralErrorSchema();
      this.response.body.errorCode = 'GE101';
      this.response.body.errorMessage = 'General Error occurred, original Error message: '
      + error.message;
    }

    if (this.isInstanceOf.error.ValidationError(error)) {
      this.response.status = 400;
      this.response.body = this.factory.schema.ehns.GeneralErrorSchema();
      this.response.body.errorCode = 'VE101';
      this.response.body.errorMessage = 'Validation Error occurred, original Error message: '
      + error.message;
    }

    if (this.isInstanceOf.error.BusinessError(error)) {
      this.response.status = 400;
      this.response.body = this.factory.schema.ehns.GeneralErrorSchema();
      this.response.body.errorCode = 'BE101';
      this.response.body.errorMessage = 'Business Error occurred, original Error message: ' 
      + error.errorMessage;
    }
  }
}