import { errors as authErrors } from '@adonisjs/auth';
import { errors as coreErrors } from '@adonisjs/core';
import { ExceptionHandler } from '@adonisjs/core/http';
import app from '@adonisjs/core/services/app';
import { Sentry } from '@rlanz/sentry';
import { errors as vineErrors } from '@vinejs/vine';
import { AKAPIException } from './api_exception.js';
export const PG_CODE_UNIQUENESS_VIOLATION = '23505';
export const PG_CODE_NOT_NULL_VIOLATION = '23502';
export default class AKHttpExceptionHandler extends ExceptionHandler {
    debug = !app.inProduction;
    resolve(error, ctx) {
        const apiException = ctx.auth?.use()?.authenticateApiException;
        if (apiException)
            return apiException;
        if (error instanceof vineErrors.E_VALIDATION_ERROR)
            return AKAPIException.validationError(error);
        if (error instanceof coreErrors.E_ROUTE_NOT_FOUND)
            return AKAPIException.notFound;
        if (error instanceof authErrors.E_UNAUTHORIZED_ACCESS)
            return AKAPIException.auth.notAuhthentified;
        if (error.code === PG_CODE_UNIQUENESS_VIOLATION)
            return AKAPIException.alreadyExist;
        if (error.code === PG_CODE_NOT_NULL_VIOLATION)
            return AKAPIException.validationError({ messages: [error.message] });
        else
            return error;
    }
    async handle(error, ctx) {
        const resolvedError = this.resolve(error, ctx);
        return super.handle(resolvedError, ctx);
    }
    async report(error, ctx) {
        const resolvedError = this.resolve(error, ctx);
        if (!(resolvedError instanceof AKAPIException)) {
            Sentry.captureException(error);
        }
        return super.report(resolvedError, ctx);
    }
}
//# sourceMappingURL=http_exception_handler.js.map