NestJS 11 · Validation, guards and interceptors
Customize error responses
A filter turns exceptions into the responses you want.
@Catch(HttpException)
export class HttpErrorFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const response = host.switchToHttp().getResponse();
response.status(exception.getStatus()).json({ message: exception.message });
}
}
Official NestJS 11 documentation
More validation, guards and interceptors commands
- Describe and validate a request body
- Enable validation globally
- Protect routes with a guard
- Run code around handlers
- Enable CORS
- Generate OpenAPI docs
All validation, guards and interceptors commands · NestJS 11 cheatsheet