Skip to main content
version 1.0.0

Error Handling

Introduction

When you start a new Athenna project, error and exception handling is already configured for you. The app/Http/Exceptions/Handler class is where all exceptions thrown by your http server are logged and then rendered to the user. And the app/Console/Exceptions/Handler is where all exceptions thrown by your Artisan commands. We'll dive deeper into both classes throughout this documentation.

Configuration

The debug option in your config/app.js configuration file determines how much information about an error is actually displayed to the user. By default, this option is set to respect the value of the APP_DEBUG environment variable, which is stored in your .env file.

During local development, you should set the APP_DEBUG environment variable to true. In your production environment, this value should always be false. If the value is set to true in production, you risk exposing sensitive configuration values to your application's end users.

Every error will be logged by default using the console driver from @athenna/logger. By default, Athenna uses the exception channel of your config/logging.js file to log all exceptions that happens in your application.

tip

You can change the driver and formatter of exception channel inside config/logging.js file. This way you can send all your errors to other driver and using a different formatter.

The cli exception handler

Athenna will forward all exceptions occurred during a command execution to the handle method of app/Console/Exceptions/Handler.js class. By default, Athenna forward the error to the handle method of ConsoleExceptionHandler from Artisan using super.

/**
* The global exception handler of all Artisan commands.
*
* @param {any} error
*/
async handle(error) {
return super.handle(error)
}

The http exception handler

Athenna will forward all exceptions occurred during an HTTP request execution to the handle method of app/Http/Exceptions/Handler.js class. By default, Athenna forward the error to the handle method of HttpExceptionHandler from Http using super.

/**
* The global exception handler of all Http requests.
*
* @param {any} error
*/
async handle(error) {
return super.handle(error)
}

You can change all the behavior of the handle method according to your business rules, but we recommend using the handle method from HttpExceptionHandler to validate the APP_DEBUG environment variable and configure a pretty error log for you.

Ignoring logs

You can simply ignore the exceptions from being logged in your exception handler using the ignoreCodes and ignoreStatuses properties of your app/Http/Exceptions/Handler.js class.

/**
* Error codes that should be ignored by Log.
*
* @type {string[]}
*/
get ignoreCodes() {
return ['E_RUNTIME']
}

/**
* Error statuses that should be ignored by Log.
*
* @type {number[]}
*/
get ignoreStatuses() {
return [400, 401, 403]
}

Custom exceptions

You can create custom exceptions by executing the following Artisan command.

node artisan make:exception NotFoundException

Next, import and raise the exception as follows.

import { NotFoundException } from '#app/Exceptions/NotFoundException'

const message = 'Your resource has not been found.'
const status = 404
const code = 'E_NOT_FOUND'
const help = 'Restart your computer. Always works.'

throw new NotFoundException(message, status, code, help)
tip

Always try to use a custom exception to throw your errors inside Athenna. If you use Error, TypeError and other classes, it will not be treated by the handlers and will always be considered as a not treated exception.