@naman_deep_singh/errors-utils 1.1.1 → 1.3.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.
Files changed (97) hide show
  1. package/README.md +157 -82
  2. package/dist/cjs/constants/errorCodes.d.ts +32 -0
  3. package/dist/cjs/constants/errorCodes.js +34 -0
  4. package/dist/cjs/constants/errorMessages.d.ts +2 -0
  5. package/dist/cjs/constants/errorMessages.js +34 -0
  6. package/dist/cjs/constants/index.d.ts +2 -0
  7. package/dist/cjs/constants/index.js +18 -0
  8. package/dist/cjs/error/AppError.d.ts +10 -1
  9. package/dist/cjs/error/AppError.js +15 -2
  10. package/dist/cjs/error/BadRequestError.d.ts +1 -1
  11. package/dist/cjs/error/BadRequestError.js +4 -2
  12. package/dist/cjs/error/ConflictError.d.ts +1 -1
  13. package/dist/cjs/error/ConflictError.js +4 -2
  14. package/dist/cjs/error/CryptoIntegrityError.d.ts +4 -0
  15. package/dist/cjs/error/CryptoIntegrityError.js +11 -0
  16. package/dist/cjs/error/ForbiddenError.d.ts +1 -1
  17. package/dist/cjs/error/ForbiddenError.js +4 -2
  18. package/dist/cjs/error/HTTPError.d.ts +3 -2
  19. package/dist/cjs/error/HTTPError.js +3 -3
  20. package/dist/cjs/error/InternalServerError.d.ts +2 -1
  21. package/dist/cjs/error/InternalServerError.js +4 -2
  22. package/dist/cjs/error/NotFoundError.d.ts +1 -1
  23. package/dist/cjs/error/NotFoundError.js +4 -2
  24. package/dist/cjs/error/RateLimitError.d.ts +4 -0
  25. package/dist/cjs/error/RateLimitError.js +11 -0
  26. package/dist/cjs/error/TokenExpiredError.d.ts +4 -0
  27. package/dist/cjs/error/TokenExpiredError.js +12 -0
  28. package/dist/cjs/error/TokenMalformedError.d.ts +4 -0
  29. package/dist/cjs/error/TokenMalformedError.js +12 -0
  30. package/dist/cjs/error/TooManyRequestsError.d.ts +5 -0
  31. package/dist/cjs/error/TooManyRequestsError.js +12 -0
  32. package/dist/cjs/error/UnauthorizedError.d.ts +2 -1
  33. package/dist/cjs/error/UnauthorizedError.js +4 -2
  34. package/dist/cjs/error/ValidationError.d.ts +1 -1
  35. package/dist/cjs/error/ValidationError.js +4 -2
  36. package/dist/cjs/middleware/express/errorConverter.d.ts +1 -1
  37. package/dist/cjs/middleware/express/errorConverter.js +23 -11
  38. package/dist/cjs/middleware/express/errorHandler.d.ts +1 -1
  39. package/dist/cjs/middleware/express/errorHandler.js +7 -6
  40. package/dist/esm/constants/errorCodes.d.ts +32 -0
  41. package/dist/esm/constants/errorCodes.js +31 -0
  42. package/dist/esm/constants/errorMessages.d.ts +2 -0
  43. package/dist/esm/constants/errorMessages.js +31 -0
  44. package/dist/esm/constants/index.d.ts +2 -0
  45. package/dist/esm/constants/index.js +2 -0
  46. package/dist/esm/error/AppError.d.ts +10 -1
  47. package/dist/esm/error/AppError.js +15 -2
  48. package/dist/esm/error/BadRequestError.d.ts +1 -1
  49. package/dist/esm/error/BadRequestError.js +4 -2
  50. package/dist/esm/error/ConflictError.d.ts +1 -1
  51. package/dist/esm/error/ConflictError.js +4 -2
  52. package/dist/esm/error/CryptoIntegrityError.d.ts +4 -0
  53. package/dist/esm/error/CryptoIntegrityError.js +7 -0
  54. package/dist/esm/error/ForbiddenError.d.ts +1 -1
  55. package/dist/esm/error/ForbiddenError.js +4 -2
  56. package/dist/esm/error/HTTPError.d.ts +3 -2
  57. package/dist/esm/error/HTTPError.js +3 -3
  58. package/dist/esm/error/InternalServerError.d.ts +2 -1
  59. package/dist/esm/error/InternalServerError.js +4 -2
  60. package/dist/esm/error/NotFoundError.d.ts +1 -1
  61. package/dist/esm/error/NotFoundError.js +4 -2
  62. package/dist/esm/error/RateLimitError.d.ts +4 -0
  63. package/dist/esm/error/RateLimitError.js +7 -0
  64. package/dist/esm/error/TokenExpiredError.d.ts +4 -0
  65. package/dist/esm/error/TokenExpiredError.js +8 -0
  66. package/dist/esm/error/TokenMalformedError.d.ts +4 -0
  67. package/dist/esm/error/TokenMalformedError.js +8 -0
  68. package/dist/esm/error/TooManyRequestsError.d.ts +5 -0
  69. package/dist/esm/error/TooManyRequestsError.js +8 -0
  70. package/dist/esm/error/UnauthorizedError.d.ts +2 -1
  71. package/dist/esm/error/UnauthorizedError.js +4 -2
  72. package/dist/esm/error/ValidationError.d.ts +1 -1
  73. package/dist/esm/error/ValidationError.js +4 -2
  74. package/dist/esm/middleware/express/errorConverter.d.ts +1 -1
  75. package/dist/esm/middleware/express/errorConverter.js +23 -11
  76. package/dist/esm/middleware/express/errorHandler.d.ts +1 -1
  77. package/dist/esm/middleware/express/errorHandler.js +7 -6
  78. package/dist/types/constants/errorCodes.d.ts +32 -0
  79. package/dist/types/constants/errorMessages.d.ts +2 -0
  80. package/dist/types/constants/index.d.ts +2 -0
  81. package/dist/types/error/AppError.d.ts +10 -1
  82. package/dist/types/error/BadRequestError.d.ts +1 -1
  83. package/dist/types/error/ConflictError.d.ts +1 -1
  84. package/dist/types/error/CryptoIntegrityError.d.ts +4 -0
  85. package/dist/types/error/ForbiddenError.d.ts +1 -1
  86. package/dist/types/error/HTTPError.d.ts +3 -2
  87. package/dist/types/error/InternalServerError.d.ts +2 -1
  88. package/dist/types/error/NotFoundError.d.ts +1 -1
  89. package/dist/types/error/RateLimitError.d.ts +4 -0
  90. package/dist/types/error/TokenExpiredError.d.ts +4 -0
  91. package/dist/types/error/TokenMalformedError.d.ts +4 -0
  92. package/dist/types/error/TooManyRequestsError.d.ts +5 -0
  93. package/dist/types/error/UnauthorizedError.d.ts +2 -1
  94. package/dist/types/error/ValidationError.d.ts +1 -1
  95. package/dist/types/middleware/express/errorConverter.d.ts +1 -1
  96. package/dist/types/middleware/express/errorHandler.d.ts +1 -1
  97. package/package.json +1 -1
package/README.md CHANGED
@@ -1,125 +1,200 @@
1
- # @naman_deep_singh/errors-utils
1
+ @naman_deep_singh/errors-utils
2
2
 
3
- **Version:** 1.1.1
3
+ Version: 1.3.1
4
4
 
5
- Standardized error classes and Express middleware for consistent error handling with TypeScript.
5
+ A standardized, code-driven error handling system for TypeScript and Express applications, providing consistent error identity, responses, and middleware integration.
6
6
 
7
- ## 🚀 Features
7
+ 🚀 Features
8
8
 
9
- - **Structured Error Classes** - AppError, HTTPError, ValidationError, etc.
10
- - ✅ **Express Middleware** - Error converter and handler
11
- - ✅ **Response Integration** - Works with @naman_deep_singh/response-utils
12
- - ✅ **TypeScript Support** - Full type safety
13
- - ✅ **Consistent Responses** - Standardized error format across your API
9
+ ✅ Structured Error Classes AppError, HTTPError, ValidationError, etc.
14
10
 
15
- ## 📦 Installation
11
+ Strongly-Typed Error Codes — Centralized error identity via constants
16
12
 
17
- ```bash
13
+ ✅ Centralized Error Messages — One source of truth for user-facing messages
14
+
15
+ ✅ Express Middleware — Error converter & global handler
16
+
17
+ ✅ Response Integration — Works seamlessly with @naman_deep_singh/response-utils
18
+
19
+ ✅ TypeScript First — Full type safety & IntelliSense
20
+
21
+ ✅ Consistent API Responses — Unified error shape across services
22
+
23
+ 📦 Installation
18
24
  npm install @naman_deep_singh/errors-utils
19
- ```
20
25
 
21
- ## 🔧 Usage
26
+ 🧠 Design Philosophy
27
+
28
+ This package is code-driven, not message-driven.
29
+
30
+ Errors are identified by stable error codes
31
+
32
+ Messages are resolved internally via centralized mappings
33
+
34
+ API contracts remain stable even if messages change
35
+
36
+ This ensures:
22
37
 
23
- ### Error Classes
38
+ Consistency across microservices
24
39
 
25
- ```typescript
26
- import {
27
- AppError,
28
- BadRequestError,
29
- UnauthorizedError,
40
+ Safe refactoring
41
+
42
+ Better logging, tracing, and observability
43
+
44
+ 🔧 Usage
45
+ Creating Errors (Recommended)
46
+ import {
47
+ BadRequestError,
48
+ UnauthorizedError,
30
49
  NotFoundError,
31
50
  ValidationError,
32
- InternalServerError
33
- } from '@naman_deep_singh/errors-utils';
51
+ InternalServerError,
52
+ ERROR_CODES,
53
+ } from '@naman_deep_singh/errors-utils'
54
+
55
+ throw new BadRequestError(ERROR_CODES.BAD_REQUEST)
56
+
57
+ throw new UnauthorizedError(ERROR_CODES.UNAUTHORIZED)
58
+
59
+ throw new NotFoundError(ERROR_CODES.NOT_FOUND)
60
+
61
+ throw new ValidationError(ERROR_CODES.VALIDATION_FAILED, {
62
+ fields: ['email', 'password'],
63
+ })
64
+
65
+ throw new InternalServerError(ERROR_CODES.INTERNAL_SERVER_ERROR)
66
+
67
+ 🧾 Error Codes & Messages
68
+
69
+ The package exposes a strongly-typed error code system.
34
70
 
35
- // Basic usage
36
- throw new BadRequestError('Invalid input data');
37
- throw new UnauthorizedError('Authentication required');
38
- throw new NotFoundError('User not found');
39
- throw new ValidationError('Email is required');
71
+ import {
72
+ ERROR_CODES,
73
+ ERROR_MESSAGES,
74
+ ErrorCode,
75
+ } from '@naman_deep_singh/errors-utils'
40
76
 
41
- // With details
42
- throw new BadRequestError('Validation failed', {
43
- fields: ['email', 'password']
44
- });
45
- ```
77
+ Exports
46
78
 
47
- ### Express Middleware
79
+ ERROR_CODES Canonical list of all supported error codes
48
80
 
49
- ```typescript
50
- import express from 'express';
51
- import { expressErrorHandler, errorConverter } from '@naman_deep_singh/errors-utils';
81
+ ERROR_MESSAGES — Mapping of error codes → user-facing messages
52
82
 
53
- const app = express();
83
+ ErrorCode Union type of all valid error codes
54
84
 
55
- // Convert unknown errors to AppError
56
- app.use(errorConverter);
85
+ Why Error Codes?
86
+
87
+ ✅ Consistent error identity across services
88
+
89
+ ✅ Centralized message management
90
+
91
+ ✅ Safer API contracts
92
+
93
+ ✅ Improved logging & observability
94
+
95
+ 🌐 Express Middleware
96
+
97
+ This package provides Express-specific middleware under the hood
98
+ and exposes a clean public API.
99
+
100
+ import express from 'express'
101
+ import {
102
+ errorConverter,
103
+ expressErrorHandler,
104
+ ValidationError,
105
+ ERROR_CODES,
106
+ } from '@naman_deep_singh/errors-utils'
107
+
108
+ const app = express()
109
+
110
+ // Convert unknown / third-party errors → AppError
111
+ app.use(errorConverter)
57
112
 
58
113
  // Handle all errors consistently
59
- app.use(expressErrorHandler);
114
+ app.use(expressErrorHandler)
60
115
 
61
- // In your routes
62
116
  app.post('/users', (req, res) => {
63
117
  if (!req.body.email) {
64
- throw new BadRequestError('Email is required');
118
+ throw new ValidationError(ERROR_CODES.VALIDATION_FAILED)
65
119
  }
66
- // Error will be caught and formatted automatically
67
- });
68
- ```
120
+ })
69
121
 
70
- ## 🔗 Integration
122
+ Middleware Responsibilities
71
123
 
72
- ### With @naman_deep_singh/server-utils
124
+ errorConverter
73
125
 
74
- ```typescript
75
- import { createServer } from '@naman_deep_singh/server-utils';
76
- import { expressErrorHandler } from '@naman_deep_singh/errors-utils';
126
+ Converts unknown errors into AppError
77
127
 
78
- const server = createServer('My API', '1.0.0');
128
+ Preserves known operational errors
79
129
 
80
- // Replace basic error handler with advanced one
81
- server.app.use(expressErrorHandler);
82
- ```
130
+ expressErrorHandler
83
131
 
84
- ### With @naman_deep_singh/response-utils
132
+ Sends standardized API responses
85
133
 
86
- ```typescript
87
- import { responderMiddleware } from '@naman_deep_singh/response-utils';
134
+ Integrates with @naman_deep_singh/response-utils
88
135
 
89
- server.app.use(responderMiddleware());
90
- server.app.use(expressErrorHandler); // Uses response-utils for consistent format
91
- ```
136
+ Hides internal errors in production
92
137
 
93
- ## 📚 Error Classes
138
+ 🔗 Integration
139
+ With @naman_deep_singh/response-utils
140
+ import { responderMiddleware } from '@naman_deep_singh/response-utils'
141
+ import { expressErrorHandler } from '@naman_deep_singh/errors-utils'
94
142
 
95
- | Class | Status Code | Use Case |
96
- |-------|-------------|----------|
97
- | `AppError` | Custom | Base error class |
98
- | `BadRequestError` | 400 | Invalid input data |
99
- | `UnauthorizedError` | 401 | Authentication failures |
100
- | `ForbiddenError` | 403 | Authorization failures |
101
- | `NotFoundError` | 404 | Resource not found |
102
- | `ConflictError` | 409 | Resource conflicts |
103
- | `ValidationError` | 422 | Input validation errors |
104
- | `InternalServerError` | 500 | Server errors |
143
+ app.use(responderMiddleware())
144
+ app.use(expressErrorHandler)
105
145
 
106
- ## 🎯 Response Format
146
+ With @naman_deep_singh/server-utils
147
+ import { createServer } from '@naman_deep_singh/server-utils'
148
+ import { expressErrorHandler } from '@naman_deep_singh/errors-utils'
107
149
 
108
- All errors produce consistent responses:
150
+ const server = createServer('My API', '1.0.0')
151
+
152
+ server.app.use(expressErrorHandler)
153
+
154
+ 🧠 Custom Errors
155
+
156
+ You can safely extend existing errors:
157
+
158
+ import {
159
+ InternalServerError,
160
+ ERROR_CODES,
161
+ } from '@naman_deep_singh/errors-utils'
162
+
163
+ export class CryptoIntegrityError extends InternalServerError {
164
+ constructor(details?: unknown, cause?: Error) {
165
+ super(ERROR_CODES.CRYPTO_INTEGRITY_ERROR, details, cause)
166
+ }
167
+ }
168
+
169
+ 📚 Available Error Classes
170
+ Class Status Code Use Case
171
+ AppError Custom Base error class
172
+ HTTPError 4xx / 5xx Base HTTP error
173
+ BadRequestError 400 Invalid input
174
+ UnauthorizedError 401 Authentication failures
175
+ TokenExpiredError 401 Expired tokens
176
+ TokenMalformedError 401 Invalid token format
177
+ ForbiddenError 403 Authorization failures
178
+ NotFoundError 404 Resource not found
179
+ ConflictError 409 Resource conflicts
180
+ ValidationError 422 Validation errors
181
+ RateLimitError 429 Rate limiting
182
+ TooManyRequestsError 429 Alias of RateLimitError
183
+ CryptoIntegrityError 500 Crypto validation failure
184
+ InternalServerError 500 Server-side failures
185
+ 🎯 Standard Error Response
186
+
187
+ All errors resolve to a consistent response shape:
109
188
 
110
- ```json
111
189
  {
112
190
  "success": false,
113
- "message": "Error message",
114
- "data": undefined,
115
- "error": {
116
- "message": "Detailed error message",
117
- "details": {...}
118
- },
119
- "meta": null
191
+ "code": "VALIDATION_FAILED",
192
+ "message": "Validation failed",
193
+ "details": {
194
+ "fields": ["email"]
195
+ }
120
196
  }
121
- ```
122
197
 
123
- ## 📄 License
198
+ 📄 License
124
199
 
125
200
  ISC © Naman Deep Singh
@@ -0,0 +1,32 @@
1
+ export declare const ERROR_CODES: {
2
+ readonly BAD_REQUEST: "BAD_REQUEST";
3
+ readonly UNAUTHORIZED: "UNAUTHORIZED";
4
+ readonly FORBIDDEN: "FORBIDDEN";
5
+ readonly NOT_FOUND: "NOT_FOUND";
6
+ 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
+ readonly TOO_MANY_REQUESTS: "TOO_MANY_REQUESTS";
12
+ readonly TOKEN_EXPIRED: "TOKEN_EXPIRED";
13
+ readonly TOKEN_INVALID: "TOKEN_INVALID";
14
+ readonly TOKEN_NOT_PROVIDED: "TOKEN_NOT_PROVIDED";
15
+ readonly TOKEN_NOT_FOUND: "TOKEN_NOT_FOUND";
16
+ readonly TOKEN_NOT_CREATED: "TOKEN_NOT_CREATED";
17
+ readonly TOKEN_NOT_UPDATED: "TOKEN_NOT_UPDATED";
18
+ readonly TOKEN_NOT_DELETED: "TOKEN_NOT_DELETED";
19
+ readonly TOKEN_NOT_VALIDATED: "TOKEN_NOT_VALIDATED";
20
+ readonly TOKEN_NOT_REFRESHED: "TOKEN_NOT_REFRESHED";
21
+ readonly TOKEN_NOT_REVOKED: "TOKEN_NOT_REVOKED";
22
+ readonly TOKEN_NOT_BLACKLISTED: "TOKEN_NOT_BLACKLISTED";
23
+ readonly TOKEN_NOT_WHITELISTED: "TOKEN_NOT_WHITELISTED";
24
+ readonly TOKEN_NOT_DECODED: "TOKEN_NOT_DECODED";
25
+ readonly TOKEN_NOT_ENCODED: "TOKEN_NOT_ENCODED";
26
+ readonly TOKEN_NOT_SIGNED: "TOKEN_NOT_SIGNED";
27
+ readonly TOKEN_NOT_VERIFIED: "TOKEN_NOT_VERIFIED";
28
+ readonly TOKEN_NOT_DECRYPTED: "TOKEN_NOT_DECRYPTED";
29
+ readonly TOKEN_NOT_ENCRYPTED: "TOKEN_NOT_ENCRYPTED";
30
+ readonly TOKEN_NOT_GENERATED: "TOKEN_NOT_GENERATED";
31
+ };
32
+ export type ErrorCode = keyof typeof ERROR_CODES;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ERROR_CODES = void 0;
4
+ exports.ERROR_CODES = {
5
+ BAD_REQUEST: 'BAD_REQUEST',
6
+ UNAUTHORIZED: 'UNAUTHORIZED',
7
+ FORBIDDEN: 'FORBIDDEN',
8
+ NOT_FOUND: 'NOT_FOUND',
9
+ 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
+ TOO_MANY_REQUESTS: 'TOO_MANY_REQUESTS',
15
+ TOKEN_EXPIRED: 'TOKEN_EXPIRED',
16
+ TOKEN_INVALID: 'TOKEN_INVALID',
17
+ TOKEN_NOT_PROVIDED: 'TOKEN_NOT_PROVIDED',
18
+ TOKEN_NOT_FOUND: 'TOKEN_NOT_FOUND',
19
+ TOKEN_NOT_CREATED: 'TOKEN_NOT_CREATED',
20
+ TOKEN_NOT_UPDATED: 'TOKEN_NOT_UPDATED',
21
+ TOKEN_NOT_DELETED: 'TOKEN_NOT_DELETED',
22
+ TOKEN_NOT_VALIDATED: 'TOKEN_NOT_VALIDATED',
23
+ TOKEN_NOT_REFRESHED: 'TOKEN_NOT_REFRESHED',
24
+ TOKEN_NOT_REVOKED: 'TOKEN_NOT_REVOKED',
25
+ TOKEN_NOT_BLACKLISTED: 'TOKEN_NOT_BLACKLISTED',
26
+ TOKEN_NOT_WHITELISTED: 'TOKEN_NOT_WHITELISTED',
27
+ TOKEN_NOT_DECODED: 'TOKEN_NOT_DECODED',
28
+ TOKEN_NOT_ENCODED: 'TOKEN_NOT_ENCODED',
29
+ TOKEN_NOT_SIGNED: 'TOKEN_NOT_SIGNED',
30
+ TOKEN_NOT_VERIFIED: 'TOKEN_NOT_VERIFIED',
31
+ TOKEN_NOT_DECRYPTED: 'TOKEN_NOT_DECRYPTED',
32
+ TOKEN_NOT_ENCRYPTED: 'TOKEN_NOT_ENCRYPTED',
33
+ TOKEN_NOT_GENERATED: 'TOKEN_NOT_GENERATED',
34
+ };
@@ -0,0 +1,2 @@
1
+ import type { ErrorCode } from './errorCodes';
2
+ export declare const ERROR_MESSAGES: Record<ErrorCode, string>;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ERROR_MESSAGES = void 0;
4
+ exports.ERROR_MESSAGES = {
5
+ BAD_REQUEST: 'Bad request',
6
+ UNAUTHORIZED: 'Unauthorized',
7
+ FORBIDDEN: 'Forbidden',
8
+ NOT_FOUND: 'Resource not found',
9
+ CONFLICT: 'Conflict occurred',
10
+ VALIDATION_FAILED: 'Validation failed',
11
+ RATE_LIMIT_EXCEEDED: 'Too many requests',
12
+ INTERNAL_SERVER_ERROR: 'Internal server error',
13
+ CRYPTO_INTEGRITY_ERROR: 'Crypto integrity error',
14
+ TOO_MANY_REQUESTS: 'Too many requests',
15
+ TOKEN_EXPIRED: 'Token expired',
16
+ TOKEN_INVALID: 'Invalid token',
17
+ TOKEN_NOT_PROVIDED: 'Token not provided',
18
+ TOKEN_NOT_FOUND: 'Token not found',
19
+ TOKEN_NOT_CREATED: 'Token not created',
20
+ TOKEN_NOT_UPDATED: 'Token not updated',
21
+ TOKEN_NOT_DELETED: 'Token not deleted',
22
+ TOKEN_NOT_VALIDATED: 'Token not validated',
23
+ TOKEN_NOT_REFRESHED: 'Token not refreshed',
24
+ TOKEN_NOT_REVOKED: 'Token not revoked',
25
+ TOKEN_NOT_BLACKLISTED: 'Token not blacklisted',
26
+ TOKEN_NOT_WHITELISTED: 'Token not whitelisted',
27
+ TOKEN_NOT_DECODED: 'Token not decoded',
28
+ TOKEN_NOT_ENCODED: 'Token not encoded',
29
+ TOKEN_NOT_SIGNED: 'Token not signed',
30
+ TOKEN_NOT_VERIFIED: 'Token not verified',
31
+ TOKEN_NOT_DECRYPTED: 'Token not decrypted',
32
+ TOKEN_NOT_ENCRYPTED: 'Token not encrypted',
33
+ TOKEN_NOT_GENERATED: 'Token not generated',
34
+ };
@@ -0,0 +1,2 @@
1
+ export * from './errorCodes';
2
+ export * from './errorMessages';
@@ -0,0 +1,18 @@
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("./errorCodes"), exports);
18
+ __exportStar(require("./errorMessages"), exports);
@@ -1,6 +1,15 @@
1
+ import { type ErrorCode } from '../constants';
1
2
  export declare class AppError extends Error {
2
3
  statusCode: number;
3
4
  isOperational: boolean;
5
+ code: ErrorCode;
4
6
  details?: unknown;
5
- constructor(message: string, statusCode?: number, details?: unknown);
7
+ cause?: Error;
8
+ constructor(code: ErrorCode, statusCode?: number, details?: unknown, cause?: Error);
9
+ toJSON(): {
10
+ success: boolean;
11
+ code: "BAD_REQUEST" | "UNAUTHORIZED" | "FORBIDDEN" | "NOT_FOUND" | "CONFLICT" | "VALIDATION_FAILED" | "RATE_LIMIT_EXCEEDED" | "INTERNAL_SERVER_ERROR" | "CRYPTO_INTEGRITY_ERROR" | "TOO_MANY_REQUESTS" | "TOKEN_EXPIRED" | "TOKEN_INVALID" | "TOKEN_NOT_PROVIDED" | "TOKEN_NOT_FOUND" | "TOKEN_NOT_CREATED" | "TOKEN_NOT_UPDATED" | "TOKEN_NOT_DELETED" | "TOKEN_NOT_VALIDATED" | "TOKEN_NOT_REFRESHED" | "TOKEN_NOT_REVOKED" | "TOKEN_NOT_BLACKLISTED" | "TOKEN_NOT_WHITELISTED" | "TOKEN_NOT_DECODED" | "TOKEN_NOT_ENCODED" | "TOKEN_NOT_SIGNED" | "TOKEN_NOT_VERIFIED" | "TOKEN_NOT_DECRYPTED" | "TOKEN_NOT_ENCRYPTED" | "TOKEN_NOT_GENERATED";
12
+ message: string;
13
+ details: {} | null;
14
+ };
6
15
  }
@@ -1,13 +1,26 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AppError = void 0;
4
+ const response_utils_1 = require("@naman_deep_singh/response-utils");
5
+ const constants_1 = require("../constants");
4
6
  class AppError extends Error {
5
- constructor(message, statusCode = 500, details) {
6
- super(message);
7
+ constructor(code, statusCode = response_utils_1.HTTP_STATUS.SERVER_ERROR.INTERNAL_SERVER_ERROR, details, cause) {
8
+ super(constants_1.ERROR_MESSAGES[code]); // message comes from mapping
9
+ this.code = code;
7
10
  this.statusCode = statusCode;
8
11
  this.isOperational = true;
9
12
  this.details = details;
13
+ if (cause)
14
+ this.cause = cause;
10
15
  Error.captureStackTrace(this, this.constructor);
11
16
  }
17
+ toJSON() {
18
+ return {
19
+ success: false,
20
+ code: this.code,
21
+ message: this.message,
22
+ details: this.details ?? null,
23
+ };
24
+ }
12
25
  }
13
26
  exports.AppError = AppError;
@@ -1,4 +1,4 @@
1
1
  import { HTTPError } from './HTTPError';
2
2
  export declare class BadRequestError extends HTTPError {
3
- constructor(message?: string, details?: unknown);
3
+ constructor(details?: unknown, cause?: Error);
4
4
  }
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.BadRequestError = void 0;
4
+ const response_utils_1 = require("@naman_deep_singh/response-utils");
5
+ const constants_1 = require("../constants");
4
6
  const HTTPError_1 = require("./HTTPError");
5
7
  class BadRequestError extends HTTPError_1.HTTPError {
6
- constructor(message = 'Bad Request', details) {
7
- super(message, 400, details);
8
+ constructor(details, cause) {
9
+ super(constants_1.ERROR_CODES.BAD_REQUEST, response_utils_1.HTTP_STATUS.CLIENT_ERROR.BAD_REQUEST, details, cause);
8
10
  }
9
11
  }
10
12
  exports.BadRequestError = BadRequestError;
@@ -1,4 +1,4 @@
1
1
  import { HTTPError } from './HTTPError';
2
2
  export declare class ConflictError extends HTTPError {
3
- constructor(message?: string, details?: unknown);
3
+ constructor(details?: unknown, cause?: Error);
4
4
  }
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ConflictError = void 0;
4
+ const response_utils_1 = require("@naman_deep_singh/response-utils");
5
+ const constants_1 = require("src/constants");
4
6
  const HTTPError_1 = require("./HTTPError");
5
7
  class ConflictError extends HTTPError_1.HTTPError {
6
- constructor(message = 'Conflict', details) {
7
- super(message, 409, details);
8
+ constructor(details, cause) {
9
+ super(constants_1.ERROR_CODES.CONFLICT, response_utils_1.HTTP_STATUS.CLIENT_ERROR.CONFLICT, details, cause);
8
10
  }
9
11
  }
10
12
  exports.ConflictError = ConflictError;
@@ -0,0 +1,4 @@
1
+ import { InternalServerError } from './InternalServerError';
2
+ export declare class CryptoIntegrityError extends InternalServerError {
3
+ constructor(details?: unknown, cause?: Error);
4
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CryptoIntegrityError = void 0;
4
+ const constants_1 = require("src/constants");
5
+ const InternalServerError_1 = require("./InternalServerError");
6
+ class CryptoIntegrityError extends InternalServerError_1.InternalServerError {
7
+ constructor(details, cause) {
8
+ super(constants_1.ERROR_CODES.CRYPTO_INTEGRITY_ERROR, details, cause);
9
+ }
10
+ }
11
+ exports.CryptoIntegrityError = CryptoIntegrityError;
@@ -1,4 +1,4 @@
1
1
  import { HTTPError } from './HTTPError';
2
2
  export declare class ForbiddenError extends HTTPError {
3
- constructor(message?: string, details?: unknown);
3
+ constructor(details?: unknown, cause?: Error);
4
4
  }
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ForbiddenError = void 0;
4
+ const response_utils_1 = require("@naman_deep_singh/response-utils");
5
+ const constants_1 = require("src/constants");
4
6
  const HTTPError_1 = require("./HTTPError");
5
7
  class ForbiddenError extends HTTPError_1.HTTPError {
6
- constructor(message = 'Forbidden', details) {
7
- super(message, 403, details);
8
+ constructor(details, cause) {
9
+ super(constants_1.ERROR_CODES.FORBIDDEN, response_utils_1.HTTP_STATUS.CLIENT_ERROR.FORBIDDEN, details, cause);
8
10
  }
9
11
  }
10
12
  exports.ForbiddenError = ForbiddenError;
@@ -1,5 +1,6 @@
1
+ import type { ErrorCode } from '../constants';
1
2
  import { AppError } from './AppError';
2
3
  export declare class HTTPError extends AppError {
3
- status: string;
4
- constructor(message: string, statusCode: number, details?: unknown);
4
+ status: 'fail' | 'error';
5
+ constructor(errorCode: ErrorCode, statusCode: number, details?: unknown, cause?: Error);
5
6
  }
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.HTTPError = void 0;
4
4
  const AppError_1 = require("./AppError");
5
5
  class HTTPError extends AppError_1.AppError {
6
- constructor(message, statusCode, details) {
7
- super(message, statusCode, details);
8
- this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';
6
+ constructor(errorCode, statusCode, details, cause) {
7
+ super(errorCode, statusCode, details, cause);
8
+ this.status = statusCode >= 400 && statusCode < 500 ? 'fail' : 'error';
9
9
  }
10
10
  }
11
11
  exports.HTTPError = HTTPError;
@@ -1,4 +1,5 @@
1
+ import { type ErrorCode } from 'src/constants';
1
2
  import { HTTPError } from './HTTPError';
2
3
  export declare class InternalServerError extends HTTPError {
3
- constructor(message?: string, details?: unknown);
4
+ constructor(errorCode?: ErrorCode, details?: unknown, cause?: Error);
4
5
  }
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.InternalServerError = void 0;
4
+ const response_utils_1 = require("@naman_deep_singh/response-utils");
5
+ const constants_1 = require("src/constants");
4
6
  const HTTPError_1 = require("./HTTPError");
5
7
  class InternalServerError extends HTTPError_1.HTTPError {
6
- constructor(message = 'Internal Server Error', details) {
7
- super(message, 500, details);
8
+ constructor(errorCode = constants_1.ERROR_CODES.INTERNAL_SERVER_ERROR, details, cause) {
9
+ super(errorCode, response_utils_1.HTTP_STATUS.SERVER_ERROR.INTERNAL_SERVER_ERROR, details, cause);
8
10
  }
9
11
  }
10
12
  exports.InternalServerError = InternalServerError;
@@ -1,4 +1,4 @@
1
1
  import { HTTPError } from './HTTPError';
2
2
  export declare class NotFoundError extends HTTPError {
3
- constructor(message?: string, details?: unknown);
3
+ constructor(details?: unknown, cause?: Error);
4
4
  }
@@ -1,10 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.NotFoundError = void 0;
4
+ const response_utils_1 = require("@naman_deep_singh/response-utils");
5
+ const constants_1 = require("src/constants");
4
6
  const HTTPError_1 = require("./HTTPError");
5
7
  class NotFoundError extends HTTPError_1.HTTPError {
6
- constructor(message = 'Not Found', details) {
7
- super(message, 404, details);
8
+ constructor(details, cause) {
9
+ super(constants_1.ERROR_CODES.NOT_FOUND, response_utils_1.HTTP_STATUS.CLIENT_ERROR.NOT_FOUND, details, cause);
8
10
  }
9
11
  }
10
12
  exports.NotFoundError = NotFoundError;
@@ -0,0 +1,4 @@
1
+ import { TooManyRequestsError } from './TooManyRequestsError';
2
+ export declare class RateLimitError extends TooManyRequestsError {
3
+ constructor(details?: unknown, cause?: Error);
4
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RateLimitError = void 0;
4
+ const constants_1 = require("src/constants");
5
+ const TooManyRequestsError_1 = require("./TooManyRequestsError");
6
+ class RateLimitError extends TooManyRequestsError_1.TooManyRequestsError {
7
+ constructor(details, cause) {
8
+ super(constants_1.ERROR_CODES.RATE_LIMIT_EXCEEDED, details, cause);
9
+ }
10
+ }
11
+ exports.RateLimitError = RateLimitError;
@@ -0,0 +1,4 @@
1
+ import { UnauthorizedError } from './UnauthorizedError';
2
+ export declare class TokenExpiredError extends UnauthorizedError {
3
+ constructor(details?: unknown, cause?: Error);
4
+ }