Created
June 22, 2021 15:19
-
-
Save GiuseppeMP/788ec5ec2e16f4dfe27f060b5faa4212 to your computer and use it in GitHub Desktop.
This file contains 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 { | |
ArgumentsHost, | |
Catch, | |
ExceptionFilter, | |
HttpException, | |
HttpStatus, | |
} from '@nestjs/common' | |
@Catch() | |
export class AllExceptionsFilter 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 | |
response.status(status).json({ | |
statusCode: status, | |
timestamp: new Date().toISOString(), | |
path: request.url, | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment