@Catch()
Declares which exception types an exception filter class handles.
Syntax
@Catch(...exceptions: Type<any>[]) Parameters
| Name | Type | Required | Description |
|---|---|---|---|
exceptions | Type<any>[] | No | Exception classes the filter handles; omit to catch all. |
Returns
ClassDecorator — A decorator applied to an exception filter class.
Examples
import {
ExceptionFilter, Catch, ArgumentsHost, HttpException,
} from '@nestjs/common';
@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
catch(exception: HttpException, host: ArgumentsHost) {
const res = host.switchToHttp().getResponse();
res.status(exception.getStatus()).json({
message: exception.message,
});
}
}
Notes
@Catch() with no arguments creates a catch-all filter. Multiple types may
be listed to handle several exception classes with one filter. Bind the
filter with @UseFilters() or app.useGlobalFilters().