UNPKG

naga-audit-service

Version:

A comprehensive audit service library for NestJS applications with MongoDB support

164 lines 7.06 kB
"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 AllExceptionsFilter_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.AllExceptionsFilter = void 0; const common_1 = require("@nestjs/common"); const mongodb_1 = require("mongodb"); const mongoose_1 = require("mongoose"); let AllExceptionsFilter = AllExceptionsFilter_1 = class AllExceptionsFilter { constructor() { this.logger = new common_1.Logger(AllExceptionsFilter_1.name); } catch(exception, host) { const ctx = host.switchToHttp(); const response = ctx.getResponse(); const request = ctx.getRequest(); let status = common_1.HttpStatus.INTERNAL_SERVER_ERROR; let message = 'Internal server error'; let errorDetails = {}; if (exception instanceof common_1.HttpException) { status = exception.getStatus(); const exceptionResponse = exception.getResponse(); if (typeof exceptionResponse === 'string') { message = exceptionResponse; } else if (typeof exceptionResponse === 'object') { message = exceptionResponse.message || message; errorDetails = exceptionResponse; } } else if (exception instanceof mongodb_1.MongoError || exception?.name === 'MongoServerError') { const mongoError = exception; switch (mongoError.code) { case 11000: status = common_1.HttpStatus.CONFLICT; const field = Object.keys(mongoError.keyPattern)[0]; message = `Duplicate value for field: ${field}`; errorDetails = { code: 'DUPLICATE_KEY_ERROR', field, value: mongoError.keyValue[field] }; break; case 121: status = common_1.HttpStatus.BAD_REQUEST; message = 'Document validation failed'; errorDetails = { code: 'VALIDATION_ERROR', details: mongoError.errInfo?.details }; break; case 13: status = common_1.HttpStatus.UNAUTHORIZED; message = 'Unauthorized access to database'; errorDetails = { code: 'UNAUTHORIZED' }; break; case 18: status = common_1.HttpStatus.UNAUTHORIZED; message = 'Database authentication failed'; errorDetails = { code: 'AUTHENTICATION_FAILED' }; break; case 48: status = common_1.HttpStatus.CONFLICT; message = 'Collection already exists'; errorDetails = { code: 'NAMESPACE_EXISTS' }; break; case 26: status = common_1.HttpStatus.NOT_FOUND; message = 'Collection not found'; errorDetails = { code: 'NAMESPACE_NOT_FOUND' }; break; case 2: status = common_1.HttpStatus.BAD_REQUEST; message = 'Invalid value provided'; errorDetails = { code: 'BAD_VALUE' }; break; case 9: status = common_1.HttpStatus.BAD_REQUEST; message = 'Failed to parse query'; errorDetails = { code: 'FAILED_TO_PARSE' }; break; case 51: status = common_1.HttpStatus.BAD_REQUEST; message = 'Failed to create index'; errorDetails = { code: 'INDEX_CREATION_FAILED' }; break; case 85: status = common_1.HttpStatus.REQUEST_TIMEOUT; message = 'Database operation timed out'; errorDetails = { code: 'OPERATION_TIMEOUT' }; break; default: message = mongoError.message || 'Database operation failed'; errorDetails = { code: 'MONGO_ERROR', mongoCode: mongoError.code }; } } else if (exception instanceof mongoose_1.Error) { if (exception instanceof mongoose_1.Error.ValidationError) { status = common_1.HttpStatus.BAD_REQUEST; message = 'Validation failed'; errorDetails = { code: 'VALIDATION_ERROR', errors: Object.keys(exception.errors).reduce((acc, key) => { acc[key] = exception.errors[key].message; return acc; }, {}) }; } else if (exception instanceof mongoose_1.Error.CastError) { status = common_1.HttpStatus.BAD_REQUEST; message = `Invalid ${exception.path}: ${exception.value}`; errorDetails = { code: 'CAST_ERROR', path: exception.path, value: exception.value }; } else { message = exception.message; errorDetails = { code: 'MONGOOSE_ERROR', name: exception.name }; } } this.logger.error(`[${request.method}] ${request.url} - ${message}`, exception?.stack); response.status(status).json({ statusCode: status, message, path: request.url, timestamp: new Date().toISOString(), ...errorDetails, }); } }; exports.AllExceptionsFilter = AllExceptionsFilter; exports.AllExceptionsFilter = AllExceptionsFilter = AllExceptionsFilter_1 = __decorate([ (0, common_1.Catch)() ], AllExceptionsFilter); //# sourceMappingURL=all-exceptions.filter.js.map