Skip to content

Instantly share code, notes, and snippets.

@Lobbyra
Created March 23, 2022 10:02
Show Gist options
  • Save Lobbyra/c19411b4018aca257d203c5da237dc1a to your computer and use it in GitHub Desktop.
Save Lobbyra/c19411b4018aca257d203c5da237dc1a to your computer and use it in GitHub Desktop.
nest filter http exception for dev verbose and production obstruction
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