nestjs-exception-handler 3.0.0 → 4.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +182 -182
  2. package/package.json +75 -75
package/README.md CHANGED
@@ -1,182 +1,182 @@
1
- # prisma-error-formatter
2
-
3
- [![npm version](https://img.shields.io/npm/v/prisma-error-formatter.svg)](https://www.npmjs.com/package/prisma-error-formatter)
4
- [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
-
6
- A flexible and customizable **Prisma** error formatter to simplify and unify error handling in Prisma Client applications. Easily transform Prisma errors into user-friendly, consistent error messages for your APIs or UI.
7
-
8
- ---
9
-
10
- ## Features
11
-
12
- - Formats common Prisma Client errors like unique constraint violations, foreign key errors, validation errors, and initialization errors.
13
- - Supports custom error formatting via a callback function.
14
- - Works with Prisma Client’s error classes:
15
- - `PrismaClientKnownRequestError`
16
- - `PrismaClientValidationError`
17
- - `PrismaClientInitializationError`
18
- - `PrismaClientRustPanicError`
19
- - Returns structured error messages with clear `path` and `message` fields.
20
- - Written in TypeScript with full typings.
21
-
22
- ---
23
-
24
- ## Installation
25
-
26
- ```bash
27
- npm install prisma-error-formatter @prisma/client
28
- ```
29
-
30
- ---
31
-
32
- ## Basic Usage
33
-
34
- ```ts
35
- import { PrismaClient } from "@prisma/client";
36
- import { PrismaExceptionFormatter } from "prisma-error-formatter";
37
-
38
- const prisma = new PrismaClient();
39
- const formatter = new PrismaExceptionFormatter();
40
-
41
- async function createUser(email: string) {
42
- try {
43
- await prisma.user.create({ data: { email } });
44
- } catch (error) {
45
- const formattedErrors = formatter.formatError(error);
46
- console.error(formattedErrors);
47
- /*
48
- Example output:
49
- [
50
- {
51
- path: "email",
52
- message: "A record with this email already exists."
53
- }
54
- ]
55
- */
56
- }
57
- }
58
- ```
59
-
60
- ---
61
-
62
- ## Using a Custom Formatter
63
-
64
- You can provide your own formatting logic by passing a `format` function when creating the formatter instance:
65
-
66
- ```ts
67
- import { PrismaExceptionFormatter, ErrorMessage } from "prisma-error-formatter";
68
-
69
- const formatter = new PrismaExceptionFormatter({
70
- format: ({ type, error, defaults }) => {
71
- // Add extra info or change messages based on error type
72
- if (type === "known" && error.code === "P2002") {
73
- return [
74
- {
75
- path: defaults[0].path,
76
- message: `Custom: Duplicate value found for ${defaults[0].path}`,
77
- },
78
- ];
79
- }
80
- // Fallback to default formatting
81
- return defaults;
82
- },
83
- });
84
- ```
85
-
86
- ---
87
-
88
- ## API
89
-
90
- ### `new PrismaExceptionFormatter(options?: { format?: FormatFunction })`
91
-
92
- Creates a new formatter instance.
93
-
94
- - `options.format` - Optional custom format function. Receives an object with:
95
- - `type`: The error type (`known`, `validation`, `initialization`, `panic`, `unknown`)
96
- - `error`: The original error object
97
- - `defaults`: The default formatted error messages (array of `{ path, message }`)
98
-
99
- Returns formatted errors as an array.
100
-
101
- ### Methods
102
-
103
- - `formatError(exception: any): ErrorMessage[]`
104
- Automatically detects the Prisma error type and returns formatted messages.
105
-
106
- - `formatPrismaError(exception: PrismaClientKnownRequestError): ErrorMessage[]`
107
- Formats known Prisma client errors.
108
-
109
- - `formatQueryError(exception: PrismaClientValidationError | PrismaClientRustPanicError): ErrorMessage[]`
110
- Formats validation or panic errors.
111
-
112
- - `formatInitializationError(exception: PrismaClientInitializationError): ErrorMessage[]`
113
- Formats database initialization errors.
114
-
115
- - `formatUnknownError(exception: any): ErrorMessage[]`
116
- Formats unknown errors.
117
-
118
- ---
119
-
120
- ## Supported Prisma Error Codes (Known Errors)
121
-
122
- - `P2002` - Unique constraint violation
123
- - `P2003` - Foreign key constraint failure
124
- - `P2005`, `P2006` - Invalid value errors
125
- - `P2025` - Record not found
126
-
127
- ---
128
-
129
- ## License
130
-
131
- MIT © Nurul Islam Rimon
132
-
133
- ---
134
-
135
- ## Contribution
136
-
137
- Contributions, issues, and feature requests are welcome! Feel free to check
138
-
139
- [project page](https://github.com/nurulislamrimon/prisma-error-formatter)
140
-
141
- [issues page](https://github.com/nurulislamrimon/prisma-error-formatter/issues)
142
-
143
- ---
144
-
145
- ## Related
146
-
147
- - [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client)
148
- - [Prisma Error Codes](https://www.prisma.io/docs/reference/api-reference/error-reference)
149
-
150
- ---
151
-
152
- _Built with ❤️ by Nurul Islam Rimon_
153
-
154
- ---
155
-
156
- ### 🛠️ Open Source Contribution
157
-
158
- This project is open to all contributors! Whether you're fixing bugs, improving documentation, adding new formatters, or suggesting ideas — your contribution is highly appreciated.
159
-
160
- #### How to Contribute
161
-
162
- 1. **Fork** this repository
163
- 2. Create your **feature branch**:
164
- ```bash
165
- git checkout -b feat/my-awesome-feature
166
- ```
167
- 3. **Commit your changes**:
168
- ```bash
169
- git commit -m "feat: add my awesome feature"
170
- ```
171
- 4. **Push to the branch**:
172
- ```bash
173
- git push origin feat/my-awesome-feature
174
- ```
175
- 5. **Open a pull request**
176
-
177
- ### 🙌 Contributions Welcome!
178
-
179
- - 📖 Improve the documentation
180
- - 🧪 Add unit tests
181
- - 🔍 Add support for more Prisma error codes
182
- - 💡 Propose new formatting strategies or ideas
1
+ # prisma-error-formatter
2
+
3
+ [![npm version](https://img.shields.io/npm/v/prisma-error-formatter.svg)](https://www.npmjs.com/package/prisma-error-formatter)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ A flexible and customizable **Prisma** error formatter to simplify and unify error handling in Prisma Client applications. Easily transform Prisma errors into user-friendly, consistent error messages for your APIs or UI.
7
+
8
+ ---
9
+
10
+ ## Features
11
+
12
+ - Formats common Prisma Client errors like unique constraint violations, foreign key errors, validation errors, and initialization errors.
13
+ - Supports custom error formatting via a callback function.
14
+ - Works with Prisma Client’s error classes:
15
+ - `PrismaClientKnownRequestError`
16
+ - `PrismaClientValidationError`
17
+ - `PrismaClientInitializationError`
18
+ - `PrismaClientRustPanicError`
19
+ - Returns structured error messages with clear `path` and `message` fields.
20
+ - Written in TypeScript with full typings.
21
+
22
+ ---
23
+
24
+ ## Installation
25
+
26
+ ```bash
27
+ npm install prisma-error-formatter @prisma/client
28
+ ```
29
+
30
+ ---
31
+
32
+ ## Basic Usage
33
+
34
+ ```ts
35
+ import { PrismaClient } from "@prisma/client";
36
+ import { PrismaExceptionFormatter } from "prisma-error-formatter";
37
+
38
+ const prisma = new PrismaClient();
39
+ const formatter = new PrismaExceptionFormatter();
40
+
41
+ async function createUser(email: string) {
42
+ try {
43
+ await prisma.user.create({ data: { email } });
44
+ } catch (error) {
45
+ const formattedErrors = formatter.formatError(error);
46
+ console.error(formattedErrors);
47
+ /*
48
+ Example output:
49
+ [
50
+ {
51
+ path: "email",
52
+ message: "A record with this email already exists."
53
+ }
54
+ ]
55
+ */
56
+ }
57
+ }
58
+ ```
59
+
60
+ ---
61
+
62
+ ## Using a Custom Formatter
63
+
64
+ You can provide your own formatting logic by passing a `format` function when creating the formatter instance:
65
+
66
+ ```ts
67
+ import { PrismaExceptionFormatter, ErrorMessage } from "prisma-error-formatter";
68
+
69
+ const formatter = new PrismaExceptionFormatter({
70
+ format: ({ type, error, defaults }) => {
71
+ // Add extra info or change messages based on error type
72
+ if (type === "known" && error.code === "P2002") {
73
+ return [
74
+ {
75
+ path: defaults[0].path,
76
+ message: `Custom: Duplicate value found for ${defaults[0].path}`,
77
+ },
78
+ ];
79
+ }
80
+ // Fallback to default formatting
81
+ return defaults;
82
+ },
83
+ });
84
+ ```
85
+
86
+ ---
87
+
88
+ ## API
89
+
90
+ ### `new PrismaExceptionFormatter(options?: { format?: FormatFunction })`
91
+
92
+ Creates a new formatter instance.
93
+
94
+ - `options.format` - Optional custom format function. Receives an object with:
95
+ - `type`: The error type (`known`, `validation`, `initialization`, `panic`, `unknown`)
96
+ - `error`: The original error object
97
+ - `defaults`: The default formatted error messages (array of `{ path, message }`)
98
+
99
+ Returns formatted errors as an array.
100
+
101
+ ### Methods
102
+
103
+ - `formatError(exception: any): ErrorMessage[]`
104
+ Automatically detects the Prisma error type and returns formatted messages.
105
+
106
+ - `formatPrismaError(exception: PrismaClientKnownRequestError): ErrorMessage[]`
107
+ Formats known Prisma client errors.
108
+
109
+ - `formatQueryError(exception: PrismaClientValidationError | PrismaClientRustPanicError): ErrorMessage[]`
110
+ Formats validation or panic errors.
111
+
112
+ - `formatInitializationError(exception: PrismaClientInitializationError): ErrorMessage[]`
113
+ Formats database initialization errors.
114
+
115
+ - `formatUnknownError(exception: any): ErrorMessage[]`
116
+ Formats unknown errors.
117
+
118
+ ---
119
+
120
+ ## Supported Prisma Error Codes (Known Errors)
121
+
122
+ - `P2002` - Unique constraint violation
123
+ - `P2003` - Foreign key constraint failure
124
+ - `P2005`, `P2006` - Invalid value errors
125
+ - `P2025` - Record not found
126
+
127
+ ---
128
+
129
+ ## License
130
+
131
+ MIT © Nurul Islam Rimon
132
+
133
+ ---
134
+
135
+ ## Contribution
136
+
137
+ Contributions, issues, and feature requests are welcome! Feel free to check
138
+
139
+ [project page](https://github.com/nurulislamrimon/prisma-error-formatter)
140
+
141
+ [issues page](https://github.com/nurulislamrimon/prisma-error-formatter/issues)
142
+
143
+ ---
144
+
145
+ ## Related
146
+
147
+ - [Prisma Client](https://www.prisma.io/docs/concepts/components/prisma-client)
148
+ - [Prisma Error Codes](https://www.prisma.io/docs/reference/api-reference/error-reference)
149
+
150
+ ---
151
+
152
+ _Built with ❤️ by Nurul Islam Rimon_
153
+
154
+ ---
155
+
156
+ ### 🛠️ Open Source Contribution
157
+
158
+ This project is open to all contributors! Whether you're fixing bugs, improving documentation, adding new formatters, or suggesting ideas — your contribution is highly appreciated.
159
+
160
+ #### How to Contribute
161
+
162
+ 1. **Fork** this repository
163
+ 2. Create your **feature branch**:
164
+ ```bash
165
+ git checkout -b feat/my-awesome-feature
166
+ ```
167
+ 3. **Commit your changes**:
168
+ ```bash
169
+ git commit -m "feat: add my awesome feature"
170
+ ```
171
+ 4. **Push to the branch**:
172
+ ```bash
173
+ git push origin feat/my-awesome-feature
174
+ ```
175
+ 5. **Open a pull request**
176
+
177
+ ### 🙌 Contributions Welcome!
178
+
179
+ - 📖 Improve the documentation
180
+ - 🧪 Add unit tests
181
+ - 🔍 Add support for more Prisma error codes
182
+ - 💡 Propose new formatting strategies or ideas
package/package.json CHANGED
@@ -1,75 +1,75 @@
1
- {
2
- "name": "nestjs-exception-handler",
3
- "version": "3.0.0",
4
- "description": "A comprehensive global exception filter for NestJS that handles all common NestJS, Prisma, and Node.js exceptions with formatted error responses.",
5
- "main": "dist/index.js",
6
- "types": "dist/index.d.ts",
7
- "exports": {
8
- ".": {
9
- "import": "./dist/index.js",
10
- "require": "./dist/index.js",
11
- "types": "./dist/index.d.ts"
12
- }
13
- },
14
- "files": [
15
- "dist",
16
- "README.md"
17
- ],
18
- "scripts": {
19
- "build": "tsc",
20
- "prepublishOnly": "npm run build",
21
- "test": "echo \"No tests specified\" && exit 0"
22
- },
23
- "keywords": [
24
- "nestjs",
25
- "exception",
26
- "filter",
27
- "error",
28
- "handler",
29
- "prisma",
30
- "error-handler",
31
- "exception-filter",
32
- "global-exception-filter",
33
- "nestjs-exception",
34
- "nestjs-error",
35
- "validation",
36
- "class-validator"
37
- ],
38
- "author": "Nurul Islam Rimon <nurulislamrimon@gmail.com>",
39
- "license": "MIT",
40
- "repository": {
41
- "type": "git",
42
- "url": "https://github.com/nurulislamrimon/nestjs-exception-handler.git"
43
- },
44
- "bugs": {
45
- "url": "https://github.com/nurulislamrimon/nestjs-exception-handler/issues"
46
- },
47
- "homepage": "https://github.com/nurulislamrimon/nestjs-exception-handler#readme",
48
- "engines": {
49
- "node": ">=16"
50
- },
51
- "peerDependencies": {
52
- "@nestjs/common": "^10.0.0",
53
- "@nestjs/core": "^10.0.0",
54
- "@prisma/client": ">=4.0.0"
55
- },
56
- "peerDependenciesMeta": {
57
- "@nestjs/common": {
58
- "optional": true
59
- },
60
- "@nestjs/core": {
61
- "optional": true
62
- },
63
- "@prisma/client": {
64
- "optional": true
65
- }
66
- },
67
- "devDependencies": {
68
- "@nestjs/common": "^10.0.0",
69
- "@nestjs/core": "^10.0.0",
70
- "@prisma/client": "^6.9.0",
71
- "@types/express": "^5.0.0",
72
- "@types/node": "^24.0.2",
73
- "typescript": "^5.8.3"
74
- }
75
- }
1
+ {
2
+ "name": "nestjs-exception-handler",
3
+ "version": "4.0.0",
4
+ "description": "A comprehensive global exception filter for NestJS that handles all common NestJS, Prisma, and Node.js exceptions with formatted error responses.",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "import": "./dist/index.js",
10
+ "require": "./dist/index.js",
11
+ "types": "./dist/index.d.ts"
12
+ }
13
+ },
14
+ "files": [
15
+ "dist",
16
+ "README.md"
17
+ ],
18
+ "scripts": {
19
+ "build": "tsc",
20
+ "prepublishOnly": "npm run build",
21
+ "test": "echo \"No tests specified\" && exit 0"
22
+ },
23
+ "keywords": [
24
+ "nestjs",
25
+ "exception",
26
+ "filter",
27
+ "error",
28
+ "handler",
29
+ "prisma",
30
+ "error-handler",
31
+ "exception-filter",
32
+ "global-exception-filter",
33
+ "nestjs-exception",
34
+ "nestjs-error",
35
+ "validation",
36
+ "class-validator"
37
+ ],
38
+ "author": "Nurul Islam Rimon <nurulislamrimon@gmail.com>",
39
+ "license": "MIT",
40
+ "repository": {
41
+ "type": "git",
42
+ "url": "https://github.com/nurulislamrimon/nestjs-exception-handler.git"
43
+ },
44
+ "bugs": {
45
+ "url": "https://github.com/nurulislamrimon/nestjs-exception-handler/issues"
46
+ },
47
+ "homepage": "https://github.com/nurulislamrimon/nestjs-exception-handler#readme",
48
+ "engines": {
49
+ "node": ">=16"
50
+ },
51
+ "peerDependencies": {
52
+ "@nestjs/common": "^10.0.0",
53
+ "@nestjs/core": "^10.0.0",
54
+ "@prisma/client": ">=4.0.0"
55
+ },
56
+ "peerDependenciesMeta": {
57
+ "@nestjs/common": {
58
+ "optional": true
59
+ },
60
+ "@nestjs/core": {
61
+ "optional": true
62
+ },
63
+ "@prisma/client": {
64
+ "optional": true
65
+ }
66
+ },
67
+ "devDependencies": {
68
+ "@nestjs/common": "^10.0.0",
69
+ "@nestjs/core": "^10.0.0",
70
+ "@prisma/client": "^6.9.0",
71
+ "@types/express": "^5.0.0",
72
+ "@types/node": "^24.0.2",
73
+ "typescript": "^5.8.3"
74
+ }
75
+ }