Something went wrong. Try again.
Something went wrong. Try again.
2.1 kB · 58 lines
TypeScript
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859import app from '@adonisjs/core/services/app'import { type HttpContext, ExceptionHandler } from '@adonisjs/core/http'import type { StatusPageRange, StatusPageRenderer } from '@adonisjs/core/types/http'import { errors as vineErrors } from '@vinejs/vine'
export default class HttpExceptionHandler extends ExceptionHandler { /** * In debug mode, the exception handler will display verbose errors * with pretty printed stack traces. */ protected debug = !app.inProduction
/** * 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 = app.inProduction
/** * Status pages is a collection of error code range and a callback * to return the HTML contents to send as a response. */ protected statusPages: Record<StatusPageRange, StatusPageRenderer> = { '404': (_, { inertia }) => inertia.render('errors/not_found', {}), '500..599': (_, { inertia }) => inertia.render('errors/server_error', {}), }
/** * The method is used for handling errors and returning * response to the client */ async handle(error: unknown, ctx: HttpContext) { if (error instanceof vineErrors.E_VALIDATION_ERROR) { if (ctx.request.method() === 'GET' && ctx.request.header('x-inertia')) { const page = await ctx.inertia.render('errors/not_found', { handle: ctx.params?.handle, }) return ctx.response.status(404).send(page) } if (!ctx.request.header('x-inertia') && ctx.request.method() === 'GET') { return ctx.response.status(422).send({ errors: error.messages }) } }
return super.handle(error, ctx) }
/** * 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) }}