"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get ExceptionFilter () {
        return ExceptionFilter;
    },
    get exceptionFilter () {
        return exceptionFilter;
    }
});
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
const _serverconstants = require("../server.constants");
const _typeorm = require("typeorm");
const _faileddependencyexception = require("../exceptions/failed-dependency.exception");
class ExceptionFilter {
    logger;
    constructor(loggerFactory){
        this.logger = loggerFactory(ExceptionFilter.name);
    }
    handle(err, _req, res, next) {
        const isTest = process.env.NODE_ENV === _serverconstants.AppConstants.defaultTestEnv;
        if (!isTest) {
            this.logger.error("API Exception occurred", {
                message: err.message,
                stack: err.stack || err?.response?.data,
                url: _req?.url,
                method: _req?.method,
                userAgent: _req?.headers?.['user-agent'],
                errorType: err.constructor.name
            });
        }
        if (err.isAxiosError) {
            const code = err.response?.status || 500;
            res.status(code).send({
                error: "External API call failed",
                type: "axios-error",
                data: err.response?.data?._readableState ? null : err.response?.data
            });
            return;
        }
        if (err instanceof _runtimeexceptions.AuthenticationError) {
            res.status(401).send({
                error: err.message,
                reasonCode: err.reasonCode
            });
            return;
        }
        if (err instanceof _runtimeexceptions.AuthorizationError) {
            const permissions = err.permissions;
            const roles = err.roles;
            const error = err.message || "You lack permission to this resource";
            const reason = err.reason;
            res.status(403).send({
                error,
                reason,
                permissions,
                roles
            });
            return;
        }
        if (err instanceof _runtimeexceptions.ForbiddenError) {
            res.status(403).send({
                error: err.message
            });
            return;
        }
        if (err instanceof _runtimeexceptions.NotFoundException || err instanceof _typeorm.EntityNotFoundError) {
            res.status(404).send({
                error: err.message
            });
            return;
        }
        if (err instanceof _runtimeexceptions.BadRequestException) {
            res.status(400).send({
                error: err.message
            });
            return;
        }
        if (err instanceof _runtimeexceptions.ValidationException) {
            res.status(400).send({
                error: "API could not accept this input",
                type: err.name,
                errors: err.errors
            });
            return;
        }
        if (err instanceof _faileddependencyexception.FailedDependencyException) {
            res.status(424).send({
                error: err.message,
                serviceCode: err.serviceCode,
                type: err.name
            });
            return;
        }
        if (err instanceof _runtimeexceptions.InternalServerException) {
            res.status(500).send({
                error: err.message,
                type: err.name,
                stack: err.stack
            });
            return;
        }
        if (err instanceof _runtimeexceptions.ExternalServiceError) {
            res.status(500).send(err.error);
            return;
        }
        if (err) {
            res.status(500).send({
                error: "Server experienced an internal error",
                type: err.name,
                stack: err.stack
            });
            return;
        }
        next();
    }
}
function exceptionFilter(_err, _req, _res, _next) {
    throw new Error("Use ExceptionFilter class instead of exceptionFilter function. Please inject ExceptionFilter and use .handle method.");
}

//# sourceMappingURL=exception.filter.js.map