@catbee/utils 0.0.1 → 0.0.3
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 +303 -107
- package/build/esm/config.d.ts +13 -1
- package/build/esm/config.js +16 -4
- package/build/esm/config.js.map +1 -1
- package/build/esm/index.d.ts +20 -18
- package/build/esm/index.js +20 -18
- package/build/esm/index.js.map +1 -1
- package/build/esm/types/api-response.d.ts +83 -1
- package/build/esm/types/api-response.js.map +1 -1
- package/build/esm/utils/array.utils.d.ts +65 -2
- package/build/esm/utils/array.utils.js +147 -20
- package/build/esm/utils/array.utils.js.map +1 -1
- package/build/esm/utils/async.utils.d.ts +56 -1
- package/build/esm/utils/async.utils.js +166 -11
- package/build/esm/utils/async.utils.js.map +1 -1
- package/build/esm/utils/cache.utils.d.ts +67 -4
- package/build/esm/utils/cache.utils.js +210 -28
- package/build/esm/utils/cache.utils.js.map +1 -1
- package/build/esm/utils/context-store.utils.d.ts +102 -1
- package/build/esm/utils/context-store.utils.js +185 -3
- package/build/esm/utils/context-store.utils.js.map +1 -1
- package/build/esm/utils/crypto.utils.d.ts +110 -8
- package/build/esm/utils/crypto.utils.js +298 -23
- package/build/esm/utils/crypto.utils.js.map +1 -1
- package/build/esm/utils/decorators.utils.d.ts +40 -0
- package/build/esm/utils/decorators.utils.js +312 -0
- package/build/esm/utils/decorators.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +122 -1
- package/build/esm/utils/dir.utils.js +578 -3
- package/build/esm/utils/dir.utils.js.map +1 -1
- package/build/esm/utils/env.utils.d.ts +83 -0
- package/build/esm/utils/env.utils.js +235 -6
- package/build/esm/utils/env.utils.js.map +1 -1
- package/build/esm/utils/exception.utils.d.ts +117 -1
- package/build/esm/utils/exception.utils.js +331 -12
- package/build/esm/utils/exception.utils.js.map +1 -1
- package/build/esm/utils/fs.utils.d.ts +132 -0
- package/build/esm/utils/fs.utils.js +421 -4
- package/build/esm/utils/fs.utils.js.map +1 -1
- package/build/esm/utils/http-status-codes.d.ts +87 -178
- package/build/esm/utils/http-status-codes.js +92 -178
- package/build/esm/utils/http-status-codes.js.map +1 -1
- package/build/esm/utils/id.utils.d.ts +11 -10
- package/build/esm/utils/id.utils.js +30 -21
- package/build/esm/utils/id.utils.js.map +1 -1
- package/build/esm/utils/logger.utils.d.ts +35 -2
- package/build/esm/utils/logger.utils.js +68 -14
- package/build/esm/utils/logger.utils.js.map +1 -1
- package/build/esm/utils/middleware.utils.d.ts +73 -0
- package/build/esm/utils/middleware.utils.js +158 -0
- package/build/esm/utils/middleware.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +65 -3
- package/build/esm/utils/obj.utils.js +206 -18
- package/build/esm/utils/obj.utils.js.map +1 -1
- package/build/esm/utils/response.utils.d.ts +103 -2
- package/build/esm/utils/response.utils.js +156 -3
- package/build/esm/utils/response.utils.js.map +1 -1
- package/build/esm/utils/string.utils.d.ts +61 -5
- package/build/esm/utils/string.utils.js +109 -19
- package/build/esm/utils/string.utils.js.map +1 -1
- package/build/esm/utils/url.utils.d.ts +117 -0
- package/build/esm/utils/url.utils.js +287 -2
- package/build/esm/utils/url.utils.js.map +1 -1
- package/build/esm/utils/validate.utils.d.ts +110 -0
- package/build/esm/utils/validate.utils.js +190 -8
- package/build/esm/utils/validate.utils.js.map +1 -1
- package/build/esnext/config.d.ts +13 -1
- package/build/esnext/config.js +16 -4
- package/build/esnext/config.js.map +1 -1
- package/build/esnext/index.d.ts +20 -18
- package/build/esnext/index.js +20 -18
- package/build/esnext/index.js.map +1 -1
- package/build/esnext/types/api-response.d.ts +83 -1
- package/build/esnext/types/api-response.js.map +1 -1
- package/build/esnext/utils/array.utils.d.ts +65 -2
- package/build/esnext/utils/array.utils.js +140 -21
- package/build/esnext/utils/array.utils.js.map +1 -1
- package/build/esnext/utils/async.utils.d.ts +56 -1
- package/build/esnext/utils/async.utils.js +130 -14
- package/build/esnext/utils/async.utils.js.map +1 -1
- package/build/esnext/utils/cache.utils.d.ts +67 -4
- package/build/esnext/utils/cache.utils.js +137 -9
- package/build/esnext/utils/cache.utils.js.map +1 -1
- package/build/esnext/utils/context-store.utils.d.ts +102 -1
- package/build/esnext/utils/context-store.utils.js +171 -3
- package/build/esnext/utils/context-store.utils.js.map +1 -1
- package/build/esnext/utils/crypto.utils.d.ts +110 -8
- package/build/esnext/utils/crypto.utils.js +199 -19
- package/build/esnext/utils/crypto.utils.js.map +1 -1
- package/build/esnext/utils/decorators.utils.d.ts +40 -0
- package/build/esnext/utils/decorators.utils.js +155 -0
- package/build/esnext/utils/decorators.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +122 -1
- package/build/esnext/utils/dir.utils.js +307 -3
- package/build/esnext/utils/dir.utils.js.map +1 -1
- package/build/esnext/utils/env.utils.d.ts +83 -0
- package/build/esnext/utils/env.utils.js +191 -8
- package/build/esnext/utils/env.utils.js.map +1 -1
- package/build/esnext/utils/exception.utils.d.ts +117 -1
- package/build/esnext/utils/exception.utils.js +222 -12
- package/build/esnext/utils/exception.utils.js.map +1 -1
- package/build/esnext/utils/fs.utils.d.ts +132 -0
- package/build/esnext/utils/fs.utils.js +268 -4
- package/build/esnext/utils/fs.utils.js.map +1 -1
- package/build/esnext/utils/http-status-codes.d.ts +87 -178
- package/build/esnext/utils/http-status-codes.js +92 -178
- package/build/esnext/utils/http-status-codes.js.map +1 -1
- package/build/esnext/utils/id.utils.d.ts +11 -10
- package/build/esnext/utils/id.utils.js +28 -20
- package/build/esnext/utils/id.utils.js.map +1 -1
- package/build/esnext/utils/logger.utils.d.ts +35 -2
- package/build/esnext/utils/logger.utils.js +56 -14
- package/build/esnext/utils/logger.utils.js.map +1 -1
- package/build/esnext/utils/middleware.utils.d.ts +73 -0
- package/build/esnext/utils/middleware.utils.js +157 -0
- package/build/esnext/utils/middleware.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +65 -3
- package/build/esnext/utils/obj.utils.js +169 -14
- package/build/esnext/utils/obj.utils.js.map +1 -1
- package/build/esnext/utils/response.utils.d.ts +103 -2
- package/build/esnext/utils/response.utils.js +141 -3
- package/build/esnext/utils/response.utils.js.map +1 -1
- package/build/esnext/utils/string.utils.d.ts +61 -5
- package/build/esnext/utils/string.utils.js +110 -14
- package/build/esnext/utils/string.utils.js.map +1 -1
- package/build/esnext/utils/url.utils.d.ts +117 -0
- package/build/esnext/utils/url.utils.js +248 -2
- package/build/esnext/utils/url.utils.js.map +1 -1
- package/build/esnext/utils/validate.utils.d.ts +110 -0
- package/build/esnext/utils/validate.utils.js +190 -8
- package/build/esnext/utils/validate.utils.js.map +1 -1
- package/build/src/config.d.ts +13 -1
- package/build/src/config.js +15 -3
- package/build/src/config.js.map +1 -1
- package/build/src/index.d.ts +20 -18
- package/build/src/index.js +2 -0
- package/build/src/index.js.map +1 -1
- package/build/src/types/api-response.d.ts +83 -1
- package/build/src/types/api-response.js.map +1 -1
- package/build/src/utils/array.utils.d.ts +65 -2
- package/build/src/utils/array.utils.js +147 -22
- package/build/src/utils/array.utils.js.map +1 -1
- package/build/src/utils/async.utils.d.ts +56 -1
- package/build/src/utils/async.utils.js +137 -17
- package/build/src/utils/async.utils.js.map +1 -1
- package/build/src/utils/cache.utils.d.ts +67 -4
- package/build/src/utils/cache.utils.js +137 -9
- package/build/src/utils/cache.utils.js.map +1 -1
- package/build/src/utils/context-store.utils.d.ts +102 -1
- package/build/src/utils/context-store.utils.js +173 -3
- package/build/src/utils/context-store.utils.js.map +1 -1
- package/build/src/utils/crypto.utils.d.ts +110 -8
- package/build/src/utils/crypto.utils.js +214 -27
- package/build/src/utils/crypto.utils.js.map +1 -1
- package/build/src/utils/decorators.utils.d.ts +40 -0
- package/build/src/utils/decorators.utils.js +165 -0
- package/build/src/utils/decorators.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +122 -1
- package/build/src/utils/dir.utils.js +314 -0
- package/build/src/utils/dir.utils.js.map +1 -1
- package/build/src/utils/env.utils.d.ts +83 -0
- package/build/src/utils/env.utils.js +192 -9
- package/build/src/utils/env.utils.js.map +1 -1
- package/build/src/utils/exception.utils.d.ts +117 -1
- package/build/src/utils/exception.utils.js +233 -11
- package/build/src/utils/exception.utils.js.map +1 -1
- package/build/src/utils/fs.utils.d.ts +132 -0
- package/build/src/utils/fs.utils.js +281 -3
- package/build/src/utils/fs.utils.js.map +1 -1
- package/build/src/utils/http-status-codes.d.ts +87 -178
- package/build/src/utils/http-status-codes.js +92 -178
- package/build/src/utils/http-status-codes.js.map +1 -1
- package/build/src/utils/id.utils.d.ts +11 -10
- package/build/src/utils/id.utils.js +28 -20
- package/build/src/utils/id.utils.js.map +1 -1
- package/build/src/utils/logger.utils.d.ts +35 -2
- package/build/src/utils/logger.utils.js +56 -11
- package/build/src/utils/logger.utils.js.map +1 -1
- package/build/src/utils/middleware.utils.d.ts +73 -0
- package/build/src/utils/middleware.utils.js +163 -0
- package/build/src/utils/middleware.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +65 -3
- package/build/src/utils/obj.utils.js +179 -18
- package/build/src/utils/obj.utils.js.map +1 -1
- package/build/src/utils/response.utils.d.ts +103 -2
- package/build/src/utils/response.utils.js +147 -2
- package/build/src/utils/response.utils.js.map +1 -1
- package/build/src/utils/string.utils.d.ts +61 -5
- package/build/src/utils/string.utils.js +122 -20
- package/build/src/utils/string.utils.js.map +1 -1
- package/build/src/utils/url.utils.d.ts +117 -0
- package/build/src/utils/url.utils.js +256 -1
- package/build/src/utils/url.utils.js.map +1 -1
- package/build/src/utils/validate.utils.d.ts +110 -0
- package/build/src/utils/validate.utils.js +204 -8
- package/build/src/utils/validate.utils.js.map +1 -1
- package/package.json +20 -12
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ErrorResponse } from
|
|
1
|
+
import { ErrorResponse } from './response.utils';
|
|
2
2
|
/**
|
|
3
3
|
* Generic HTTP error class used for custom exceptions with any status code.
|
|
4
4
|
* Inherit this when you need to throw errors dynamically at runtime.
|
|
@@ -111,4 +111,120 @@ export declare class GatewayTimeoutException extends ErrorResponse {
|
|
|
111
111
|
*/
|
|
112
112
|
constructor(message?: string);
|
|
113
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Represents a 422 Unprocessable Entity Error.
|
|
116
|
+
* Used when the server understands the content type but cannot process the contained instructions.
|
|
117
|
+
*/
|
|
118
|
+
export declare class UnprocessableEntityException extends ErrorResponse {
|
|
119
|
+
details?: Record<string, any> | undefined;
|
|
120
|
+
/**
|
|
121
|
+
* @param message - Optional custom message. Defaults to "Unprocessable Entity".
|
|
122
|
+
* @param details - Optional validation details or additional error context.
|
|
123
|
+
*/
|
|
124
|
+
constructor(message?: string, details?: Record<string, any> | undefined);
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Represents a 405 Method Not Allowed Error.
|
|
128
|
+
* Used when the request method is not supported for the requested resource.
|
|
129
|
+
*/
|
|
130
|
+
export declare class MethodNotAllowedException extends ErrorResponse {
|
|
131
|
+
allowedMethods?: string[] | undefined;
|
|
132
|
+
/**
|
|
133
|
+
* @param message - Optional custom message. Defaults to "Method Not Allowed".
|
|
134
|
+
* @param allowedMethods - Optional array of allowed HTTP methods.
|
|
135
|
+
*/
|
|
136
|
+
constructor(message?: string, allowedMethods?: string[] | undefined);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Represents a 406 Not Acceptable Error.
|
|
140
|
+
* Used when the server cannot produce a response matching the client's accepted content types.
|
|
141
|
+
*/
|
|
142
|
+
export declare class NotAcceptableException extends ErrorResponse {
|
|
143
|
+
/**
|
|
144
|
+
* @param message - Optional custom message. Defaults to "Not Acceptable".
|
|
145
|
+
*/
|
|
146
|
+
constructor(message?: string);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Represents a 408 Request Timeout Error.
|
|
150
|
+
* Used when the server times out waiting for the client to send a request.
|
|
151
|
+
*/
|
|
152
|
+
export declare class RequestTimeoutException extends ErrorResponse {
|
|
153
|
+
/**
|
|
154
|
+
* @param message - Optional custom message. Defaults to "Request Timeout".
|
|
155
|
+
*/
|
|
156
|
+
constructor(message?: string);
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Represents a 415 Unsupported Media Type Error.
|
|
160
|
+
* Used when the request's Content-Type is not supported.
|
|
161
|
+
*/
|
|
162
|
+
export declare class UnsupportedMediaTypeException extends ErrorResponse {
|
|
163
|
+
/**
|
|
164
|
+
* @param message - Optional custom message. Defaults to "Unsupported Media Type".
|
|
165
|
+
*/
|
|
166
|
+
constructor(message?: string);
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Represents a 413 Payload Too Large Error.
|
|
170
|
+
* Used when the request body exceeds server limits.
|
|
171
|
+
*/
|
|
172
|
+
export declare class PayloadTooLargeException extends ErrorResponse {
|
|
173
|
+
/**
|
|
174
|
+
* @param message - Optional custom message. Defaults to "Payload Too Large".
|
|
175
|
+
*/
|
|
176
|
+
constructor(message?: string);
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Represents a 507 Insufficient Storage Error.
|
|
180
|
+
* Used when the server has insufficient storage to complete the request.
|
|
181
|
+
*/
|
|
182
|
+
export declare class InsufficientStorageException extends ErrorResponse {
|
|
183
|
+
/**
|
|
184
|
+
* @param message - Optional custom message. Defaults to "Insufficient Storage".
|
|
185
|
+
*/
|
|
186
|
+
constructor(message?: string);
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* Checks if an error is an instance of HttpError or its subclasses.
|
|
190
|
+
*
|
|
191
|
+
* @param error - The error to check.
|
|
192
|
+
* @returns True if the error is an HttpError, false otherwise.
|
|
193
|
+
*/
|
|
194
|
+
export declare function isHttpError(error: unknown): error is ErrorResponse;
|
|
195
|
+
/**
|
|
196
|
+
* Creates an HTTP error with the specified status code and message.
|
|
197
|
+
* Factory function for creating appropriate HTTP error instances.
|
|
198
|
+
*
|
|
199
|
+
* @param status - HTTP status code.
|
|
200
|
+
* @param message - Optional custom error message.
|
|
201
|
+
* @returns An instance of the appropriate HTTP error class.
|
|
202
|
+
*/
|
|
203
|
+
export declare function createHttpError(status: number, message?: string): ErrorResponse;
|
|
204
|
+
/**
|
|
205
|
+
* Type guard to check if an object has specific error properties.
|
|
206
|
+
*
|
|
207
|
+
* @param error - The object to check.
|
|
208
|
+
* @returns True if object has error-like structure.
|
|
209
|
+
*/
|
|
210
|
+
export declare function hasErrorShape(error: unknown): error is {
|
|
211
|
+
message: string;
|
|
212
|
+
status?: number;
|
|
213
|
+
code?: string;
|
|
214
|
+
};
|
|
215
|
+
/**
|
|
216
|
+
* Safely extracts message from any error type.
|
|
217
|
+
*
|
|
218
|
+
* @param error - Any error object or value.
|
|
219
|
+
* @returns A string error message.
|
|
220
|
+
*/
|
|
221
|
+
export declare function getErrorMessage(error: unknown): string;
|
|
222
|
+
/**
|
|
223
|
+
* Wraps a function and transforms any errors into HTTP errors.
|
|
224
|
+
* Useful for API handlers to ensure consistent error responses.
|
|
225
|
+
*
|
|
226
|
+
* @param handler - The async function to wrap.
|
|
227
|
+
* @returns A function that always returns or throws an HTTP error.
|
|
228
|
+
*/
|
|
229
|
+
export declare function withErrorHandling<T extends (...args: any[]) => Promise<any>>(handler: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>;
|
|
114
230
|
//# sourceMappingURL=exception.utils.d.ts.map
|
|
@@ -13,8 +13,70 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
13
13
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
14
|
};
|
|
15
15
|
})();
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
17
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
18
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
19
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
20
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
21
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
22
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
23
|
+
});
|
|
24
|
+
};
|
|
25
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
26
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
27
|
+
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
28
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
29
|
+
function step(op) {
|
|
30
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
31
|
+
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
32
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
33
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
34
|
+
switch (op[0]) {
|
|
35
|
+
case 0: case 1: t = op; break;
|
|
36
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
37
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
38
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
39
|
+
default:
|
|
40
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
41
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
42
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
43
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
44
|
+
if (t[2]) _.ops.pop();
|
|
45
|
+
_.trys.pop(); continue;
|
|
46
|
+
}
|
|
47
|
+
op = body.call(thisArg, _);
|
|
48
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
49
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
var __read = (this && this.__read) || function (o, n) {
|
|
53
|
+
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
54
|
+
if (!m) return o;
|
|
55
|
+
var i = m.call(o), r, ar = [], e;
|
|
56
|
+
try {
|
|
57
|
+
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
58
|
+
}
|
|
59
|
+
catch (error) { e = { error: error }; }
|
|
60
|
+
finally {
|
|
61
|
+
try {
|
|
62
|
+
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
63
|
+
}
|
|
64
|
+
finally { if (e) throw e.error; }
|
|
65
|
+
}
|
|
66
|
+
return ar;
|
|
67
|
+
};
|
|
68
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
69
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
70
|
+
if (ar || !(i in from)) {
|
|
71
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
72
|
+
ar[i] = from[i];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
76
|
+
};
|
|
77
|
+
import { HttpStatusCodes } from './http-status-codes';
|
|
78
|
+
import { getLogger } from './logger.utils';
|
|
79
|
+
import { ErrorResponse } from './response.utils';
|
|
18
80
|
/**
|
|
19
81
|
* Generic HTTP error class used for custom exceptions with any status code.
|
|
20
82
|
* Inherit this when you need to throw errors dynamically at runtime.
|
|
@@ -44,7 +106,7 @@ var InternalServerErrorException = /** @class */ (function (_super) {
|
|
|
44
106
|
* @param message - Optional custom message. Defaults to "Internal server error".
|
|
45
107
|
*/
|
|
46
108
|
function InternalServerErrorException(message) {
|
|
47
|
-
if (message === void 0) { message =
|
|
109
|
+
if (message === void 0) { message = 'Internal server error'; }
|
|
48
110
|
var _this = _super.call(this, message) || this;
|
|
49
111
|
_this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
50
112
|
return _this;
|
|
@@ -62,7 +124,7 @@ var UnauthorizedException = /** @class */ (function (_super) {
|
|
|
62
124
|
* @param message - Optional custom message. Defaults to "Unauthorized".
|
|
63
125
|
*/
|
|
64
126
|
function UnauthorizedException(message) {
|
|
65
|
-
if (message === void 0) { message =
|
|
127
|
+
if (message === void 0) { message = 'Unauthorized'; }
|
|
66
128
|
var _this = _super.call(this, message) || this;
|
|
67
129
|
_this.status = HttpStatusCodes.UNAUTHORIZED;
|
|
68
130
|
return _this;
|
|
@@ -80,7 +142,7 @@ var BadRequestException = /** @class */ (function (_super) {
|
|
|
80
142
|
* @param message - Optional custom message. Defaults to "Bad request".
|
|
81
143
|
*/
|
|
82
144
|
function BadRequestException(message) {
|
|
83
|
-
if (message === void 0) { message =
|
|
145
|
+
if (message === void 0) { message = 'Bad request'; }
|
|
84
146
|
var _this = _super.call(this, message) || this;
|
|
85
147
|
_this.status = HttpStatusCodes.BAD_REQUEST;
|
|
86
148
|
return _this;
|
|
@@ -98,7 +160,7 @@ var NotFoundException = /** @class */ (function (_super) {
|
|
|
98
160
|
* @param message - Optional custom message. Defaults to "Resource not found".
|
|
99
161
|
*/
|
|
100
162
|
function NotFoundException(message) {
|
|
101
|
-
if (message === void 0) { message =
|
|
163
|
+
if (message === void 0) { message = 'Resource not found'; }
|
|
102
164
|
var _this = _super.call(this, message) || this;
|
|
103
165
|
_this.status = HttpStatusCodes.NOT_FOUND;
|
|
104
166
|
return _this;
|
|
@@ -116,7 +178,7 @@ var ForbiddenException = /** @class */ (function (_super) {
|
|
|
116
178
|
* @param message - Optional custom message. Defaults to "Forbidden".
|
|
117
179
|
*/
|
|
118
180
|
function ForbiddenException(message) {
|
|
119
|
-
if (message === void 0) { message =
|
|
181
|
+
if (message === void 0) { message = 'Forbidden'; }
|
|
120
182
|
var _this = _super.call(this, message) || this;
|
|
121
183
|
_this.status = HttpStatusCodes.FORBIDDEN;
|
|
122
184
|
return _this;
|
|
@@ -134,7 +196,7 @@ var ConflictException = /** @class */ (function (_super) {
|
|
|
134
196
|
* @param message - Optional custom message. Defaults to "Conflict".
|
|
135
197
|
*/
|
|
136
198
|
function ConflictException(message) {
|
|
137
|
-
if (message === void 0) { message =
|
|
199
|
+
if (message === void 0) { message = 'Conflict'; }
|
|
138
200
|
var _this = _super.call(this, message) || this;
|
|
139
201
|
_this.status = HttpStatusCodes.CONFLICT;
|
|
140
202
|
return _this;
|
|
@@ -152,7 +214,7 @@ var BadGatewayException = /** @class */ (function (_super) {
|
|
|
152
214
|
* @param message - Optional custom message. Defaults to "Bad Gateway".
|
|
153
215
|
*/
|
|
154
216
|
function BadGatewayException(message) {
|
|
155
|
-
if (message === void 0) { message =
|
|
217
|
+
if (message === void 0) { message = 'Bad Gateway'; }
|
|
156
218
|
var _this = _super.call(this, message) || this;
|
|
157
219
|
_this.status = HttpStatusCodes.BAD_GATEWAY;
|
|
158
220
|
return _this;
|
|
@@ -170,7 +232,7 @@ var TooManyRequestsException = /** @class */ (function (_super) {
|
|
|
170
232
|
* @param message - Optional custom message. Defaults to "Too many requests".
|
|
171
233
|
*/
|
|
172
234
|
function TooManyRequestsException(message) {
|
|
173
|
-
if (message === void 0) { message =
|
|
235
|
+
if (message === void 0) { message = 'Too many requests'; }
|
|
174
236
|
var _this = _super.call(this, message) || this;
|
|
175
237
|
_this.status = HttpStatusCodes.TOO_MANY_REQUESTS;
|
|
176
238
|
return _this;
|
|
@@ -188,7 +250,7 @@ var ServiceUnavailableException = /** @class */ (function (_super) {
|
|
|
188
250
|
* @param message - Optional custom message. Defaults to "Service Unavailable".
|
|
189
251
|
*/
|
|
190
252
|
function ServiceUnavailableException(message) {
|
|
191
|
-
if (message === void 0) { message =
|
|
253
|
+
if (message === void 0) { message = 'Service Unavailable'; }
|
|
192
254
|
var _this = _super.call(this, message) || this;
|
|
193
255
|
_this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;
|
|
194
256
|
return _this;
|
|
@@ -206,7 +268,7 @@ var GatewayTimeoutException = /** @class */ (function (_super) {
|
|
|
206
268
|
* @param message - Optional custom message. Defaults to "Gateway Timeout".
|
|
207
269
|
*/
|
|
208
270
|
function GatewayTimeoutException(message) {
|
|
209
|
-
if (message === void 0) { message =
|
|
271
|
+
if (message === void 0) { message = 'Gateway Timeout'; }
|
|
210
272
|
var _this = _super.call(this, message) || this;
|
|
211
273
|
_this.status = HttpStatusCodes.GATEWAY_TIMEOUT;
|
|
212
274
|
return _this;
|
|
@@ -214,4 +276,261 @@ var GatewayTimeoutException = /** @class */ (function (_super) {
|
|
|
214
276
|
return GatewayTimeoutException;
|
|
215
277
|
}(ErrorResponse));
|
|
216
278
|
export { GatewayTimeoutException };
|
|
279
|
+
/**
|
|
280
|
+
* Represents a 422 Unprocessable Entity Error.
|
|
281
|
+
* Used when the server understands the content type but cannot process the contained instructions.
|
|
282
|
+
*/
|
|
283
|
+
var UnprocessableEntityException = /** @class */ (function (_super) {
|
|
284
|
+
__extends(UnprocessableEntityException, _super);
|
|
285
|
+
/**
|
|
286
|
+
* @param message - Optional custom message. Defaults to "Unprocessable Entity".
|
|
287
|
+
* @param details - Optional validation details or additional error context.
|
|
288
|
+
*/
|
|
289
|
+
function UnprocessableEntityException(message, details) {
|
|
290
|
+
if (message === void 0) { message = 'Unprocessable Entity'; }
|
|
291
|
+
var _this = _super.call(this, message) || this;
|
|
292
|
+
_this.details = details;
|
|
293
|
+
_this.status = HttpStatusCodes.UNPROCESSABLE_ENTITY;
|
|
294
|
+
return _this;
|
|
295
|
+
}
|
|
296
|
+
return UnprocessableEntityException;
|
|
297
|
+
}(ErrorResponse));
|
|
298
|
+
export { UnprocessableEntityException };
|
|
299
|
+
/**
|
|
300
|
+
* Represents a 405 Method Not Allowed Error.
|
|
301
|
+
* Used when the request method is not supported for the requested resource.
|
|
302
|
+
*/
|
|
303
|
+
var MethodNotAllowedException = /** @class */ (function (_super) {
|
|
304
|
+
__extends(MethodNotAllowedException, _super);
|
|
305
|
+
/**
|
|
306
|
+
* @param message - Optional custom message. Defaults to "Method Not Allowed".
|
|
307
|
+
* @param allowedMethods - Optional array of allowed HTTP methods.
|
|
308
|
+
*/
|
|
309
|
+
function MethodNotAllowedException(message, allowedMethods) {
|
|
310
|
+
if (message === void 0) { message = 'Method Not Allowed'; }
|
|
311
|
+
var _this = _super.call(this, message) || this;
|
|
312
|
+
_this.allowedMethods = allowedMethods;
|
|
313
|
+
_this.status = HttpStatusCodes.METHOD_NOT_ALLOWED;
|
|
314
|
+
return _this;
|
|
315
|
+
}
|
|
316
|
+
return MethodNotAllowedException;
|
|
317
|
+
}(ErrorResponse));
|
|
318
|
+
export { MethodNotAllowedException };
|
|
319
|
+
/**
|
|
320
|
+
* Represents a 406 Not Acceptable Error.
|
|
321
|
+
* Used when the server cannot produce a response matching the client's accepted content types.
|
|
322
|
+
*/
|
|
323
|
+
var NotAcceptableException = /** @class */ (function (_super) {
|
|
324
|
+
__extends(NotAcceptableException, _super);
|
|
325
|
+
/**
|
|
326
|
+
* @param message - Optional custom message. Defaults to "Not Acceptable".
|
|
327
|
+
*/
|
|
328
|
+
function NotAcceptableException(message) {
|
|
329
|
+
if (message === void 0) { message = 'Not Acceptable'; }
|
|
330
|
+
var _this = _super.call(this, message) || this;
|
|
331
|
+
_this.status = HttpStatusCodes.NOT_ACCEPTABLE;
|
|
332
|
+
return _this;
|
|
333
|
+
}
|
|
334
|
+
return NotAcceptableException;
|
|
335
|
+
}(ErrorResponse));
|
|
336
|
+
export { NotAcceptableException };
|
|
337
|
+
/**
|
|
338
|
+
* Represents a 408 Request Timeout Error.
|
|
339
|
+
* Used when the server times out waiting for the client to send a request.
|
|
340
|
+
*/
|
|
341
|
+
var RequestTimeoutException = /** @class */ (function (_super) {
|
|
342
|
+
__extends(RequestTimeoutException, _super);
|
|
343
|
+
/**
|
|
344
|
+
* @param message - Optional custom message. Defaults to "Request Timeout".
|
|
345
|
+
*/
|
|
346
|
+
function RequestTimeoutException(message) {
|
|
347
|
+
if (message === void 0) { message = 'Request Timeout'; }
|
|
348
|
+
var _this = _super.call(this, message) || this;
|
|
349
|
+
_this.status = HttpStatusCodes.REQUEST_TIMEOUT;
|
|
350
|
+
return _this;
|
|
351
|
+
}
|
|
352
|
+
return RequestTimeoutException;
|
|
353
|
+
}(ErrorResponse));
|
|
354
|
+
export { RequestTimeoutException };
|
|
355
|
+
/**
|
|
356
|
+
* Represents a 415 Unsupported Media Type Error.
|
|
357
|
+
* Used when the request's Content-Type is not supported.
|
|
358
|
+
*/
|
|
359
|
+
var UnsupportedMediaTypeException = /** @class */ (function (_super) {
|
|
360
|
+
__extends(UnsupportedMediaTypeException, _super);
|
|
361
|
+
/**
|
|
362
|
+
* @param message - Optional custom message. Defaults to "Unsupported Media Type".
|
|
363
|
+
*/
|
|
364
|
+
function UnsupportedMediaTypeException(message) {
|
|
365
|
+
if (message === void 0) { message = 'Unsupported Media Type'; }
|
|
366
|
+
var _this = _super.call(this, message) || this;
|
|
367
|
+
_this.status = HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE;
|
|
368
|
+
return _this;
|
|
369
|
+
}
|
|
370
|
+
return UnsupportedMediaTypeException;
|
|
371
|
+
}(ErrorResponse));
|
|
372
|
+
export { UnsupportedMediaTypeException };
|
|
373
|
+
/**
|
|
374
|
+
* Represents a 413 Payload Too Large Error.
|
|
375
|
+
* Used when the request body exceeds server limits.
|
|
376
|
+
*/
|
|
377
|
+
var PayloadTooLargeException = /** @class */ (function (_super) {
|
|
378
|
+
__extends(PayloadTooLargeException, _super);
|
|
379
|
+
/**
|
|
380
|
+
* @param message - Optional custom message. Defaults to "Payload Too Large".
|
|
381
|
+
*/
|
|
382
|
+
function PayloadTooLargeException(message) {
|
|
383
|
+
if (message === void 0) { message = 'Payload Too Large'; }
|
|
384
|
+
var _this = _super.call(this, message) || this;
|
|
385
|
+
_this.status = HttpStatusCodes.PAYLOAD_TOO_LARGE;
|
|
386
|
+
return _this;
|
|
387
|
+
}
|
|
388
|
+
return PayloadTooLargeException;
|
|
389
|
+
}(ErrorResponse));
|
|
390
|
+
export { PayloadTooLargeException };
|
|
391
|
+
/**
|
|
392
|
+
* Represents a 507 Insufficient Storage Error.
|
|
393
|
+
* Used when the server has insufficient storage to complete the request.
|
|
394
|
+
*/
|
|
395
|
+
var InsufficientStorageException = /** @class */ (function (_super) {
|
|
396
|
+
__extends(InsufficientStorageException, _super);
|
|
397
|
+
/**
|
|
398
|
+
* @param message - Optional custom message. Defaults to "Insufficient Storage".
|
|
399
|
+
*/
|
|
400
|
+
function InsufficientStorageException(message) {
|
|
401
|
+
if (message === void 0) { message = 'Insufficient Storage'; }
|
|
402
|
+
var _this = _super.call(this, message) || this;
|
|
403
|
+
_this.status = HttpStatusCodes.INSUFFICIENT_STORAGE;
|
|
404
|
+
return _this;
|
|
405
|
+
}
|
|
406
|
+
return InsufficientStorageException;
|
|
407
|
+
}(ErrorResponse));
|
|
408
|
+
export { InsufficientStorageException };
|
|
409
|
+
/**
|
|
410
|
+
* Checks if an error is an instance of HttpError or its subclasses.
|
|
411
|
+
*
|
|
412
|
+
* @param error - The error to check.
|
|
413
|
+
* @returns True if the error is an HttpError, false otherwise.
|
|
414
|
+
*/
|
|
415
|
+
export function isHttpError(error) {
|
|
416
|
+
return error instanceof ErrorResponse;
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Creates an HTTP error with the specified status code and message.
|
|
420
|
+
* Factory function for creating appropriate HTTP error instances.
|
|
421
|
+
*
|
|
422
|
+
* @param status - HTTP status code.
|
|
423
|
+
* @param message - Optional custom error message.
|
|
424
|
+
* @returns An instance of the appropriate HTTP error class.
|
|
425
|
+
*/
|
|
426
|
+
export function createHttpError(status, message) {
|
|
427
|
+
switch (status) {
|
|
428
|
+
case HttpStatusCodes.BAD_REQUEST:
|
|
429
|
+
return new BadRequestException(message);
|
|
430
|
+
case HttpStatusCodes.UNAUTHORIZED:
|
|
431
|
+
return new UnauthorizedException(message);
|
|
432
|
+
case HttpStatusCodes.FORBIDDEN:
|
|
433
|
+
return new ForbiddenException(message);
|
|
434
|
+
case HttpStatusCodes.NOT_FOUND:
|
|
435
|
+
return new NotFoundException(message);
|
|
436
|
+
case HttpStatusCodes.METHOD_NOT_ALLOWED:
|
|
437
|
+
return new MethodNotAllowedException(message);
|
|
438
|
+
case HttpStatusCodes.NOT_ACCEPTABLE:
|
|
439
|
+
return new NotAcceptableException(message);
|
|
440
|
+
case HttpStatusCodes.REQUEST_TIMEOUT:
|
|
441
|
+
return new RequestTimeoutException(message);
|
|
442
|
+
case HttpStatusCodes.CONFLICT:
|
|
443
|
+
return new ConflictException(message);
|
|
444
|
+
case HttpStatusCodes.GONE:
|
|
445
|
+
return new HttpError(HttpStatusCodes.GONE, message || 'Gone');
|
|
446
|
+
case HttpStatusCodes.PAYLOAD_TOO_LARGE:
|
|
447
|
+
return new PayloadTooLargeException(message);
|
|
448
|
+
case HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE:
|
|
449
|
+
return new UnsupportedMediaTypeException(message);
|
|
450
|
+
case HttpStatusCodes.UNPROCESSABLE_ENTITY:
|
|
451
|
+
return new UnprocessableEntityException(message);
|
|
452
|
+
case HttpStatusCodes.TOO_MANY_REQUESTS:
|
|
453
|
+
return new TooManyRequestsException(message);
|
|
454
|
+
case HttpStatusCodes.INTERNAL_SERVER_ERROR:
|
|
455
|
+
return new InternalServerErrorException(message);
|
|
456
|
+
case HttpStatusCodes.BAD_GATEWAY:
|
|
457
|
+
return new BadGatewayException(message);
|
|
458
|
+
case HttpStatusCodes.SERVICE_UNAVAILABLE:
|
|
459
|
+
return new ServiceUnavailableException(message);
|
|
460
|
+
case HttpStatusCodes.GATEWAY_TIMEOUT:
|
|
461
|
+
return new GatewayTimeoutException(message);
|
|
462
|
+
case HttpStatusCodes.INSUFFICIENT_STORAGE:
|
|
463
|
+
return new InsufficientStorageException(message);
|
|
464
|
+
default:
|
|
465
|
+
return new HttpError(status, message || "HTTP Error ".concat(status));
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
/**
|
|
469
|
+
* Type guard to check if an object has specific error properties.
|
|
470
|
+
*
|
|
471
|
+
* @param error - The object to check.
|
|
472
|
+
* @returns True if object has error-like structure.
|
|
473
|
+
*/
|
|
474
|
+
export function hasErrorShape(error) {
|
|
475
|
+
return (typeof error === 'object' && error !== null && 'message' in error && typeof error.message === 'string');
|
|
476
|
+
}
|
|
477
|
+
/**
|
|
478
|
+
* Safely extracts message from any error type.
|
|
479
|
+
*
|
|
480
|
+
* @param error - Any error object or value.
|
|
481
|
+
* @returns A string error message.
|
|
482
|
+
*/
|
|
483
|
+
export function getErrorMessage(error) {
|
|
484
|
+
if (typeof error === 'string') {
|
|
485
|
+
return error;
|
|
486
|
+
}
|
|
487
|
+
if (error instanceof Error) {
|
|
488
|
+
return error.message;
|
|
489
|
+
}
|
|
490
|
+
if (hasErrorShape(error)) {
|
|
491
|
+
return error.message;
|
|
492
|
+
}
|
|
493
|
+
return 'Unknown error occurred';
|
|
494
|
+
}
|
|
495
|
+
/**
|
|
496
|
+
* Wraps a function and transforms any errors into HTTP errors.
|
|
497
|
+
* Useful for API handlers to ensure consistent error responses.
|
|
498
|
+
*
|
|
499
|
+
* @param handler - The async function to wrap.
|
|
500
|
+
* @returns A function that always returns or throws an HTTP error.
|
|
501
|
+
*/
|
|
502
|
+
export function withErrorHandling(handler) {
|
|
503
|
+
var _this = this;
|
|
504
|
+
return function () {
|
|
505
|
+
var args = [];
|
|
506
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
507
|
+
args[_i] = arguments[_i];
|
|
508
|
+
}
|
|
509
|
+
return __awaiter(_this, void 0, void 0, function () {
|
|
510
|
+
var error_1, status_1;
|
|
511
|
+
return __generator(this, function (_a) {
|
|
512
|
+
switch (_a.label) {
|
|
513
|
+
case 0:
|
|
514
|
+
_a.trys.push([0, 2, , 3]);
|
|
515
|
+
return [4 /*yield*/, handler.apply(void 0, __spreadArray([], __read(args), false))];
|
|
516
|
+
case 1: return [2 /*return*/, _a.sent()];
|
|
517
|
+
case 2:
|
|
518
|
+
error_1 = _a.sent();
|
|
519
|
+
if (isHttpError(error_1)) {
|
|
520
|
+
throw error_1;
|
|
521
|
+
}
|
|
522
|
+
// Log the original error (consider using a proper logger)
|
|
523
|
+
getLogger().error('Caught error in handler:', error_1);
|
|
524
|
+
status_1 = HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
525
|
+
if (hasErrorShape(error_1) && typeof error_1.status === 'number') {
|
|
526
|
+
status_1 = error_1.status;
|
|
527
|
+
}
|
|
528
|
+
// Create appropriate HTTP error
|
|
529
|
+
throw createHttpError(status_1, getErrorMessage(error_1));
|
|
530
|
+
case 3: return [2 /*return*/];
|
|
531
|
+
}
|
|
532
|
+
});
|
|
533
|
+
});
|
|
534
|
+
};
|
|
535
|
+
}
|
|
217
536
|
//# sourceMappingURL=exception.utils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"exception.utils.js","sourceRoot":"","sources":["../../../src/utils/exception.utils.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEjD;;;GAGG;AACH;IAA+B,6BAAa;IAC1C;;;;OAIG;IACH,mBAAY,MAAc,EAAE,OAAe;QACzC,YAAA,MAAK,YAAC,OAAO,CAAC,SAAC;QACf,KAAI,CAAC,MAAM,GAAG,MAAM,CAAC;;IACvB,CAAC;IACH,gBAAC;AAAD,CAAC,AAVD,CAA+B,aAAa,GAU3C;;AAED;;;GAGG;AACH;IAAkD,gDAAa;IAC7D;;OAEG;IAC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AD,CAAC,AARD,CAA6C,aAAa,GAQzD","sourcesContent":["import { HttpStatusCodes } from \"./http-status-codes\";\nimport { ErrorResponse } from \"./response.utils\";\n\n/**\n * Generic HTTP error class used for custom exceptions with any status code.\n * Inherit this when you need to throw errors dynamically at runtime.\n */\nexport class HttpError extends ErrorResponse {\n /**\n * Creates a new HTTP error instance.\n * @param status - A valid HTTP status code (e.g., 400, 500).\n * @param message - The error message to return to the client.\n */\n constructor(status: number, message: string) {\n super(message);\n this.status = status;\n }\n}\n\n/**\n * Represents a 500 Internal Server Error.\n * Use this for unexpected backend errors that are not caused by the client.\n */\nexport class InternalServerErrorException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Internal server error\".\n */\n constructor(message: string = \"Internal server error\") {\n super(message);\n this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;\n }\n}\n\n/**\n * Represents a 401 Unauthorized Error.\n * Indicates that authentication is required and has failed or not been provided.\n */\nexport class UnauthorizedException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unauthorized\".\n */\n constructor(message: string = \"Unauthorized\") {\n super(message);\n this.status = HttpStatusCodes.UNAUTHORIZED;\n }\n}\n\n/**\n * Represents a 400 Bad Request Error.\n * Indicates that the client has sent invalid data (e.g., malformed request body).\n */\nexport class BadRequestException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad request\".\n */\n constructor(message: string = \"Bad request\") {\n super(message);\n this.status = HttpStatusCodes.BAD_REQUEST;\n }\n}\n\n/**\n * Represents a 404 Not Found Error.\n * Used when a requested resource (e.g., user, file) could not be located.\n */\nexport class NotFoundException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Resource not found\".\n */\n constructor(message: string = \"Resource not found\") {\n super(message);\n this.status = HttpStatusCodes.NOT_FOUND;\n }\n}\n\n/**\n * Represents a 403 Forbidden Error.\n * Indicates that the client is authenticated but not allowed to access the resource.\n */\nexport class ForbiddenException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Forbidden\".\n */\n constructor(message: string = \"Forbidden\") {\n super(message);\n this.status = HttpStatusCodes.FORBIDDEN;\n }\n}\n\n/**\n * Represents a 409 Conflict Error.\n * Commonly used when a resource already exists or a versioning conflict occurs.\n */\nexport class ConflictException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Conflict\".\n */\n constructor(message: string = \"Conflict\") {\n super(message);\n this.status = HttpStatusCodes.CONFLICT;\n }\n}\n\n/**\n * Represents a 502 Bad Gateway Error.\n * Used when the server receives an invalid response from an upstream server.\n */\nexport class BadGatewayException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad Gateway\".\n */\n constructor(message: string = \"Bad Gateway\") {\n super(message);\n this.status = HttpStatusCodes.BAD_GATEWAY;\n }\n}\n\n/**\n * Represents a 429 Too Many Requests Error.\n * Returned when the client has hit a rate limit.\n */\nexport class TooManyRequestsException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Too many requests\".\n */\n constructor(message: string = \"Too many requests\") {\n super(message);\n this.status = HttpStatusCodes.TOO_MANY_REQUESTS;\n }\n}\n\n/**\n * Represents a 503 Service Unavailable Error.\n * Indicates that the server is temporarily unavailable (e.g., for maintenance).\n */\nexport class ServiceUnavailableException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Service Unavailable\".\n */\n constructor(message: string = \"Service Unavailable\") {\n super(message);\n this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;\n }\n}\n\n/**\n * Represents a 504 Gateway Timeout Error.\n * Returned when the server acting as a gateway times out waiting for a response.\n */\nexport class GatewayTimeoutException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Gateway Timeout\".\n */\n constructor(message: string = \"Gateway Timeout\") {\n super(message);\n this.status = HttpStatusCodes.GATEWAY_TIMEOUT;\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"exception.utils.js","sourceRoot":"","sources":["../../../src/utils/exception.utils.ts"],"names":[],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ArBC,OAAO;QAAO,cAAsB;aAAtB,UAAsB,EAAtB,qBAAsB,EAAtB,IAAsB;YAAtB,yBAAsB;;;;;;;;wBAEzB,qBAAM,OAAO,wCAAI,IAAI,YAAC;4BAA7B,sBAAO,SAAsB,EAAC;;;wBAE9B,IAAI,WAAW,CAAC,OAAK,CAAC,EAAE,CAAC;4BACvB,MAAM,OAAK,CAAC;wBACd,CAAC;wBAED,0DAA0D;wBAC1D,SAAS,EAAE,CAAC,KAAK,CAAC,0BAA0B,EAAE,OAAK,CAAC,CAAC;wBAGjD,WAAS,eAAe,CAAC,qBAAqB,CAAC;wBACnD,IAAI,aAAa,CAAC,OAAK,CAAC,IAAI,OAAO,OAAK,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;4BAC7D,QAAM,GAAG,OAAK,CAAC,MAAM,CAAC;wBACxB,CAAC;wBAED,gCAAgC;wBAChC,MAAM,eAAe,CAAC,QAAM,EAAE,eAAe,CAAC,OAAK,CAAC,CAAC,CAAC;;;;;KAEzD,CAAC;AACJ,CAAC","sourcesContent":["import { HttpStatusCodes } from './http-status-codes';\nimport { getLogger } from './logger.utils';\nimport { ErrorResponse } from './response.utils';\n\n/**\n * Generic HTTP error class used for custom exceptions with any status code.\n * Inherit this when you need to throw errors dynamically at runtime.\n */\nexport class HttpError extends ErrorResponse {\n /**\n * Creates a new HTTP error instance.\n * @param status - A valid HTTP status code (e.g., 400, 500).\n * @param message - The error message to return to the client.\n */\n constructor(status: number, message: string) {\n super(message);\n this.status = status;\n }\n}\n\n/**\n * Represents a 500 Internal Server Error.\n * Use this for unexpected backend errors that are not caused by the client.\n */\nexport class InternalServerErrorException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Internal server error\".\n */\n constructor(message: string = 'Internal server error') {\n super(message);\n this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;\n }\n}\n\n/**\n * Represents a 401 Unauthorized Error.\n * Indicates that authentication is required and has failed or not been provided.\n */\nexport class UnauthorizedException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unauthorized\".\n */\n constructor(message: string = 'Unauthorized') {\n super(message);\n this.status = HttpStatusCodes.UNAUTHORIZED;\n }\n}\n\n/**\n * Represents a 400 Bad Request Error.\n * Indicates that the client has sent invalid data (e.g., malformed request body).\n */\nexport class BadRequestException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad request\".\n */\n constructor(message: string = 'Bad request') {\n super(message);\n this.status = HttpStatusCodes.BAD_REQUEST;\n }\n}\n\n/**\n * Represents a 404 Not Found Error.\n * Used when a requested resource (e.g., user, file) could not be located.\n */\nexport class NotFoundException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Resource not found\".\n */\n constructor(message: string = 'Resource not found') {\n super(message);\n this.status = HttpStatusCodes.NOT_FOUND;\n }\n}\n\n/**\n * Represents a 403 Forbidden Error.\n * Indicates that the client is authenticated but not allowed to access the resource.\n */\nexport class ForbiddenException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Forbidden\".\n */\n constructor(message: string = 'Forbidden') {\n super(message);\n this.status = HttpStatusCodes.FORBIDDEN;\n }\n}\n\n/**\n * Represents a 409 Conflict Error.\n * Commonly used when a resource already exists or a versioning conflict occurs.\n */\nexport class ConflictException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Conflict\".\n */\n constructor(message: string = 'Conflict') {\n super(message);\n this.status = HttpStatusCodes.CONFLICT;\n }\n}\n\n/**\n * Represents a 502 Bad Gateway Error.\n * Used when the server receives an invalid response from an upstream server.\n */\nexport class BadGatewayException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad Gateway\".\n */\n constructor(message: string = 'Bad Gateway') {\n super(message);\n this.status = HttpStatusCodes.BAD_GATEWAY;\n }\n}\n\n/**\n * Represents a 429 Too Many Requests Error.\n * Returned when the client has hit a rate limit.\n */\nexport class TooManyRequestsException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Too many requests\".\n */\n constructor(message: string = 'Too many requests') {\n super(message);\n this.status = HttpStatusCodes.TOO_MANY_REQUESTS;\n }\n}\n\n/**\n * Represents a 503 Service Unavailable Error.\n * Indicates that the server is temporarily unavailable (e.g., for maintenance).\n */\nexport class ServiceUnavailableException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Service Unavailable\".\n */\n constructor(message: string = 'Service Unavailable') {\n super(message);\n this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;\n }\n}\n\n/**\n * Represents a 504 Gateway Timeout Error.\n * Returned when the server acting as a gateway times out waiting for a response.\n */\nexport class GatewayTimeoutException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Gateway Timeout\".\n */\n constructor(message: string = 'Gateway Timeout') {\n super(message);\n this.status = HttpStatusCodes.GATEWAY_TIMEOUT;\n }\n}\n\n/**\n * Represents a 422 Unprocessable Entity Error.\n * Used when the server understands the content type but cannot process the contained instructions.\n */\nexport class UnprocessableEntityException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unprocessable Entity\".\n * @param details - Optional validation details or additional error context.\n */\n constructor(\n message: string = 'Unprocessable Entity',\n public details?: Record<string, any>\n ) {\n super(message);\n this.status = HttpStatusCodes.UNPROCESSABLE_ENTITY;\n }\n}\n\n/**\n * Represents a 405 Method Not Allowed Error.\n * Used when the request method is not supported for the requested resource.\n */\nexport class MethodNotAllowedException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Method Not Allowed\".\n * @param allowedMethods - Optional array of allowed HTTP methods.\n */\n constructor(\n message: string = 'Method Not Allowed',\n public allowedMethods?: string[]\n ) {\n super(message);\n this.status = HttpStatusCodes.METHOD_NOT_ALLOWED;\n }\n}\n\n/**\n * Represents a 406 Not Acceptable Error.\n * Used when the server cannot produce a response matching the client's accepted content types.\n */\nexport class NotAcceptableException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Not Acceptable\".\n */\n constructor(message: string = 'Not Acceptable') {\n super(message);\n this.status = HttpStatusCodes.NOT_ACCEPTABLE;\n }\n}\n\n/**\n * Represents a 408 Request Timeout Error.\n * Used when the server times out waiting for the client to send a request.\n */\nexport class RequestTimeoutException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Request Timeout\".\n */\n constructor(message: string = 'Request Timeout') {\n super(message);\n this.status = HttpStatusCodes.REQUEST_TIMEOUT;\n }\n}\n\n/**\n * Represents a 415 Unsupported Media Type Error.\n * Used when the request's Content-Type is not supported.\n */\nexport class UnsupportedMediaTypeException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unsupported Media Type\".\n */\n constructor(message: string = 'Unsupported Media Type') {\n super(message);\n this.status = HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE;\n }\n}\n\n/**\n * Represents a 413 Payload Too Large Error.\n * Used when the request body exceeds server limits.\n */\nexport class PayloadTooLargeException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Payload Too Large\".\n */\n constructor(message: string = 'Payload Too Large') {\n super(message);\n this.status = HttpStatusCodes.PAYLOAD_TOO_LARGE;\n }\n}\n\n/**\n * Represents a 507 Insufficient Storage Error.\n * Used when the server has insufficient storage to complete the request.\n */\nexport class InsufficientStorageException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Insufficient Storage\".\n */\n constructor(message: string = 'Insufficient Storage') {\n super(message);\n this.status = HttpStatusCodes.INSUFFICIENT_STORAGE;\n }\n}\n\n/**\n * Checks if an error is an instance of HttpError or its subclasses.\n *\n * @param error - The error to check.\n * @returns True if the error is an HttpError, false otherwise.\n */\nexport function isHttpError(error: unknown): error is ErrorResponse {\n return error instanceof ErrorResponse;\n}\n\n/**\n * Creates an HTTP error with the specified status code and message.\n * Factory function for creating appropriate HTTP error instances.\n *\n * @param status - HTTP status code.\n * @param message - Optional custom error message.\n * @returns An instance of the appropriate HTTP error class.\n */\nexport function createHttpError(status: number, message?: string): ErrorResponse {\n switch (status) {\n case HttpStatusCodes.BAD_REQUEST:\n return new BadRequestException(message);\n case HttpStatusCodes.UNAUTHORIZED:\n return new UnauthorizedException(message);\n case HttpStatusCodes.FORBIDDEN:\n return new ForbiddenException(message);\n case HttpStatusCodes.NOT_FOUND:\n return new NotFoundException(message);\n case HttpStatusCodes.METHOD_NOT_ALLOWED:\n return new MethodNotAllowedException(message);\n case HttpStatusCodes.NOT_ACCEPTABLE:\n return new NotAcceptableException(message);\n case HttpStatusCodes.REQUEST_TIMEOUT:\n return new RequestTimeoutException(message);\n case HttpStatusCodes.CONFLICT:\n return new ConflictException(message);\n case HttpStatusCodes.GONE:\n return new HttpError(HttpStatusCodes.GONE, message || 'Gone');\n case HttpStatusCodes.PAYLOAD_TOO_LARGE:\n return new PayloadTooLargeException(message);\n case HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE:\n return new UnsupportedMediaTypeException(message);\n case HttpStatusCodes.UNPROCESSABLE_ENTITY:\n return new UnprocessableEntityException(message);\n case HttpStatusCodes.TOO_MANY_REQUESTS:\n return new TooManyRequestsException(message);\n case HttpStatusCodes.INTERNAL_SERVER_ERROR:\n return new InternalServerErrorException(message);\n case HttpStatusCodes.BAD_GATEWAY:\n return new BadGatewayException(message);\n case HttpStatusCodes.SERVICE_UNAVAILABLE:\n return new ServiceUnavailableException(message);\n case HttpStatusCodes.GATEWAY_TIMEOUT:\n return new GatewayTimeoutException(message);\n case HttpStatusCodes.INSUFFICIENT_STORAGE:\n return new InsufficientStorageException(message);\n default:\n return new HttpError(status, message || `HTTP Error ${status}`);\n }\n}\n\n/**\n * Type guard to check if an object has specific error properties.\n *\n * @param error - The object to check.\n * @returns True if object has error-like structure.\n */\nexport function hasErrorShape(error: unknown): error is { message: string; status?: number; code?: string } {\n return (\n typeof error === 'object' && error !== null && 'message' in error && typeof (error as any).message === 'string'\n );\n}\n\n/**\n * Safely extracts message from any error type.\n *\n * @param error - Any error object or value.\n * @returns A string error message.\n */\nexport function getErrorMessage(error: unknown): string {\n if (typeof error === 'string') {\n return error;\n }\n\n if (error instanceof Error) {\n return error.message;\n }\n\n if (hasErrorShape(error)) {\n return error.message;\n }\n\n return 'Unknown error occurred';\n}\n\n/**\n * Wraps a function and transforms any errors into HTTP errors.\n * Useful for API handlers to ensure consistent error responses.\n *\n * @param handler - The async function to wrap.\n * @returns A function that always returns or throws an HTTP error.\n */\nexport function withErrorHandling<T extends (...args: any[]) => Promise<any>>(\n handler: T\n): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> {\n return async (...args: Parameters<T>) => {\n try {\n return await handler(...args);\n } catch (error) {\n if (isHttpError(error)) {\n throw error;\n }\n\n // Log the original error (consider using a proper logger)\n getLogger().error('Caught error in handler:', error);\n\n // Extract status code if it exists, otherwise use 500\n let status = HttpStatusCodes.INTERNAL_SERVER_ERROR;\n if (hasErrorShape(error) && typeof error.status === 'number') {\n status = error.status;\n }\n\n // Create appropriate HTTP error\n throw createHttpError(status, getErrorMessage(error));\n }\n };\n}\n"]}
|