Nest

How can we implement global exception filters for consistent enterprise error responses?

March 18, 2026

download ready
Thank You
Your submission has been received.
We will be in touch and contact you soon!

A global exception filter in NestJS catches all errors across your whole app and returns a neat, consistent JSON response every time, which is perfect for enterprise-level APIs that need clear, uniform error messages.

Just create a class with the @Catch() decorator, implement the ExceptionFilter interface, and format the error response however you want, like adding status, timestamp, and message. Then register this filter globally in your main.ts with app.useGlobalFilters() so it covers the entire app.

Code

import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common';

@Catch()
export class GlobalExceptionFilter implements ExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const request = ctx.getRequest();

    const status = exception instanceof HttpException
      ? exception.getStatus()
      : HttpStatus.INTERNAL_SERVER_ERROR;

    const message = exception instanceof HttpException
      ? exception.getResponse()
      : 'Internal server error';

    response.status(status).json({
      statusCode: status,
      timestamp: new Date().toISOString(),
      path: request.url,
      message,
    });
  }
}
      

Register in main.ts:

Code

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalFilters(new GlobalExceptionFilter());
  await app.listen(3000);
}
bootstrap();
      
Hire Now!

Need Help with Nest Development ?

Work with our skilled nest developers to accelerate your project and boost its performance.
**Hire now**Hire Now**Hire Now**Hire now**Hire now

How can we implement global exception filters for consistent enterprise error responses?

A global exception filter in NestJS catches all errors across your whole app and returns a neat, consistent JSON response every time, which is perfect for enterprise-level APIs that need clear, uniform error messages.

Just create a class with the @Catch() decorator, implement the ExceptionFilter interface, and format the error response however you want, like adding status, timestamp, and message. Then register this filter globally in your main.ts with app.useGlobalFilters() so it covers the entire app.

Code

import { ExceptionFilter, Catch, ArgumentsHost, HttpException, HttpStatus } from '@nestjs/common';

@Catch()
export class GlobalExceptionFilter implements ExceptionFilter {
  catch(exception: unknown, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse();
    const request = ctx.getRequest();

    const status = exception instanceof HttpException
      ? exception.getStatus()
      : HttpStatus.INTERNAL_SERVER_ERROR;

    const message = exception instanceof HttpException
      ? exception.getResponse()
      : 'Internal server error';

    response.status(status).json({
      statusCode: status,
      timestamp: new Date().toISOString(),
      path: request.url,
      message,
    });
  }
}
      

Register in main.ts:

Code

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalFilters(new GlobalExceptionFilter());
  await app.listen(3000);
}
bootstrap();