Skip to content

Instantly share code, notes, and snippets.

@gsusmonzon
Last active February 17, 2024 06:04
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save gsusmonzon/ecd7842495f07594761e40c2758617d0 to your computer and use it in GitHub Desktop.
Save gsusmonzon/ecd7842495f07594761e40c2758617d0 to your computer and use it in GitHub Desktop.
Make NestJs returns 404 when EntityNotFoundError exception is thrown

Make NestJs returns 404 when EntityNotFoundError exception is thrown

When using findOrFail() or findOneOrFail() from typeORM, a 500 error is returned if there is no entity (EntityNotFoundError).

To make it returns a 404, use an exception filter as described in https://docs.nestjs.com/exception-filters .

file /src/filters/entity-not-found-exception.filter.ts

import { Catch, ExceptionFilter, ArgumentsHost} from "@nestjs/common";
import { EntityNotFoundError } from 'typeorm/error/EntityNotFoundError'
import { Response } from 'express';


/**
 * Custom exception filter to convert EntityNotFoundError from TypeOrm to NestJs responses
 * @see also @https://docs.nestjs.com/exception-filters
 */
@Catch(EntityNotFoundError, Error)
export class EntityNotFoundExceptionFilter implements ExceptionFilter {
  public catch(exception: EntityNotFoundError, host: ArgumentsHost) {

    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    return response.status(404).json({ message: { statusCode: 404, error: 'Not Found', message: exception.message } });
  }
}

Use it in your main module

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { EntityNotFoundExceptionFilter } from './filters/entity-not-found-exception.filter';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  app.useGlobalFilters(new EntityNotFoundExceptionFilter());
  await app.listen(3000);
}
bootstrap();
@lakmalniranga
Copy link

Really helpful thanks!

@marchrius
Copy link

Great! IMHO catching a generic Error is not a good choice.

@crrmacarse
Copy link

Be careful by including Error in the catch. This will include exception thrown by others(such as class-validator) to be formatted.

This should be EntityNotFoundError only as it focuses with typeorm

@gsusmonzon
Copy link
Author

@marchrius , @crrmacarse you are right
in fact reviewing my current code, i am using only @Catch(EntityNotFoundError)
I guess i did a mistake when editing this gist

@Wangenye
Copy link

Wangenye commented Jun 6, 2022

Great..Really useful!

@achepukov
Copy link

Thanks lot! You made my day! :)

@Wangenye
Copy link

Wangenye commented Jul 1, 2023

Now you can also use one form nestjs/common

throw new NotFoundException()

@RockYou-cmd
Copy link

@Wangenye thank you so much

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment