Created
March 27, 2025 18:31
-
-
Save Vatsalya-singhi/e1a7b81d3b62d2d27a3335488c2c5cc2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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