-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add entityNotFoundExceptionFilter
- Loading branch information
1 parent
b275d8d
commit 2960905
Showing
2 changed files
with
19 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
import { ValidationPipe } from '@nestjs/common'; | ||
import { NestFactory } from '@nestjs/core'; | ||
import { AppModule } from './app.module'; | ||
import { EntityNotFoundExceptionFilter } from './utils/entity-not-found-exception.filter'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(AppModule); | ||
|
||
app.useGlobalPipes(new ValidationPipe()); | ||
app.useGlobalFilters(new EntityNotFoundExceptionFilter()); | ||
|
||
await app.listen(3000); | ||
} | ||
bootstrap(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'; | ||
import { EntityNotFoundError } from 'typeorm'; | ||
import { Response } from 'express'; | ||
|
||
@Catch(EntityNotFoundError) | ||
export class EntityNotFoundExceptionFilter implements ExceptionFilter { | ||
public catch(exception: EntityNotFoundError, host: ArgumentsHost) { | ||
const ctx = host.switchToHttp(); | ||
const response = ctx.getResponse<Response>(); | ||
|
||
const statusCode = 404; | ||
|
||
return response.status(statusCode).json({ statusCode, message: 'Not Found' }); | ||
} | ||
} |