Skip to content

Instantly share code, notes, and snippets.

@Vatsalya-singhi
Created March 27, 2025 18:31
Show Gist options
  • Save Vatsalya-singhi/e1a7b81d3b62d2d27a3335488c2c5cc2 to your computer and use it in GitHub Desktop.
Save Vatsalya-singhi/e1a7b81d3b62d2d27a3335488c2c5cc2 to your computer and use it in GitHub Desktop.
import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { Response } from 'express';
@Catch(HttpException) // Catches only HTTP-related exceptions
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const status = exception.getStatus();
const message = exception.getResponse();
response.status(status).json({
statusCode: status,
message,
timestamp: new Date().toISOString(),
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment