Create a Custom Exception
export class ForbiddenException extends HttpException {
constructor() {
super('Forbidden', HttpStatus.FORBIDDEN);
}
}
- In many cases, you will not need to write custom exceptions, and can use the built-in Nest HTTP exception.
- If you do need to create customized exceptions, it's good practice to create your own exceptions hierarchy, where your custom exceptions inherit from the base HttpException class.
- With this approach, Nest will recognize your exceptions, and automatically take care of the error responses.
CatsController
@Get()
async findAll() {
throw new ForbiddenException();
}