@naman_deep_singh/errors-utils 1.3.4 → 1.4.1
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.
- package/README.md +37 -48
- package/dist/cjs/constants/errorCodes.d.ts +28 -23
- package/dist/cjs/constants/errorCodes.js +57 -22
- package/dist/cjs/constants/errorMessages.d.ts +8 -1
- package/dist/cjs/constants/errorMessages.js +72 -29
- package/dist/cjs/error/AppError.d.ts +2 -2
- package/dist/cjs/error/AppError.js +2 -2
- package/dist/cjs/error/ServiceUnavailableError.d.ts +4 -0
- package/dist/cjs/error/ServiceUnavailableError.js +11 -0
- package/dist/cjs/error/index.d.ts +14 -0
- package/dist/cjs/error/index.js +39 -0
- package/dist/cjs/errorRegistry/errorRegistry.d.ts +19 -0
- package/dist/cjs/errorRegistry/errorRegistry.js +63 -0
- package/dist/cjs/errorRegistry/index.d.ts +3 -0
- package/dist/cjs/errorRegistry/index.js +6 -0
- package/dist/cjs/index.d.ts +2 -14
- package/dist/cjs/index.js +3 -24
- package/dist/cjs/middleware/express/{errorHandler.js → errorHandler.middleware.js} +4 -4
- package/dist/cjs/middleware/express/index.d.ts +2 -2
- package/dist/cjs/middleware/express/index.js +2 -2
- package/dist/esm/constants/errorCodes.d.ts +28 -23
- package/dist/esm/constants/errorCodes.js +57 -22
- package/dist/esm/constants/errorMessages.d.ts +8 -1
- package/dist/esm/constants/errorMessages.js +72 -29
- package/dist/esm/error/AppError.d.ts +2 -2
- package/dist/esm/error/AppError.js +2 -2
- package/dist/esm/error/ServiceUnavailableError.d.ts +4 -0
- package/dist/esm/error/ServiceUnavailableError.js +7 -0
- package/dist/esm/error/index.d.ts +14 -0
- package/dist/esm/error/index.js +23 -0
- package/dist/esm/errorRegistry/errorRegistry.d.ts +19 -0
- package/dist/esm/errorRegistry/errorRegistry.js +59 -0
- package/dist/esm/errorRegistry/index.d.ts +3 -0
- package/dist/esm/errorRegistry/index.js +3 -0
- package/dist/esm/index.d.ts +2 -14
- package/dist/esm/index.js +3 -24
- package/dist/esm/middleware/express/{errorHandler.js → errorHandler.middleware.js} +5 -5
- package/dist/esm/middleware/express/index.d.ts +2 -2
- package/dist/esm/middleware/express/index.js +2 -2
- package/dist/types/constants/errorCodes.d.ts +28 -23
- package/dist/types/constants/errorMessages.d.ts +8 -1
- package/dist/types/error/AppError.d.ts +2 -2
- package/dist/types/error/ServiceUnavailableError.d.ts +4 -0
- package/dist/types/error/index.d.ts +14 -0
- package/dist/types/errorRegistry/errorRegistry.d.ts +19 -0
- package/dist/types/errorRegistry/index.d.ts +3 -0
- package/dist/types/index.d.ts +2 -14
- package/dist/types/middleware/express/index.d.ts +2 -2
- package/package.json +1 -1
- package/dist/cjs/error/RateLimitError.d.ts +0 -4
- package/dist/cjs/error/RateLimitError.js +0 -11
- package/dist/cjs/utils/mapAppErrorToResponder.d.ts +0 -3
- package/dist/cjs/utils/mapAppErrorToResponder.js +0 -27
- package/dist/esm/error/RateLimitError.d.ts +0 -4
- package/dist/esm/error/RateLimitError.js +0 -7
- package/dist/esm/utils/mapAppErrorToResponder.d.ts +0 -3
- package/dist/esm/utils/mapAppErrorToResponder.js +0 -24
- package/dist/types/error/RateLimitError.d.ts +0 -4
- package/dist/types/utils/mapAppErrorToResponder.d.ts +0 -3
- /package/dist/cjs/middleware/express/{errorConverter.d.ts → errorConverter.middleware.d.ts} +0 -0
- /package/dist/cjs/middleware/express/{errorConverter.js → errorConverter.middleware.js} +0 -0
- /package/dist/cjs/middleware/express/{errorHandler.d.ts → errorHandler.middleware.d.ts} +0 -0
- /package/dist/esm/middleware/express/{errorConverter.d.ts → errorConverter.middleware.d.ts} +0 -0
- /package/dist/esm/middleware/express/{errorConverter.js → errorConverter.middleware.js} +0 -0
- /package/dist/esm/middleware/express/{errorHandler.d.ts → errorHandler.middleware.d.ts} +0 -0
- /package/dist/types/middleware/express/{errorConverter.d.ts → errorConverter.middleware.d.ts} +0 -0
- /package/dist/types/middleware/express/{errorHandler.d.ts → errorHandler.middleware.d.ts} +0 -0
package/README.md
CHANGED
|
@@ -1,24 +1,20 @@
|
|
|
1
|
+
```bash
|
|
1
2
|
@naman_deep_singh/errors-utils
|
|
2
3
|
|
|
3
|
-
Version: 1.
|
|
4
|
+
Version: 1.4.1
|
|
4
5
|
|
|
5
6
|
A standardized, code-driven error handling system for TypeScript and Express applications, providing consistent error identity, responses, and middleware integration.
|
|
6
7
|
|
|
7
8
|
🚀 Features
|
|
8
9
|
|
|
9
10
|
✅ Structured Error Classes — AppError, HTTPError, ValidationError, etc.
|
|
10
|
-
|
|
11
11
|
✅ Strongly-Typed Error Codes — Centralized error identity via constants
|
|
12
|
-
|
|
13
12
|
✅ Centralized Error Messages — One source of truth for user-facing messages
|
|
14
|
-
|
|
15
13
|
✅ Express Middleware — Error converter & global handler
|
|
16
|
-
|
|
17
14
|
✅ Response Integration — Works seamlessly with @naman_deep_singh/response-utils
|
|
18
|
-
|
|
19
15
|
✅ TypeScript First — Full type safety & IntelliSense
|
|
20
|
-
|
|
21
16
|
✅ Consistent API Responses — Unified error shape across services
|
|
17
|
+
✅ Extendable Error Messages — Add or override messages at runtime
|
|
22
18
|
|
|
23
19
|
📦 Installation
|
|
24
20
|
npm install @naman_deep_singh/errors-utils
|
|
@@ -27,19 +23,17 @@ npm install @naman_deep_singh/errors-utils
|
|
|
27
23
|
|
|
28
24
|
This package is code-driven, not message-driven.
|
|
29
25
|
|
|
30
|
-
Errors are identified by stable error codes
|
|
31
|
-
|
|
32
|
-
Messages are resolved internally via centralized mappings
|
|
26
|
+
Errors are identified by stable error codes.
|
|
33
27
|
|
|
34
|
-
|
|
28
|
+
Messages are resolved internally via centralized mappings.
|
|
35
29
|
|
|
36
|
-
|
|
30
|
+
API contracts remain stable even if messages change.
|
|
37
31
|
|
|
38
|
-
|
|
32
|
+
Benefits:
|
|
39
33
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
Better logging, tracing, and observability
|
|
34
|
+
✅ Consistency across microservices
|
|
35
|
+
✅ Safe refactoring
|
|
36
|
+
✅ Better logging, tracing, and observability
|
|
43
37
|
|
|
44
38
|
🔧 Usage
|
|
45
39
|
Creating Errors (Recommended)
|
|
@@ -52,12 +46,14 @@ import {
|
|
|
52
46
|
ERROR_CODES,
|
|
53
47
|
} from '@naman_deep_singh/errors-utils'
|
|
54
48
|
|
|
49
|
+
// Basic usage
|
|
55
50
|
throw new BadRequestError(ERROR_CODES.BAD_REQUEST)
|
|
56
51
|
|
|
57
52
|
throw new UnauthorizedError(ERROR_CODES.UNAUTHORIZED)
|
|
58
53
|
|
|
59
54
|
throw new NotFoundError(ERROR_CODES.NOT_FOUND)
|
|
60
55
|
|
|
56
|
+
// With additional details
|
|
61
57
|
throw new ValidationError(ERROR_CODES.VALIDATION_FAILED, {
|
|
62
58
|
fields: ['email', 'password'],
|
|
63
59
|
})
|
|
@@ -65,16 +61,12 @@ throw new ValidationError(ERROR_CODES.VALIDATION_FAILED, {
|
|
|
65
61
|
throw new InternalServerError(ERROR_CODES.INTERNAL_SERVER_ERROR)
|
|
66
62
|
|
|
67
63
|
🧾 Error Codes & Messages
|
|
68
|
-
|
|
69
|
-
The package exposes a strongly-typed error code system.
|
|
70
|
-
|
|
71
64
|
import {
|
|
72
65
|
ERROR_CODES,
|
|
73
66
|
ERROR_MESSAGES,
|
|
74
67
|
ErrorCode,
|
|
75
68
|
} from '@naman_deep_singh/errors-utils'
|
|
76
69
|
|
|
77
|
-
Exports
|
|
78
70
|
|
|
79
71
|
ERROR_CODES — Canonical list of all supported error codes
|
|
80
72
|
|
|
@@ -85,18 +77,11 @@ ErrorCode — Union type of all valid error codes
|
|
|
85
77
|
Why Error Codes?
|
|
86
78
|
|
|
87
79
|
✅ Consistent error identity across services
|
|
88
|
-
|
|
89
80
|
✅ Centralized message management
|
|
90
|
-
|
|
91
81
|
✅ Safer API contracts
|
|
92
|
-
|
|
93
82
|
✅ Improved logging & observability
|
|
94
83
|
|
|
95
84
|
🌐 Express Middleware
|
|
96
|
-
|
|
97
|
-
This package provides Express-specific middleware under the hood
|
|
98
|
-
and exposes a clean public API.
|
|
99
|
-
|
|
100
85
|
import express from 'express'
|
|
101
86
|
import {
|
|
102
87
|
errorConverter,
|
|
@@ -119,21 +104,12 @@ app.post('/users', (req, res) => {
|
|
|
119
104
|
}
|
|
120
105
|
})
|
|
121
106
|
|
|
122
|
-
Middleware Responsibilities
|
|
123
|
-
|
|
124
|
-
errorConverter
|
|
125
107
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
Preserves known operational errors
|
|
129
|
-
|
|
130
|
-
expressErrorHandler
|
|
131
|
-
|
|
132
|
-
Sends standardized API responses
|
|
108
|
+
Middleware Responsibilities
|
|
133
109
|
|
|
134
|
-
|
|
110
|
+
errorConverter — Converts unknown errors into AppError, preserves operational errors
|
|
135
111
|
|
|
136
|
-
|
|
112
|
+
expressErrorHandler — Sends standardized API responses and integrates with @naman_deep_singh/response-utils
|
|
137
113
|
|
|
138
114
|
🔗 Integration
|
|
139
115
|
With @naman_deep_singh/response-utils
|
|
@@ -148,17 +124,14 @@ import { createServer } from '@naman_deep_singh/server-utils'
|
|
|
148
124
|
import { expressErrorHandler } from '@naman_deep_singh/errors-utils'
|
|
149
125
|
|
|
150
126
|
const server = createServer('My API', '1.0.0')
|
|
151
|
-
|
|
152
127
|
server.app.use(expressErrorHandler)
|
|
153
128
|
|
|
154
|
-
🧠
|
|
129
|
+
🧠 Extending & Adding Error Messages
|
|
155
130
|
|
|
156
|
-
You can safely extend existing errors
|
|
131
|
+
You can safely extend existing errors or add new codes/messages dynamically.
|
|
157
132
|
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
ERROR_CODES,
|
|
161
|
-
} from '@naman_deep_singh/errors-utils'
|
|
133
|
+
Extending Existing Error Class
|
|
134
|
+
import { InternalServerError, ERROR_CODES } from '@naman_deep_singh/errors-utils'
|
|
162
135
|
|
|
163
136
|
export class CryptoIntegrityError extends InternalServerError {
|
|
164
137
|
constructor(details?: unknown, cause?: Error) {
|
|
@@ -166,6 +139,22 @@ export class CryptoIntegrityError extends InternalServerError {
|
|
|
166
139
|
}
|
|
167
140
|
}
|
|
168
141
|
|
|
142
|
+
Registering Custom Error Messages
|
|
143
|
+
import { errorMessageRegistry } from '@naman_deep_singh/errors-utils'
|
|
144
|
+
|
|
145
|
+
// Add new messages or override existing ones
|
|
146
|
+
errorMessageRegistry.register({
|
|
147
|
+
CUSTOM_ERROR: 'Something went wrong with custom logic',
|
|
148
|
+
VALIDATION_FAILED: 'Custom validation failed message',
|
|
149
|
+
})
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
After this, AppError or any derived class will use the updated messages automatically.
|
|
153
|
+
|
|
154
|
+
import { AppError } from '@naman_deep_singh/errors-utils'
|
|
155
|
+
|
|
156
|
+
throw new AppError('CUSTOM_ERROR') // message: "Something went wrong with custom logic"
|
|
157
|
+
|
|
169
158
|
📚 Available Error Classes
|
|
170
159
|
Class Status Code Use Case
|
|
171
160
|
AppError Custom Base error class
|
|
@@ -179,7 +168,6 @@ NotFoundError 404 Resource not found
|
|
|
179
168
|
ConflictError 409 Resource conflicts
|
|
180
169
|
ValidationError 422 Validation errors
|
|
181
170
|
RateLimitError 429 Rate limiting
|
|
182
|
-
TooManyRequestsError 429 Alias of RateLimitError
|
|
183
171
|
CryptoIntegrityError 500 Crypto validation failure
|
|
184
172
|
InternalServerError 500 Server-side failures
|
|
185
173
|
🎯 Standard Error Response
|
|
@@ -197,4 +185,5 @@ All errors resolve to a consistent response shape:
|
|
|
197
185
|
|
|
198
186
|
📄 License
|
|
199
187
|
|
|
200
|
-
ISC © Naman Deep Singh
|
|
188
|
+
ISC © Naman Deep Singh
|
|
189
|
+
```
|
|
@@ -1,32 +1,37 @@
|
|
|
1
1
|
export declare const ERROR_CODES: {
|
|
2
2
|
readonly BAD_REQUEST: "BAD_REQUEST";
|
|
3
|
+
readonly VALIDATION_FAILED: "VALIDATION_FAILED";
|
|
3
4
|
readonly UNAUTHORIZED: "UNAUTHORIZED";
|
|
4
5
|
readonly FORBIDDEN: "FORBIDDEN";
|
|
5
6
|
readonly NOT_FOUND: "NOT_FOUND";
|
|
6
7
|
readonly CONFLICT: "CONFLICT";
|
|
7
|
-
readonly VALIDATION_FAILED: "VALIDATION_FAILED";
|
|
8
|
-
readonly RATE_LIMIT_EXCEEDED: "RATE_LIMIT_EXCEEDED";
|
|
9
|
-
readonly INTERNAL_SERVER_ERROR: "INTERNAL_SERVER_ERROR";
|
|
10
|
-
readonly CRYPTO_INTEGRITY_ERROR: "CRYPTO_INTEGRITY_ERROR";
|
|
11
8
|
readonly TOO_MANY_REQUESTS: "TOO_MANY_REQUESTS";
|
|
12
|
-
readonly
|
|
9
|
+
readonly INTERNAL_SERVER_ERROR: "INTERNAL_SERVER_ERROR";
|
|
10
|
+
readonly SERVICE_UNAVAILABLE: "SERVICE_UNAVAILABLE";
|
|
11
|
+
readonly DEPENDENCY_FAILURE: "DEPENDENCY_FAILURE";
|
|
12
|
+
readonly HTTP_ERROR: "HTTP_ERROR";
|
|
13
|
+
readonly HTTP_TIMEOUT: "HTTP_TIMEOUT";
|
|
14
|
+
readonly UPSTREAM_SERVICE_ERROR: "UPSTREAM_SERVICE_ERROR";
|
|
15
|
+
readonly AUTH_FAILED: "AUTH_FAILED";
|
|
16
|
+
readonly TOKEN_MISSING: "TOKEN_MISSING";
|
|
13
17
|
readonly TOKEN_INVALID: "TOKEN_INVALID";
|
|
14
|
-
readonly
|
|
15
|
-
readonly
|
|
16
|
-
readonly
|
|
17
|
-
readonly
|
|
18
|
-
readonly
|
|
19
|
-
readonly
|
|
20
|
-
readonly
|
|
21
|
-
readonly
|
|
22
|
-
readonly
|
|
23
|
-
readonly
|
|
24
|
-
readonly
|
|
25
|
-
readonly
|
|
26
|
-
readonly
|
|
27
|
-
readonly
|
|
28
|
-
readonly
|
|
29
|
-
readonly
|
|
30
|
-
readonly
|
|
18
|
+
readonly TOKEN_EXPIRED: "TOKEN_EXPIRED";
|
|
19
|
+
readonly CRYPTO_ERROR: "CRYPTO_ERROR";
|
|
20
|
+
readonly CRYPTO_INTEGRITY_ERROR: "CRYPTO_INTEGRITY_ERROR";
|
|
21
|
+
readonly CACHE_ERROR: "CACHE_ERROR";
|
|
22
|
+
readonly CACHE_CONNECTION_FAILED: "CACHE_CONNECTION_FAILED";
|
|
23
|
+
readonly DATABASE_ERROR: "DATABASE_ERROR";
|
|
24
|
+
readonly DATABASE_CONNECTION_FAILED: "DATABASE_CONNECTION_FAILED";
|
|
25
|
+
readonly DATABASE_CONSTRAINT_VIOLATION: "DATABASE_CONSTRAINT_VIOLATION";
|
|
26
|
+
readonly MESSAGE_BROKER_ERROR: "MESSAGE_BROKER_ERROR";
|
|
27
|
+
readonly MESSAGE_PUBLISH_FAILED: "MESSAGE_PUBLISH_FAILED";
|
|
28
|
+
readonly FILE_ERROR: "FILE_ERROR";
|
|
29
|
+
readonly FILE_NOT_FOUND: "FILE_NOT_FOUND";
|
|
30
|
+
readonly FILE_UPLOAD_FAILED: "FILE_UPLOAD_FAILED";
|
|
31
|
+
readonly FILE_TOO_LARGE: "FILE_TOO_LARGE";
|
|
32
|
+
readonly CONFIG_ERROR: "CONFIG_ERROR";
|
|
33
|
+
readonly CONFIG_MISSING: "CONFIG_MISSING";
|
|
34
|
+
readonly TIMEOUT_ERROR: "TIMEOUT_ERROR";
|
|
35
|
+
readonly RESOURCE_EXHAUSTED: "RESOURCE_EXHAUSTED";
|
|
31
36
|
};
|
|
32
|
-
export type ErrorCode = keyof typeof ERROR_CODES;
|
|
37
|
+
export type ErrorCode = (typeof ERROR_CODES)[keyof typeof ERROR_CODES];
|
|
@@ -2,33 +2,68 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ERROR_CODES = void 0;
|
|
4
4
|
exports.ERROR_CODES = {
|
|
5
|
+
/* ------------------------------------------------------------------ */
|
|
6
|
+
/* 🧱 Common / Generic */
|
|
7
|
+
/* ------------------------------------------------------------------ */
|
|
5
8
|
BAD_REQUEST: 'BAD_REQUEST',
|
|
9
|
+
VALIDATION_FAILED: 'VALIDATION_FAILED',
|
|
6
10
|
UNAUTHORIZED: 'UNAUTHORIZED',
|
|
7
11
|
FORBIDDEN: 'FORBIDDEN',
|
|
8
12
|
NOT_FOUND: 'NOT_FOUND',
|
|
9
13
|
CONFLICT: 'CONFLICT',
|
|
10
|
-
VALIDATION_FAILED: 'VALIDATION_FAILED',
|
|
11
|
-
RATE_LIMIT_EXCEEDED: 'RATE_LIMIT_EXCEEDED',
|
|
12
|
-
INTERNAL_SERVER_ERROR: 'INTERNAL_SERVER_ERROR',
|
|
13
|
-
CRYPTO_INTEGRITY_ERROR: 'CRYPTO_INTEGRITY_ERROR',
|
|
14
14
|
TOO_MANY_REQUESTS: 'TOO_MANY_REQUESTS',
|
|
15
|
-
|
|
15
|
+
INTERNAL_SERVER_ERROR: 'INTERNAL_SERVER_ERROR',
|
|
16
|
+
SERVICE_UNAVAILABLE: 'SERVICE_UNAVAILABLE',
|
|
17
|
+
DEPENDENCY_FAILURE: 'DEPENDENCY_FAILURE',
|
|
18
|
+
/* ------------------------------------------------------------------ */
|
|
19
|
+
/* 🌐 HTTP / Network */
|
|
20
|
+
/* ------------------------------------------------------------------ */
|
|
21
|
+
HTTP_ERROR: 'HTTP_ERROR',
|
|
22
|
+
HTTP_TIMEOUT: 'HTTP_TIMEOUT',
|
|
23
|
+
UPSTREAM_SERVICE_ERROR: 'UPSTREAM_SERVICE_ERROR',
|
|
24
|
+
/* ------------------------------------------------------------------ */
|
|
25
|
+
/* 🔐 Authentication / Authorization */
|
|
26
|
+
/* ------------------------------------------------------------------ */
|
|
27
|
+
AUTH_FAILED: 'AUTH_FAILED',
|
|
28
|
+
TOKEN_MISSING: 'TOKEN_MISSING',
|
|
16
29
|
TOKEN_INVALID: 'TOKEN_INVALID',
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
TOKEN_EXPIRED: 'TOKEN_EXPIRED',
|
|
31
|
+
/* ------------------------------------------------------------------ */
|
|
32
|
+
/* 🔑 Cryptography / Security */
|
|
33
|
+
/* ------------------------------------------------------------------ */
|
|
34
|
+
CRYPTO_ERROR: 'CRYPTO_ERROR',
|
|
35
|
+
CRYPTO_INTEGRITY_ERROR: 'CRYPTO_INTEGRITY_ERROR',
|
|
36
|
+
/* ------------------------------------------------------------------ */
|
|
37
|
+
/* 💾 Cache */
|
|
38
|
+
/* ------------------------------------------------------------------ */
|
|
39
|
+
CACHE_ERROR: 'CACHE_ERROR',
|
|
40
|
+
CACHE_CONNECTION_FAILED: 'CACHE_CONNECTION_FAILED',
|
|
41
|
+
/* ------------------------------------------------------------------ */
|
|
42
|
+
/* 🗄️ Database */
|
|
43
|
+
/* ------------------------------------------------------------------ */
|
|
44
|
+
DATABASE_ERROR: 'DATABASE_ERROR',
|
|
45
|
+
DATABASE_CONNECTION_FAILED: 'DATABASE_CONNECTION_FAILED',
|
|
46
|
+
DATABASE_CONSTRAINT_VIOLATION: 'DATABASE_CONSTRAINT_VIOLATION',
|
|
47
|
+
/* ------------------------------------------------------------------ */
|
|
48
|
+
/* 📩 Messaging / Queues */
|
|
49
|
+
/* ------------------------------------------------------------------ */
|
|
50
|
+
MESSAGE_BROKER_ERROR: 'MESSAGE_BROKER_ERROR',
|
|
51
|
+
MESSAGE_PUBLISH_FAILED: 'MESSAGE_PUBLISH_FAILED',
|
|
52
|
+
/* ------------------------------------------------------------------ */
|
|
53
|
+
/* 📁 File / Storage */
|
|
54
|
+
/* ------------------------------------------------------------------ */
|
|
55
|
+
FILE_ERROR: 'FILE_ERROR',
|
|
56
|
+
FILE_NOT_FOUND: 'FILE_NOT_FOUND',
|
|
57
|
+
FILE_UPLOAD_FAILED: 'FILE_UPLOAD_FAILED',
|
|
58
|
+
FILE_TOO_LARGE: 'FILE_TOO_LARGE',
|
|
59
|
+
/* ------------------------------------------------------------------ */
|
|
60
|
+
/* ⚙️ Configuration / Environment */
|
|
61
|
+
/* ------------------------------------------------------------------ */
|
|
62
|
+
CONFIG_ERROR: 'CONFIG_ERROR',
|
|
63
|
+
CONFIG_MISSING: 'CONFIG_MISSING',
|
|
64
|
+
/* ------------------------------------------------------------------ */
|
|
65
|
+
/* ⏱️ Timeouts / Resources */
|
|
66
|
+
/* ------------------------------------------------------------------ */
|
|
67
|
+
TIMEOUT_ERROR: 'TIMEOUT_ERROR',
|
|
68
|
+
RESOURCE_EXHAUSTED: 'RESOURCE_EXHAUSTED',
|
|
34
69
|
};
|
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type ErrorCode } from './errorCodes';
|
|
2
|
+
/**
|
|
3
|
+
* Canonical error messages mapped to ERROR_CODES
|
|
4
|
+
* - Human-readable
|
|
5
|
+
* - Safe for API responses
|
|
6
|
+
* - Useful for logs
|
|
7
|
+
* - Can be overridden by services if needed
|
|
8
|
+
*/
|
|
2
9
|
export declare const ERROR_MESSAGES: Record<ErrorCode, string>;
|
|
@@ -1,34 +1,77 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ERROR_MESSAGES = void 0;
|
|
4
|
+
const errorCodes_1 = require("./errorCodes");
|
|
5
|
+
/**
|
|
6
|
+
* Canonical error messages mapped to ERROR_CODES
|
|
7
|
+
* - Human-readable
|
|
8
|
+
* - Safe for API responses
|
|
9
|
+
* - Useful for logs
|
|
10
|
+
* - Can be overridden by services if needed
|
|
11
|
+
*/
|
|
4
12
|
exports.ERROR_MESSAGES = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
TOO_MANY_REQUESTS: 'Too many requests',
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
13
|
+
/* ------------------------------------------------------------------ */
|
|
14
|
+
/* 🧱 Common / Generic */
|
|
15
|
+
/* ------------------------------------------------------------------ */
|
|
16
|
+
[errorCodes_1.ERROR_CODES.BAD_REQUEST]: 'The request is invalid or malformed.',
|
|
17
|
+
[errorCodes_1.ERROR_CODES.VALIDATION_FAILED]: 'Request validation failed.',
|
|
18
|
+
[errorCodes_1.ERROR_CODES.UNAUTHORIZED]: 'Authentication is required.',
|
|
19
|
+
[errorCodes_1.ERROR_CODES.FORBIDDEN]: 'You do not have permission to perform this action.',
|
|
20
|
+
[errorCodes_1.ERROR_CODES.NOT_FOUND]: 'The requested resource was not found.',
|
|
21
|
+
[errorCodes_1.ERROR_CODES.CONFLICT]: 'The request could not be completed due to a conflict.',
|
|
22
|
+
[errorCodes_1.ERROR_CODES.TOO_MANY_REQUESTS]: 'Too many requests. Please try again later.',
|
|
23
|
+
[errorCodes_1.ERROR_CODES.INTERNAL_SERVER_ERROR]: 'An unexpected internal error occurred.',
|
|
24
|
+
[errorCodes_1.ERROR_CODES.SERVICE_UNAVAILABLE]: 'The service is currently unavailable.',
|
|
25
|
+
[errorCodes_1.ERROR_CODES.DEPENDENCY_FAILURE]: 'A dependent service failed to respond.',
|
|
26
|
+
/* ------------------------------------------------------------------ */
|
|
27
|
+
/* 🌐 HTTP / Network */
|
|
28
|
+
/* ------------------------------------------------------------------ */
|
|
29
|
+
[errorCodes_1.ERROR_CODES.HTTP_ERROR]: 'An HTTP error occurred while processing the request.',
|
|
30
|
+
[errorCodes_1.ERROR_CODES.HTTP_TIMEOUT]: 'The request timed out.',
|
|
31
|
+
[errorCodes_1.ERROR_CODES.UPSTREAM_SERVICE_ERROR]: 'An upstream service returned an error.',
|
|
32
|
+
/* ------------------------------------------------------------------ */
|
|
33
|
+
/* 🔐 Authentication / Authorization */
|
|
34
|
+
/* ------------------------------------------------------------------ */
|
|
35
|
+
[errorCodes_1.ERROR_CODES.AUTH_FAILED]: 'Authentication failed.',
|
|
36
|
+
[errorCodes_1.ERROR_CODES.TOKEN_MISSING]: 'Authentication token is missing.',
|
|
37
|
+
[errorCodes_1.ERROR_CODES.TOKEN_INVALID]: 'Authentication token is invalid.',
|
|
38
|
+
[errorCodes_1.ERROR_CODES.TOKEN_EXPIRED]: 'Authentication token has expired.',
|
|
39
|
+
/* ------------------------------------------------------------------ */
|
|
40
|
+
/* 🔑 Cryptography / Security */
|
|
41
|
+
/* ------------------------------------------------------------------ */
|
|
42
|
+
[errorCodes_1.ERROR_CODES.CRYPTO_ERROR]: 'A cryptographic operation failed.',
|
|
43
|
+
[errorCodes_1.ERROR_CODES.CRYPTO_INTEGRITY_ERROR]: 'Data integrity verification failed.',
|
|
44
|
+
/* ------------------------------------------------------------------ */
|
|
45
|
+
/* 💾 Cache */
|
|
46
|
+
/* ------------------------------------------------------------------ */
|
|
47
|
+
[errorCodes_1.ERROR_CODES.CACHE_ERROR]: 'A cache operation failed.',
|
|
48
|
+
[errorCodes_1.ERROR_CODES.CACHE_CONNECTION_FAILED]: 'Failed to connect to the cache store.',
|
|
49
|
+
/* ------------------------------------------------------------------ */
|
|
50
|
+
/* 🗄️ Database */
|
|
51
|
+
/* ------------------------------------------------------------------ */
|
|
52
|
+
[errorCodes_1.ERROR_CODES.DATABASE_ERROR]: 'A database error occurred.',
|
|
53
|
+
[errorCodes_1.ERROR_CODES.DATABASE_CONNECTION_FAILED]: 'Failed to connect to the database.',
|
|
54
|
+
[errorCodes_1.ERROR_CODES.DATABASE_CONSTRAINT_VIOLATION]: 'The operation violates a database constraint.',
|
|
55
|
+
/* ------------------------------------------------------------------ */
|
|
56
|
+
/* 📩 Messaging / Queues */
|
|
57
|
+
/* ------------------------------------------------------------------ */
|
|
58
|
+
[errorCodes_1.ERROR_CODES.MESSAGE_BROKER_ERROR]: 'A message broker error occurred.',
|
|
59
|
+
[errorCodes_1.ERROR_CODES.MESSAGE_PUBLISH_FAILED]: 'Failed to publish message to the message broker.',
|
|
60
|
+
/* ------------------------------------------------------------------ */
|
|
61
|
+
/* 📁 File / Storage */
|
|
62
|
+
/* ------------------------------------------------------------------ */
|
|
63
|
+
[errorCodes_1.ERROR_CODES.FILE_ERROR]: 'A file operation failed.',
|
|
64
|
+
[errorCodes_1.ERROR_CODES.FILE_NOT_FOUND]: 'The requested file was not found.',
|
|
65
|
+
[errorCodes_1.ERROR_CODES.FILE_UPLOAD_FAILED]: 'File upload failed.',
|
|
66
|
+
[errorCodes_1.ERROR_CODES.FILE_TOO_LARGE]: 'The uploaded file exceeds the allowed size.',
|
|
67
|
+
/* ------------------------------------------------------------------ */
|
|
68
|
+
/* ⚙️ Configuration / Environment */
|
|
69
|
+
/* ------------------------------------------------------------------ */
|
|
70
|
+
[errorCodes_1.ERROR_CODES.CONFIG_ERROR]: 'Configuration error detected.',
|
|
71
|
+
[errorCodes_1.ERROR_CODES.CONFIG_MISSING]: 'Required configuration is missing.',
|
|
72
|
+
/* ------------------------------------------------------------------ */
|
|
73
|
+
/* ⏱️ Timeouts / Resources */
|
|
74
|
+
/* ------------------------------------------------------------------ */
|
|
75
|
+
[errorCodes_1.ERROR_CODES.TIMEOUT_ERROR]: 'The operation timed out.',
|
|
76
|
+
[errorCodes_1.ERROR_CODES.RESOURCE_EXHAUSTED]: 'The system has exhausted required resources.',
|
|
34
77
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { ErrorCode } from '../constants';
|
|
2
2
|
export declare class AppError extends Error {
|
|
3
3
|
statusCode: number;
|
|
4
4
|
isOperational: boolean;
|
|
@@ -8,7 +8,7 @@ export declare class AppError extends Error {
|
|
|
8
8
|
constructor(code: ErrorCode, statusCode?: number, details?: unknown, cause?: Error);
|
|
9
9
|
toJSON(): {
|
|
10
10
|
success: boolean;
|
|
11
|
-
code:
|
|
11
|
+
code: ErrorCode;
|
|
12
12
|
message: string;
|
|
13
13
|
details: {} | null;
|
|
14
14
|
};
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.AppError = void 0;
|
|
4
4
|
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
5
|
-
const
|
|
5
|
+
const src_1 = require("src");
|
|
6
6
|
class AppError extends Error {
|
|
7
7
|
constructor(code, statusCode = response_utils_1.HTTP_STATUS.SERVER_ERROR.INTERNAL_SERVER_ERROR, details, cause) {
|
|
8
|
-
super(
|
|
8
|
+
super(src_1.errorMessageRegistry.resolve(code)); // message comes from mapping
|
|
9
9
|
this.code = code;
|
|
10
10
|
this.statusCode = statusCode;
|
|
11
11
|
this.isOperational = true;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ServiceUnavailableError = void 0;
|
|
4
|
+
const constants_1 = require("src/constants");
|
|
5
|
+
const InternalServerError_1 = require("./InternalServerError");
|
|
6
|
+
class ServiceUnavailableError extends InternalServerError_1.InternalServerError {
|
|
7
|
+
constructor(details, cause) {
|
|
8
|
+
super(constants_1.ERROR_CODES.SERVICE_UNAVAILABLE, details, cause);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
exports.ServiceUnavailableError = ServiceUnavailableError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export * from './AppError';
|
|
2
|
+
export * from './HTTPError';
|
|
3
|
+
export * from './BadRequestError';
|
|
4
|
+
export * from './UnauthorizedError';
|
|
5
|
+
export * from './ForbiddenError';
|
|
6
|
+
export * from './NotFoundError';
|
|
7
|
+
export * from './ConflictError';
|
|
8
|
+
export * from './ValidationError';
|
|
9
|
+
export * from './TooManyRequestsError';
|
|
10
|
+
export * from './TokenExpiredError';
|
|
11
|
+
export * from './TokenMalformedError';
|
|
12
|
+
export * from './InternalServerError';
|
|
13
|
+
export * from './CryptoIntegrityError';
|
|
14
|
+
export * from './ServiceUnavailableError';
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./AppError"), exports);
|
|
18
|
+
__exportStar(require("./HTTPError"), exports);
|
|
19
|
+
// =========================
|
|
20
|
+
// 4xx Client Errors
|
|
21
|
+
// =========================
|
|
22
|
+
__exportStar(require("./BadRequestError"), exports);
|
|
23
|
+
__exportStar(require("./UnauthorizedError"), exports);
|
|
24
|
+
__exportStar(require("./ForbiddenError"), exports);
|
|
25
|
+
__exportStar(require("./NotFoundError"), exports);
|
|
26
|
+
__exportStar(require("./ConflictError"), exports);
|
|
27
|
+
__exportStar(require("./ValidationError"), exports);
|
|
28
|
+
__exportStar(require("./TooManyRequestsError"), exports);
|
|
29
|
+
// =========================
|
|
30
|
+
// Auth / Token Errors
|
|
31
|
+
// =========================
|
|
32
|
+
__exportStar(require("./TokenExpiredError"), exports);
|
|
33
|
+
__exportStar(require("./TokenMalformedError"), exports);
|
|
34
|
+
// =========================
|
|
35
|
+
// 5xx Server Errors
|
|
36
|
+
// =========================
|
|
37
|
+
__exportStar(require("./InternalServerError"), exports);
|
|
38
|
+
__exportStar(require("./CryptoIntegrityError"), exports);
|
|
39
|
+
__exportStar(require("./ServiceUnavailableError"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ExpressResponder } from '@naman_deep_singh/response-utils';
|
|
2
|
+
import { type ErrorCode } from 'src/constants';
|
|
3
|
+
import type { AppError } from 'src/error/AppError';
|
|
4
|
+
export declare class ErrorMessageRegistry {
|
|
5
|
+
private static instance;
|
|
6
|
+
private readonly registry;
|
|
7
|
+
private constructor();
|
|
8
|
+
/** Singleton accessor */
|
|
9
|
+
static getInstance(): ErrorMessageRegistry;
|
|
10
|
+
/** Register or override messages */
|
|
11
|
+
register(messages: Record<string, string>): void;
|
|
12
|
+
/** Resolve a message for a given error code */
|
|
13
|
+
resolve(code: ErrorCode | string, defaultMessage?: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Map an AppError to an ExpressResponder
|
|
16
|
+
* Centralizes error-to-HTTP mapping
|
|
17
|
+
*/
|
|
18
|
+
mapAppErrorToResponder(responder: ExpressResponder<unknown>, err: AppError): any;
|
|
19
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ErrorMessageRegistry = void 0;
|
|
4
|
+
const response_utils_1 = require("@naman_deep_singh/response-utils");
|
|
5
|
+
const constants_1 = require("src/constants");
|
|
6
|
+
class ErrorMessageRegistry {
|
|
7
|
+
constructor() {
|
|
8
|
+
this.registry = new Map();
|
|
9
|
+
// Initialize with default messages
|
|
10
|
+
Object.entries(constants_1.ERROR_MESSAGES).forEach(([code, message]) => {
|
|
11
|
+
this.registry.set(code, message);
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
/** Singleton accessor */
|
|
15
|
+
static getInstance() {
|
|
16
|
+
if (!ErrorMessageRegistry.instance) {
|
|
17
|
+
ErrorMessageRegistry.instance = new ErrorMessageRegistry();
|
|
18
|
+
}
|
|
19
|
+
return ErrorMessageRegistry.instance;
|
|
20
|
+
}
|
|
21
|
+
/** Register or override messages */
|
|
22
|
+
register(messages) {
|
|
23
|
+
for (const [code, message] of Object.entries(messages)) {
|
|
24
|
+
this.registry.set(code, message);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** Resolve a message for a given error code */
|
|
28
|
+
resolve(code, defaultMessage = 'Unexpected error') {
|
|
29
|
+
return this.registry.get(code) ?? defaultMessage;
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Map an AppError to an ExpressResponder
|
|
33
|
+
* Centralizes error-to-HTTP mapping
|
|
34
|
+
*/
|
|
35
|
+
mapAppErrorToResponder(responder, err) {
|
|
36
|
+
switch (err.statusCode) {
|
|
37
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.BAD_REQUEST:
|
|
38
|
+
return responder.badRequest(this.resolve(err.code), {
|
|
39
|
+
details: err.details,
|
|
40
|
+
});
|
|
41
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.UNAUTHORIZED:
|
|
42
|
+
return responder.unauthorized(this.resolve(err.code));
|
|
43
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.FORBIDDEN:
|
|
44
|
+
return responder.forbidden(this.resolve(err.code));
|
|
45
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.NOT_FOUND:
|
|
46
|
+
return responder.notFound(this.resolve(err.code));
|
|
47
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.CONFLICT:
|
|
48
|
+
return responder.conflict(this.resolve(err.code));
|
|
49
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.UNPROCESSABLE_ENTITY:
|
|
50
|
+
return responder.unprocessableEntity(this.resolve(err.code), {
|
|
51
|
+
details: err.details,
|
|
52
|
+
});
|
|
53
|
+
case response_utils_1.HTTP_STATUS.CLIENT_ERROR.TOO_MANY_REQUESTS:
|
|
54
|
+
return responder.tooManyRequests(this.resolve(err.code));
|
|
55
|
+
default:
|
|
56
|
+
// Any other status maps to generic server error
|
|
57
|
+
return responder.serverError(this.resolve(err.code), {
|
|
58
|
+
details: err.details,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.ErrorMessageRegistry = ErrorMessageRegistry;
|