
Security News
Deno Goes the Way of the Dinosaur
Cloudflare is acquiring Deno’s team, which will provide another year of bug fixes and security updates before ending support for the runtime.
nestjs-exception-handler
Advanced tools
A production-grade global exception handling system for NestJS applications
A production-grade global exception handling system for NestJS applications that provides standardized error responses, supports Prisma errors, DTO validation, HTTP exceptions, and more.
npm install nestjs-exception-handler
import { Module } from '@nestjs/common';
import { ExceptionHandlerModule } from 'nestjs-exception-handler';
@Module({
imports: [ExceptionHandlerModule.forRoot()],
})
export class AppModule {}
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { GlobalExceptionFilter } from 'nestjs-exception-handler';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const filter = app.get(GlobalExceptionFilter);
app.useGlobalFilters(filter);
await app.listen(3000);
}
bootstrap();
ExceptionHandlerModule.forRoot({
enableLogging: true, // Enable structured logging
hideStackTrace: true, // Hide stack traces in production
});
All responses follow this structure:
{
"success": false,
"message": "Database error",
"errorMessages": [
{
"path": "email",
"message": "User with this email already exists"
}
]
}
Handles all Prisma error types with automatic path extraction:
P2002 - Unique constraint violationP2003 - Foreign key constraint violationP2005 - Invalid valueP2006 - Invalid formatP2025 - Record not foundExample:
{
"path": "email",
"message": "A record with this email already exists."
}
Automatically extracts validation errors from class-validator:
// Example validation error
{
"path": "email",
"message": "email must be a valid email"
}
Handles all NestJS HTTP exceptions:
throw new BadRequestException('Invalid input');
Fallback handler returns generic error message.
Extend the ExceptionFormatter interface to create custom formatters:
import { ExceptionFormatter, ErrorMessage } from 'nestjs-exception-handler';
export class CustomExceptionFormatter implements ExceptionFormatter {
supports(exception: unknown): boolean {
return exception instanceof CustomError;
}
format(exception: unknown): ErrorMessage[] {
const error = exception as CustomError;
return [{ path: error.field, message: error.message }];
}
message(exception: unknown): string {
return 'Custom error occurred';
}
}
forRoot(config?) - Register globally with optional configurationforFeature(config?) - Register for specific modulesregisterFormatter(formatter) - Add custom formatterformatException(exception) - Format exception to standardized responseformatErrors(exception) - Get error messages arraygetErrorMessage(exception) - Get error message stringgetAllFormatters() - Get registered formatters{
"success": false,
"message": "A record with this email already exists.",
"errorMessages": [
{
"path": "email",
"message": "A record with this email already exists."
}
]
}
{
"success": false,
"message": "Validation failed",
"errorMessages": [
{
"path": "email",
"message": "email must be a valid email"
},
{
"path": "password",
"message": "password must be longer than 8 characters"
}
]
}
{
"success": false,
"message": "Resource not found",
"errorMessages": [
{
"path": "unknown",
"message": "Resource not found"
}
]
}
Contributions are welcome! Please feel free to submit a Pull Request.
git checkout -b feature/amazing-feature)git commit -m 'Add some amazing feature')git push origin feature/amazing-feature)MIT
Nurul Islam Rimon
FAQs
A production-grade global exception handling system for NestJS applications
The npm package nestjs-exception-handler receives a total of 0 weekly downloads. As such, nestjs-exception-handler popularity was classified as not popular.
We found that nestjs-exception-handler demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.

Security News
Cloudflare is acquiring Deno’s team, which will provide another year of bug fixes and security updates before ending support for the runtime.

Security News
Anthropic’s free OSS Scanner uses Mythos and other Claude models to find open source vulnerabilities and send maintainers reports with proposed fixes.

Security News
On Front Lines’ Early Adopters: CISO podcast, Andrew Becherer shares lessons from Datadog, weighs the long-term costs of AI-built tools, and explains how founders can actually gain a CISO’s trust.