UNPKG

@webxsid/nest-exception

Version:

A centralized exception handling module for NestJS applications. It provides structured error management, logging, and automatic exception handling.

46 lines 1.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AppException = void 0; const common_1 = require("@nestjs/common"); const constant_1 = require("../constant"); class AppException extends common_1.HttpException { constructor(errorOrCode, trace) { super(AppException.createResponse(errorOrCode), AppException.getStatus(errorOrCode)); this.code = AppException.getCode(errorOrCode); this.statusCode = AppException.getStatus(errorOrCode); this.trace = AppException.isDevMode ? (trace ?? new Error().stack) : undefined; } static init(errorRegistryService, isDev) { this.errorRegistryService = errorRegistryService; this.isDevMode = isDev; } static getErrorRegistry() { if (!this.errorRegistryService) { throw new Error('AppException is not initialized. Call AppException.init() before using it.'); } return this.errorRegistryService; } static getError(errorOrCode) { return AppException.getErrorRegistry().getError(errorOrCode); } static getStatus(errorOrCode) { return (AppException.getError(errorOrCode)?.statusCode ?? common_1.HttpStatus.INTERNAL_SERVER_ERROR); } static getCode(errorOrCode) { return AppException.getError(errorOrCode)?.code ?? constant_1.kDefaultErrorCode; } static createResponse(errorOrCode) { const error = AppException.getError(errorOrCode); return { message: error?.message ?? errorOrCode, statusCode: error?.statusCode ?? common_1.HttpStatus.INTERNAL_SERVER_ERROR, code: error?.code ?? constant_1.kDefaultErrorCode, }; } } exports.AppException = AppException; AppException.isDevMode = false; //# sourceMappingURL=app-exception.js.map