nestjs-mapped-exception
Version:
Mapped Exceptions for NestJS
71 lines • 3.07 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DEFAULT_EXCEPTIONS = void 0;
const common_1 = require("@nestjs/common");
const mapped_exception_error_class_1 = require("./mapped-exception-error.class");
const mapped_exceptions_item_class_1 = require("./types/mapped-exceptions-item.class");
const prepareCode = (code, suffix) => {
return `${globalThis.EXCEPTION_ERROR_PREFIX || 'ERR'}${code.toString().padStart(4, '0')}${suffix}`;
};
const DATABASE = {
DEFAULT: new mapped_exceptions_item_class_1.MappedExceptionItem({
message: 'There was an database error',
code: prepareCode(1, 'DTB'),
statusCode: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
throw: () => {
throw new mapped_exception_error_class_1.MappedExceptionError(DATABASE.DEFAULT);
},
}),
};
const VALIDATION = {
DEFAULT: new mapped_exceptions_item_class_1.MappedExceptionItem({
message: 'There was an validation error',
code: prepareCode(1, 'VAL'),
statusCode: common_1.HttpStatus.BAD_REQUEST,
throw: () => {
throw new mapped_exception_error_class_1.MappedExceptionError(VALIDATION.DEFAULT);
},
}),
INVALID_TYPE: new mapped_exceptions_item_class_1.MappedExceptionItem({
message: 'The variable type is invalid',
code: prepareCode(2, 'VAL'),
statusCode: common_1.HttpStatus.BAD_REQUEST,
throw: () => {
throw new mapped_exception_error_class_1.MappedExceptionError(VALIDATION.INVALID_TYPE);
},
}),
};
const OPERATION = {
DEFAULT: new mapped_exceptions_item_class_1.MappedExceptionItem({
message: 'There was an error',
code: prepareCode(1, 'OPE'),
statusCode: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
throw: () => {
throw new mapped_exception_error_class_1.MappedExceptionError(OPERATION.DEFAULT);
},
}),
};
const DEFAULT = new mapped_exceptions_item_class_1.MappedExceptionItem({
message: 'There was an unknown error',
code: prepareCode(1, 'DFT'),
statusCode: common_1.HttpStatus.INTERNAL_SERVER_ERROR,
throw: () => {
throw new mapped_exception_error_class_1.MappedExceptionError(DEFAULT);
},
});
const custom = (exception, code) => {
if (exception instanceof mapped_exceptions_item_class_1.MappedExceptionItem) {
const exceptionItem = Object.assign(Object.assign({}, exception), { code: prepareCode(code, 'CST') });
return new mapped_exceptions_item_class_1.MappedExceptionItem(Object.assign(Object.assign({}, DEFAULT), exceptionItem));
}
const customExceptionItem = new mapped_exceptions_item_class_1.MappedExceptionItem(Object.assign(Object.assign({}, DEFAULT), { message: exception, code: prepareCode(code, 'CST') }));
return new mapped_exceptions_item_class_1.MappedExceptionItem(customExceptionItem);
};
exports.DEFAULT_EXCEPTIONS = {
DEFAULT,
DATABASE,
VALIDATION,
OPERATION,
Custom: custom,
};
//# sourceMappingURL=mapped-exception.errors.js.map