"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const exceptions_1 = require("../exceptions");
const logger_1 = __importDefault(require("../logger"));
const errorHandler = (err, req, res, next) => {
    if (err instanceof exceptions_1.BaseException) {
        logger_1.default.debug(err);
        res.status(err.status);
        const payload = {
            error: {
                code: err.code,
                message: err.message,
            },
        };
        if (process.env.NODE_ENV === 'development') {
            payload.error.stack = err.stack;
        }
        return res.json(payload);
    }
    else {
        logger_1.default.error(err);
        res.status(500);
        const payload = {
            error: {
                code: 'INTERNAL_SERVER_ERROR',
                message: err.message,
            },
        };
        if (process.env.NODE_ENV === 'development') {
            payload.error.stack = err.stack;
        }
        return res.json(payload);
    }
};
exports.default = errorHandler;
