"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "exceptionFilter", {
    enumerable: true,
    get: function() {
        return exceptionFilter;
    }
});
const _runtimeexceptions = require("../exceptions/runtime.exceptions");
const _serverconstants = require("../server.constants");
const _httpstatuscodesconstants = require("../constants/http-status-codes.constants");
const _typeorm = require("typeorm");
const _faileddependencyexception = require("../exceptions/failed-dependency.exception");
function exceptionFilter(err, req, res, next) {
    const isTest = process.env.NODE_ENV === _serverconstants.AppConstants.defaultTestEnv;
    if (!isTest) {
        console.error("[API Exception Handler]", err.stack || err?.response?.data);
    }
    if (err.isAxiosError) {
        const code = err.response?.status || 500;
        return res.status(code).send({
            error: "External API call failed",
            type: "axios-error",
            data: err.response?.data?._readableState ? null : err.response?.data
        });
    }
    if (err instanceof _runtimeexceptions.AuthenticationError) {
        const code = err.statusCode || 401;
        return res.status(code).send({
            error: err.message,
            reasonCode: err.reasonCode
        });
    }
    if (err instanceof _runtimeexceptions.AuthorizationError) {
        const code = err.statusCode || _httpstatuscodesconstants.HttpStatusCode.FORBIDDEN;
        const permissions = err.permissions;
        const roles = err.roles;
        const error = err.message || "You lack permission to this resource";
        const reason = err.reason;
        return res.status(code).send({
            error,
            reason,
            permissions,
            roles
        });
    }
    if (err instanceof _runtimeexceptions.ForbiddenError) {
        const code = err.statusCode || _httpstatuscodesconstants.HttpStatusCode.FORBIDDEN;
        return res.status(code).send({
            error: err.message
        });
    }
    if (err instanceof _runtimeexceptions.NotFoundException || err instanceof _typeorm.EntityNotFoundError) {
        const code = err.statusCode || 404;
        return res.status(code).send({
            error: err.message
        });
    }
    if (err instanceof _runtimeexceptions.BadRequestException) {
        const code = err.statusCode || 400;
        return res.status(code).send({
            error: err.message
        });
    }
    if (err instanceof _runtimeexceptions.ValidationException) {
        const code = err.statusCode || 400;
        return res.status(code).send({
            error: "API could not accept this input",
            type: err.name,
            errors: err.errors
        });
    }
    if (err instanceof _faileddependencyexception.FailedDependencyException) {
        const code = 424;
        return res.status(code).send({
            error: err.message,
            serviceCode: err.serviceCode,
            type: err.name
        });
    }
    if (err instanceof _runtimeexceptions.InternalServerException) {
        const code = err.statusCode || 500;
        return res.status(code).send({
            error: err.message,
            type: err.name,
            stack: err.stack
        });
    }
    if (err instanceof _runtimeexceptions.ExternalServiceError) {
        const code = err.error.statusCode || 500;
        return res.status(code).send(err.error);
    }
    if (!!err) {
        const code = err.statusCode || 500;
        return res.status(code).send({
            error: "Server experienced an internal error",
            type: err.name,
            stack: err.stack
        });
    }
    next();
}

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