api/app/exceptions/handler.ts
2025-05-19 13:25:42 +02:00

42 lines
1.3 KiB
TypeScript

import { HttpContext, ExceptionHandler } from '@adonisjs/core/http'
export default class HttpExceptionHandler extends ExceptionHandler {
/**
* In debug mode, the exception handler will display verbose errors
* with pretty printed stack traces.
*/
protected debug = false
/**
* Status pages are used to display a custom HTML pages for certain error
* codes. You might want to enable them in production only, but feel
* free to enable them in development as well.
*/
protected renderStatusPages = false
/**
* The method is used for handling errors and returning
* response to the client
*/
async handle(error: any, ctx: HttpContext) {
const statusCode = error.status || error.statusCode || 500
const message = error.message || 'Internal Server Error'
const stack = this.debug ? error.stack : undefined
const response = {
status: statusCode,
error: message,
stack,
}
return ctx.response.status(statusCode).json(response)
}
/**
* The method is used to report error to the logging service or
* the a third party error monitoring service.
*
* @note You should not attempt to send a response from this method.
*/
async report(error: unknown, ctx: HttpContext) {
return super.report(error, ctx)
}
}