Created
March 23, 2022 10:02
-
-
Save Lobbyra/c19411b4018aca257d203c5da237dc1a to your computer and use it in GitHub Desktop.
nest filter http exception for dev verbose and production obstruction
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 { ArgumentsHost, Catch, ExceptionFilter, HttpException } from "@nestjs/common"; | |
import { Response } from "express"; | |
@Catch(HttpException) | |
export class ErrorHidingFilter implements ExceptionFilter { | |
catch(exception: HttpException, host: ArgumentsHost): void { | |
const ctx = host.switchToHttp(); | |
const response = ctx.getResponse<Response>(); | |
const status = exception.getStatus(); | |
const message = exception.getResponse(); | |
if (process.env.NODE_ENV === "dev") { | |
response.status(status) | |
.json(message); | |
} else { | |
response.status(status) | |
.json({ | |
status: status | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment