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

All validation, guards and interceptors commands · NestJS 11 cheatsheet

Was this page helpful?