Skip to content

Error Handling

Kosala Tennakoon edited this page Dec 1, 2019 · 2 revisions

Package /app/error

Overview

Catalyst uses a global error handler to deal with errors in a consistent manner. The global error handler is invoked in the Controller layer. You will have to bubble the error up to the controller layer from below layers.

Error handling logic usually looks like this in the controller.

samples, err := ctl.sampleUseCase.Get(ctx)
if err != nil {
    response.Error(ctx, w, err, ctl.container.Adapters.Log)
    return
}

NOTE: The error handler is called through the /app/transport/response package so that we have a single point to deal with both normal and error responses.

Error Types

By default Catalyst has following error types.

In base error package (/app/error)

  • AdapterError
  • MiddlewareError
  • RepositoryError
  • ServerError
  • ServiceError
  • ValidationError

In domain (/domain)

  • DomainError

NOTE: Domain errors exists outside of the base error package. But the error handler is configured to handle them. If you need to handle more error types you will have to tweak the error handler.

In order to handle the error properly by the handler the error message has to follow the following pattern.

    "message|code|type|details"
  • message: Error message
  • code: Error code
  • type: Error type
  • details: additional error details

The pipe character (|) is the delimiter for the error message.

NOTE: The only instance this differs is with validation errors which are handled in a slightly different manner.

Error Formats

Errors have one of two formats depending on its type.

Generic Errors

{
    "errors": {
        "type": "Error Type",
        "code": 0,
        "message": "",
        "trace": ""
    }
}

Validation Errors

{
    "errors": {
        "type": "Validation Errors",
        "trace": {
            "fieldName": "validation error"
        }
    }
}

Clone this wiki locally