-
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: handle errors with prisma exception filter
- Loading branch information
1 parent
287d4b2
commit 8bb926e
Showing
6 changed files
with
98 additions
and
28 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
7 changes: 7 additions & 0 deletions
7
src/prisma-client-exception/prisma-client-exception.filter.spec.ts
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,7 @@ | ||
import { PrismaClientExceptionFilter } from "./prisma-client-exception.filter"; | ||
|
||
describe("PrismaClientExceptionFilter", () => { | ||
it("should be defined", () => { | ||
expect(new PrismaClientExceptionFilter()).toBeDefined(); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
src/prisma-client-exception/prisma-client-exception.filter.ts
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,36 @@ | ||
import { | ||
ArgumentsHost, | ||
Catch, | ||
HttpException, | ||
HttpStatus, | ||
} from "@nestjs/common"; | ||
import { BaseExceptionFilter } from "@nestjs/core"; | ||
import { Prisma } from "@prisma/client"; | ||
|
||
@Catch(Prisma.PrismaClientKnownRequestError) | ||
export class PrismaClientExceptionFilter extends BaseExceptionFilter { | ||
catch(exception: Prisma.PrismaClientKnownRequestError, host: ArgumentsHost): void { | ||
const { code, message } = exception; | ||
|
||
// Log full error message on the server | ||
console.error(message); | ||
|
||
// Trim error message for the client | ||
const trimmedMessage = message.substring(message.lastIndexOf("\n") + 1); | ||
|
||
// Map Prisma Client exception codes to their corresponding HTTP status code | ||
switch (code) { | ||
case "P2000": | ||
super.catch(new HttpException(trimmedMessage, HttpStatus.BAD_REQUEST), host); | ||
break; | ||
case "P2002": | ||
super.catch(new HttpException(trimmedMessage, HttpStatus.CONFLICT), host); | ||
break; | ||
case "P2025": | ||
super.catch(new HttpException(trimmedMessage, HttpStatus.NOT_FOUND), host); | ||
break; | ||
default: | ||
super.catch(exception, host); | ||
} | ||
} | ||
} |
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
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
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
8bb926e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
node-nest-typescript-starter-project – ./
node-nest-typescript-starter-project-git-main-nicolasbuecher.vercel.app
node-nest-typescript-starter-project-nicolasbuecher.vercel.app
node-nest-typescript-starter-project.vercel.app