- update to AdonisJS 6

This commit is contained in:
Kaimbacher 2024-03-14 20:25:27 +01:00
parent f828ca4491
commit cb51a4136f
167 changed files with 21485 additions and 21212 deletions

View file

@ -12,24 +12,53 @@
| properly.
|
*/
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';
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext';
import Logger from '@ioc:Adonis/Core/Logger';
import HttpExceptionHandler from '@ioc:Adonis/Core/HttpExceptionHandler';
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;
export default class ExceptionHandler extends HttpExceptionHandler {
protected statusPages = {
'401,403': 'errors/unauthorized',
'404': 'errors/not-found',
'500..599': 'errors/server-error',
/**
* 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 = true;
// protected statusPages = {
// '401,403': 'errors/unauthorized',
// '404': 'errors/not-found',
// '500..599': 'errors/server-error',
// };
/**
* 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> = {
'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 });
},
};
constructor() {
super(Logger);
}
// constructor() {
// super(logger);
// }
public async handle(error: any, ctx: HttpContextContract) {
const { response, request, inertia } = ctx;
public async handle(error: any, ctx: HttpContext) {
const { response, request, session } = ctx;
/**
* Handle failed authentication attempt
@ -44,11 +73,16 @@ export default class ExceptionHandler extends HttpExceptionHandler {
// }
// https://github.com/inertiajs/inertia-laravel/issues/56
if (request.header('X-Inertia') && [500, 503, 404, 403, 401].includes(response.getStatus())) {
return inertia.render('Error', {
status: response.getStatus(),
message: error.message,
});
// let test = response.getStatus(); //200
// let header = request.header('X-Inertia'); // true
if (request.header('X-Inertia') && [500, 503, 404, 403, 401, 200].includes(response.getStatus())) {
// session.flash('errors', error.messages.errors);
session.flash('errors', error.messages);
return response.redirect().back();
// return inertia.render('Error', {
// status: response.getStatus(),
// message: error.message,
// });
// ->toResponse($request)
// ->setStatusCode($response->status());
}
@ -58,4 +92,14 @@ export default class ExceptionHandler extends HttpExceptionHandler {
*/
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);
}
}

View file

@ -1,5 +1,5 @@
import { StatusCodes } from 'http-status-codes';
import HTTPException from './HttpException';
import HTTPException from './HttpException.js';
class InternalServerErrorException extends HTTPException {
constructor(message?: string) {

View file

@ -1,4 +1,5 @@
import { Exception } from '@adonisjs/core/build/standalone';
import { Exception } from "@adonisjs/core/exceptions";
import { HttpContext } from "@adonisjs/core/http";
/*
|--------------------------------------------------------------------------
@ -23,21 +24,21 @@ export default class InvalidCredentialException extends Exception {
* Unable to find user
*/
public static invalidUid() {
const error = new this('User not found', 400, 'E_INVALID_AUTH_UID');
const error = new this('User not found', {status: 400, code: 'E_INVALID_AUTH_UID'});
return error;
}
/**
* Invalid user password
*/
public static invalidPassword() {
const error = new this('Password mis-match', 400, 'E_INVALID_AUTH_PASSWORD');
const error = new this('Password mis-match', {status: 400, code: 'E_INVALID_AUTH_PASSWORD'});
return error;
}
/**
* Flash error message and redirect the user back
*/
private respondWithRedirect(error, ctx) {
private respondWithRedirect(error: any, ctx: HttpContext) {
// if (!ctx.session) {
// return ctx.response.status(this.status).send(this.responseText);
// }
@ -59,7 +60,7 @@ export default class InvalidCredentialException extends Exception {
* Handle this exception by itself
*/
public handle(error, ctx) {
public handle(error: any, ctx: HttpContext) {
// return response.status(403).view.render("errors/unauthorized", {
// error: error,
// });

View file

@ -1,6 +1,6 @@
import { StatusCodes } from 'http-status-codes';
// import HTTPException from './HttpException';
import { OaiErrorCodes } from './OaiErrorCodes';
import { OaiErrorCodes } from './OaiErrorCodes.js';
export class ErrorCode {
public static readonly Unauthenticated = 'Unauthenticated';