2023-03-03 15:54:28 +00:00
|
|
|
/*
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
| Http Exception Handler
|
|
|
|
|--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
| AdonisJs will forward all exceptions occurred during an HTTP request to
|
|
|
|
| the following class. You can learn more about exception handling by
|
|
|
|
| reading docs.
|
|
|
|
|
|
|
|
|
| The exception handler extends a base `HttpExceptionHandler` which is not
|
|
|
|
| mandatory, however it can do lot of heavy lifting to handle the errors
|
|
|
|
| properly.
|
|
|
|
|
|
|
|
|
*/
|
2024-03-14 19:25:27 +00:00
|
|
|
import app from '@adonisjs/core/services/app';
|
|
|
|
import { HttpContext, ExceptionHandler } from '@adonisjs/core/http';
|
|
|
|
// import logger from '@adonisjs/core/services/logger';
|
|
|
|
import type { StatusPageRange, StatusPageRenderer } from '@adonisjs/core/types/http';
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
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;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
2024-04-29 09:25:50 +00:00
|
|
|
protected renderStatusPages = true; //app.inProduction;
|
2024-03-14 19:25:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Status pages is a collection of error code range and a callback
|
|
|
|
* to return the HTML contents to send as a response.
|
|
|
|
*/
|
2024-04-23 17:36:45 +00:00
|
|
|
// protected statusPages: Record<StatusPageRange, StatusPageRenderer> = {
|
|
|
|
// '401..403': (error, { view }) => {
|
|
|
|
// return view.render('./errors/unauthorized', { error });
|
|
|
|
// },
|
|
|
|
// '404': (error, { view }) => {
|
|
|
|
// return view.render('./errors/not-found', { error });
|
|
|
|
// },
|
|
|
|
// '500..599': (error, { view }) => {
|
|
|
|
// return view.render('./errors/server-error', { error });
|
|
|
|
// },
|
|
|
|
// };
|
2024-03-14 19:25:27 +00:00
|
|
|
protected statusPages: Record<StatusPageRange, StatusPageRenderer> = {
|
2024-04-23 17:36:45 +00:00
|
|
|
'404': (error, { inertia }) => {
|
|
|
|
return inertia.render('Errors/ServerError', {
|
|
|
|
error: error.message,
|
|
|
|
code: error.status,
|
|
|
|
});
|
2024-03-14 19:25:27 +00:00
|
|
|
},
|
2024-04-23 17:36:45 +00:00
|
|
|
'401..403': async (error, { inertia }) => {
|
|
|
|
// session.flash('errors', error.message);
|
|
|
|
return inertia.render('Errors/ServerError', {
|
|
|
|
error: error.message,
|
|
|
|
code: error.status,
|
|
|
|
});
|
2024-03-14 19:25:27 +00:00
|
|
|
},
|
2024-04-23 17:36:45 +00:00
|
|
|
'500..599': (error, { inertia }) => inertia.render('Errors/ServerError', { error: error.message, code: error.status }),
|
2023-06-22 15:20:04 +00:00
|
|
|
};
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
// constructor() {
|
|
|
|
// super(logger);
|
|
|
|
// }
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async handle(error: any, ctx: HttpContext) {
|
2024-04-29 09:25:50 +00:00
|
|
|
const { response, request, session, inertia } = ctx;
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
/**
|
|
|
|
* Handle failed authentication attempt
|
|
|
|
*/
|
|
|
|
// if (['E_INVALID_AUTH_PASSWORD', 'E_INVALID_AUTH_UID'].includes(error.code)) {
|
|
|
|
// session.flash('errors', { login: error.message });
|
|
|
|
// return response.redirect('/login');
|
|
|
|
// }
|
|
|
|
// if ([401].includes(error.status)) {
|
|
|
|
// session.flash('errors', { login: error.message });
|
|
|
|
// return response.redirect('/dashboard');
|
|
|
|
// }
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// https://github.com/inertiajs/inertia-laravel/issues/56
|
2024-03-14 19:25:27 +00:00
|
|
|
// let test = response.getStatus(); //200
|
|
|
|
// let header = request.header('X-Inertia'); // true
|
2024-04-23 17:36:45 +00:00
|
|
|
// if (request.header('X-Inertia') && [500, 503, 404, 403, 401, 200].includes(response.getStatus())) {
|
2024-04-29 09:25:50 +00:00
|
|
|
if (request.header('X-Inertia') && [422].includes(error.status)) {
|
|
|
|
// session.flash('errors', error.messages.errors);
|
|
|
|
session.flash('errors', error.messages);
|
|
|
|
return response.redirect().back();
|
|
|
|
// return inertia.render('errors/server_error', {
|
|
|
|
// return inertia.render('errors/server_error', {
|
|
|
|
// // status: response.getStatus(),
|
|
|
|
// error: error,
|
|
|
|
// });
|
|
|
|
// ->toResponse($request)
|
|
|
|
// ->setStatusCode($response->status());
|
|
|
|
}
|
2024-04-23 17:36:45 +00:00
|
|
|
// Dynamically change the error templates based on the absence of X-Inertia header
|
|
|
|
// if (!ctx.request.header('X-Inertia')) {
|
|
|
|
// this.statusPages = {
|
|
|
|
// '401..403': (error, { view }) => view.render('./errors/unauthorized', { error }),
|
|
|
|
// '404': (error, { view }) => view.render('./errors/not-found', { error }),
|
|
|
|
// '500..599': (error, { view }) => view.render('./errors/server-error', { error }),
|
|
|
|
// };
|
|
|
|
// }
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
/**
|
|
|
|
* Forward rest of the exceptions to the parent class
|
|
|
|
*/
|
|
|
|
return super.handle(error, ctx);
|
|
|
|
}
|
2024-03-14 19:25:27 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
}
|