"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
    if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
var HttpExceptionFilter_1;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpExceptionFilter = void 0;
const common_1 = require("@nestjs/common");
const logging_interceptor_1 = require("../interceptors/logging.interceptor");
const error_mapper_service_1 = require("../helpers/error-mapper.service");
let HttpExceptionFilter = HttpExceptionFilter_1 = class HttpExceptionFilter {
    constructor(errorMapper) {
        this.errorMapper = errorMapper;
        this.logger = new common_1.Logger(HttpExceptionFilter_1.name);
        this.logger.debug('HttpExceptionFilter initialized');
    }
    catch(exception, host) {
        const ctx = host.switchToHttp();
        const response = ctx.getResponse();
        const request = ctx.getRequest();
        const isHttp = exception instanceof common_1.HttpException;
        const explicitStatus = isHttp ? exception.getStatus() : undefined;
        const code = this.errorMapper.mapException(exception);
        const defaultStatus = this.errorMapper.getHttpStatus(code);
        const message = code === 'solidx-unknown-error' ? `${exception?.message}` : this.errorMapper.getMessage(code);
        const status = explicitStatus ?? defaultStatus ?? 500;
        this.logger.error(`[${status} ${logging_interceptor_1.HttpStatusCodeMessages[status] || 'Internal Server Error'}] ${request?.method} ${request?.url} - ${exception?.message || message} [code=${code}]`);
        if (exception?.stack) {
            this.logger.error(exception.stack);
        }
        const extra = (isHttp && exception.getResponse?.()) ??
            exception?.response ??
            {};
        response.status(status).json({
            statusCode: status,
            statusCodeMessage: logging_interceptor_1.HttpStatusCodeMessages[status] || 'Internal Server Error',
            message: message,
            errorCode: code,
            error: message,
            data: extra,
        });
    }
};
exports.HttpExceptionFilter = HttpExceptionFilter;
exports.HttpExceptionFilter = HttpExceptionFilter = HttpExceptionFilter_1 = __decorate([
    (0, common_1.Catch)(),
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [error_mapper_service_1.ErrorMapperService])
], HttpExceptionFilter);
//# sourceMappingURL=http-exception.filter.js.map