API reference for error handling utilities and exception classes in AdonisJS Core
The errors module provides comprehensive error handling utilities for AdonisJS applications. It aggregates errors from multiple AdonisJS packages and provides utilities for creating and displaying custom exceptions.
import { errors } from '@adonisjs/core'// All error classes are available on the errors objectthrow new errors.E_INVALID_CREDENTIALS('Invalid username or password')
Exception class for invalid argument errors, typically used in function parameter validation.
import { InvalidArgumentsException } from '@adonisjs/core/exceptions'function processUser(userId: string) { if (!userId) { throw new InvalidArgumentsException('User ID is required') }}
import { errors } from '@adonisjs/core'// 401 Unauthorizedthrow new errors.E_UNAUTHORIZED_ACCESS('You must be logged in')// 403 Forbiddenthrow new errors.E_AUTHORIZATION_FAILURE('You do not have permission')// 404 Not Foundthrow new errors.E_ROUTE_NOT_FOUND('Route not found')// 422 Validation Errorthrow new errors.E_VALIDATION_FAILURE('Validation failed')
import { errors } from '@adonisjs/core'// Invalid configurationthrow new errors.E_INVALID_CONFIG('Invalid configuration provided')// Missing environment variablethrow new errors.E_MISSING_ENV_VALUE('DATABASE_URL is required')
import { errors } from '@adonisjs/core'import type { HttpContext } from '@adonisjs/core/http'export default class UsersController { async show({ params, response }: HttpContext) { const user = await User.find(params.id) if (!user) { throw new errors.E_ROUTE_NOT_FOUND('User not found') } return user }}