@web-ts-toolkit/http-errors 0.0.2

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/index.mjs ADDED
@@ -0,0 +1,372 @@
1
+ // src/status.ts
2
+ var canonicalStatusByHttpStatus = {
3
+ 400: "INVALID_ARGUMENT",
4
+ 401: "UNAUTHENTICATED",
5
+ 403: "PERMISSION_DENIED",
6
+ 404: "NOT_FOUND",
7
+ 409: "ABORTED",
8
+ 412: "FAILED_PRECONDITION",
9
+ 429: "RESOURCE_EXHAUSTED",
10
+ 500: "INTERNAL",
11
+ 501: "UNIMPLEMENTED",
12
+ 503: "UNAVAILABLE",
13
+ 504: "DEADLINE_EXCEEDED"
14
+ };
15
+ var getCanonicalStatus = (statusCode) => canonicalStatusByHttpStatus[statusCode] || "UNKNOWN";
16
+
17
+ // src/serialize.ts
18
+ var createAip193ErrorInfoDetail = (reason, domain, metadata) => ({
19
+ type: "error_info",
20
+ reason,
21
+ domain,
22
+ ...metadata ? { metadata } : {}
23
+ });
24
+ var toAip193ErrorPayload = (error, fallbackDomain = "http-errors") => {
25
+ const status = error.status || getCanonicalStatus(error.statusCode);
26
+ const details = [
27
+ createAip193ErrorInfoDetail(error.reason || status, error.domain || fallbackDomain, error.metadata),
28
+ ...error.errors !== void 0 ? [{ type: "bad_request", errors: error.errors }] : [],
29
+ ...error.details || []
30
+ ];
31
+ return {
32
+ error: {
33
+ code: error.statusCode,
34
+ status,
35
+ message: error.message,
36
+ ...details.length > 0 ? { details } : {}
37
+ }
38
+ };
39
+ };
40
+
41
+ // src/messages.ts
42
+ var messages = {
43
+ 400: "The server cannot process the request due to a client error",
44
+ 401: "The user is not authorized",
45
+ 403: "The server refused to authorize the request",
46
+ 404: "The server did not find a current representation for the target resource",
47
+ 405: "The method received is not allowed",
48
+ 406: "The request is not acceptable to the user agent",
49
+ 407: "The client needs to authenticate itself in order to use a proxy",
50
+ 408: "The request was not completed in the expected time",
51
+ 409: "The request was not completed due to a conflict with the target resource",
52
+ 410: "The target resource is no longer available at the origin server",
53
+ 411: "The server refuses to accept the request without a defined Content-Length",
54
+ 412: "One or more conditions given in the request header fields evaluated to false",
55
+ 413: "The request payload is too large",
56
+ 414: "The request target is too long",
57
+ 415: "The payload is in a format not supported",
58
+ 416: "None of the ranges in the request's Range header field overlap the current extent of the selected resource",
59
+ 417: "The expectation given in the request's Expect header field could not be met",
60
+ 418: "I'm a teapot",
61
+ 421: "The request was directed at a server that is not able to produce a response",
62
+ 422: "The server is unable to process the request",
63
+ 423: "The source or destination resource of a method is locked",
64
+ 424: "The requested action depended on another action",
65
+ 426: "This service requires use of a different protocol",
66
+ 428: "This request is required to be conditional",
67
+ 429: "The user has sent too many requests in a given amount of time",
68
+ 431: "Request header fields too large",
69
+ 451: "Denied access due to a consequence of a legal demand",
70
+ 500: "The server encountered an unexpected condition",
71
+ 501: "The server does not support the functionality required to fulfill the request",
72
+ 502: "The server received an invalid response from an upstream server",
73
+ 503: "The server is temporarily unable to handle the request",
74
+ 504: "The server did not receive a timely response from an upstream server",
75
+ 505: "The server does not support the HTTP protocol version used in the request",
76
+ 506: "The server has an internal configuration error",
77
+ 507: "The server is unable to store the representation needed to complete the request",
78
+ 508: "The server detected an infinite loop while processing the request",
79
+ 510: "Further extensions to the request are required for the server to fulfill it",
80
+ 511: "The client needs to authenticate to gain network access"
81
+ };
82
+ var getDefaultMessage = (statusCode) => messages[statusCode] || messages[500];
83
+
84
+ // src/base.ts
85
+ var ErrorCtor = Error;
86
+ var toMetadata = (metadata) => {
87
+ if (!metadata) {
88
+ return void 0;
89
+ }
90
+ return Object.entries(metadata).reduce((result, [key, value]) => {
91
+ result[key] = String(value);
92
+ return result;
93
+ }, {});
94
+ };
95
+ var HttpError = class extends Error {
96
+ constructor(statusCode = 500, message, options = {}) {
97
+ super(message || getDefaultMessage(statusCode), options);
98
+ if (ErrorCtor.captureStackTrace) {
99
+ ErrorCtor.captureStackTrace(this, this.constructor);
100
+ }
101
+ this.name = this.constructor.name;
102
+ this.statusCode = statusCode;
103
+ this.status = options.status || getCanonicalStatus(statusCode);
104
+ this.date = /* @__PURE__ */ new Date();
105
+ if (options.reason) {
106
+ this.reason = options.reason;
107
+ }
108
+ if (options.domain) {
109
+ this.domain = options.domain;
110
+ }
111
+ if (options.metadata) {
112
+ this.metadata = toMetadata(options.metadata);
113
+ }
114
+ if (options.details) {
115
+ this.details = options.details;
116
+ }
117
+ if (options.errors !== void 0) {
118
+ this.errors = options.errors;
119
+ }
120
+ }
121
+ };
122
+ var ClientError = class extends HttpError {
123
+ constructor(statusCode = 400, message, options = {}) {
124
+ super(statusCode, message, options);
125
+ }
126
+ };
127
+ var ServerError = class extends HttpError {
128
+ constructor(statusCode = 500, message, options = {}) {
129
+ super(statusCode, message, options);
130
+ }
131
+ };
132
+
133
+ // src/client-errors.ts
134
+ var BadRequestError = class extends ClientError {
135
+ constructor(message, options = {}) {
136
+ super(400, message, options);
137
+ }
138
+ };
139
+ var UnauthorizedError = class extends ClientError {
140
+ constructor(message, options = {}) {
141
+ super(401, message, options);
142
+ }
143
+ };
144
+ var ForbiddenError = class extends ClientError {
145
+ constructor(message, options = {}) {
146
+ super(403, message, options);
147
+ }
148
+ };
149
+ var NotFoundError = class extends ClientError {
150
+ constructor(message, options = {}) {
151
+ super(404, message, options);
152
+ }
153
+ };
154
+ var MethodNotAllowedError = class extends ClientError {
155
+ constructor(message, options = {}) {
156
+ super(405, message, options);
157
+ }
158
+ };
159
+ var NotAcceptableError = class extends ClientError {
160
+ constructor(message, options = {}) {
161
+ super(406, message, options);
162
+ }
163
+ };
164
+ var ProxyAuthRequiredError = class extends ClientError {
165
+ constructor(message, options = {}) {
166
+ super(407, message, options);
167
+ }
168
+ };
169
+ var RequestTimeoutError = class extends ClientError {
170
+ constructor(message, options = {}) {
171
+ super(408, message, options);
172
+ }
173
+ };
174
+ var ConflictError = class extends ClientError {
175
+ constructor(message, options = {}) {
176
+ super(409, message, options);
177
+ }
178
+ };
179
+ var GoneError = class extends ClientError {
180
+ constructor(message, options = {}) {
181
+ super(410, message, options);
182
+ }
183
+ };
184
+ var LengthRequiredError = class extends ClientError {
185
+ constructor(message, options = {}) {
186
+ super(411, message, options);
187
+ }
188
+ };
189
+ var PreconditionFailedError = class extends ClientError {
190
+ constructor(message, options = {}) {
191
+ super(412, message, options);
192
+ }
193
+ };
194
+ var PayloadTooLargeError = class extends ClientError {
195
+ constructor(message, options = {}) {
196
+ super(413, message, options);
197
+ }
198
+ };
199
+ var UriTooLongError = class extends ClientError {
200
+ constructor(message, options = {}) {
201
+ super(414, message, options);
202
+ }
203
+ };
204
+ var UnsupportedMediaTypeError = class extends ClientError {
205
+ constructor(message, options = {}) {
206
+ super(415, message, options);
207
+ }
208
+ };
209
+ var RequestedRangeNotSatisfiableError = class extends ClientError {
210
+ constructor(message, options = {}) {
211
+ super(416, message, options);
212
+ }
213
+ };
214
+ var ExpectationFailedError = class extends ClientError {
215
+ constructor(message, options = {}) {
216
+ super(417, message, options);
217
+ }
218
+ };
219
+ var TeapotError = class extends ClientError {
220
+ constructor(message, options = {}) {
221
+ super(418, message, options);
222
+ }
223
+ };
224
+ var MisdirectedRequestError = class extends ClientError {
225
+ constructor(message, options = {}) {
226
+ super(421, message, options);
227
+ }
228
+ };
229
+ var UnprocessableEntityError = class extends ClientError {
230
+ constructor(message, options = {}) {
231
+ super(422, message, options);
232
+ }
233
+ };
234
+ var LockedError = class extends ClientError {
235
+ constructor(message, options = {}) {
236
+ super(423, message, options);
237
+ }
238
+ };
239
+ var FailedDependencyError = class extends ClientError {
240
+ constructor(message, options = {}) {
241
+ super(424, message, options);
242
+ }
243
+ };
244
+ var UpgradeRequiredError = class extends ClientError {
245
+ constructor(message, options = {}) {
246
+ super(426, message, options);
247
+ }
248
+ };
249
+ var PreconditionRequiredError = class extends ClientError {
250
+ constructor(message, options = {}) {
251
+ super(428, message, options);
252
+ }
253
+ };
254
+ var TooManyRequestsError = class extends ClientError {
255
+ constructor(message, options = {}) {
256
+ super(429, message, options);
257
+ }
258
+ };
259
+ var RequestHeaderFieldsTooLargeError = class extends ClientError {
260
+ constructor(message, options = {}) {
261
+ super(431, message, options);
262
+ }
263
+ };
264
+ var UnavailableForLegalReasonsError = class extends ClientError {
265
+ constructor(message, options = {}) {
266
+ super(451, message, options);
267
+ }
268
+ };
269
+
270
+ // src/server-errors.ts
271
+ var InternalServerError = class extends ServerError {
272
+ constructor(message, options = {}) {
273
+ super(500, message, options);
274
+ }
275
+ };
276
+ var NotImplementedError = class extends ServerError {
277
+ constructor(message, options = {}) {
278
+ super(501, message, options);
279
+ }
280
+ };
281
+ var BadGatewayError = class extends ServerError {
282
+ constructor(message, options = {}) {
283
+ super(502, message, options);
284
+ }
285
+ };
286
+ var ServiceUnavailableError = class extends ServerError {
287
+ constructor(message, options = {}) {
288
+ super(503, message, options);
289
+ }
290
+ };
291
+ var GatewayTimeoutError = class extends ServerError {
292
+ constructor(message, options = {}) {
293
+ super(504, message, options);
294
+ }
295
+ };
296
+ var HttpVersionNotSupportedError = class extends ServerError {
297
+ constructor(message, options = {}) {
298
+ super(505, message, options);
299
+ }
300
+ };
301
+ var VariantAlsoNegotiatesError = class extends ServerError {
302
+ constructor(message, options = {}) {
303
+ super(506, message, options);
304
+ }
305
+ };
306
+ var InsufficientStorageError = class extends ServerError {
307
+ constructor(message, options = {}) {
308
+ super(507, message, options);
309
+ }
310
+ };
311
+ var LoopDetectedError = class extends ServerError {
312
+ constructor(message, options = {}) {
313
+ super(508, message, options);
314
+ }
315
+ };
316
+ var NotExtendedError = class extends ServerError {
317
+ constructor(message, options = {}) {
318
+ super(510, message, options);
319
+ }
320
+ };
321
+ var NetworkAuthenticationRequiredError = class extends ServerError {
322
+ constructor(message, options = {}) {
323
+ super(511, message, options);
324
+ }
325
+ };
326
+ export {
327
+ BadGatewayError,
328
+ BadRequestError,
329
+ ClientError,
330
+ ConflictError,
331
+ ExpectationFailedError,
332
+ FailedDependencyError,
333
+ ForbiddenError,
334
+ GatewayTimeoutError,
335
+ GoneError,
336
+ HttpError,
337
+ HttpVersionNotSupportedError,
338
+ InsufficientStorageError,
339
+ InternalServerError,
340
+ LengthRequiredError,
341
+ LockedError,
342
+ LoopDetectedError,
343
+ MethodNotAllowedError,
344
+ MisdirectedRequestError,
345
+ NetworkAuthenticationRequiredError,
346
+ NotAcceptableError,
347
+ NotExtendedError,
348
+ NotFoundError,
349
+ NotImplementedError,
350
+ PayloadTooLargeError,
351
+ PreconditionFailedError,
352
+ PreconditionRequiredError,
353
+ ProxyAuthRequiredError,
354
+ RequestHeaderFieldsTooLargeError,
355
+ RequestTimeoutError,
356
+ RequestedRangeNotSatisfiableError,
357
+ ServerError,
358
+ ServiceUnavailableError,
359
+ TeapotError,
360
+ TooManyRequestsError,
361
+ UnauthorizedError,
362
+ UnavailableForLegalReasonsError,
363
+ UnprocessableEntityError,
364
+ UnsupportedMediaTypeError,
365
+ UpgradeRequiredError,
366
+ UriTooLongError,
367
+ VariantAlsoNegotiatesError,
368
+ canonicalStatusByHttpStatus,
369
+ createAip193ErrorInfoDetail,
370
+ getCanonicalStatus,
371
+ toAip193ErrorPayload
372
+ };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@web-ts-toolkit/http-errors",
3
+ "description": "HTTP error classes for backend APIs",
4
+ "version": "0.0.2",
5
+ "keywords": [
6
+ "http-errors",
7
+ "api-errors"
8
+ ],
9
+ "main": "./index.js",
10
+ "module": "./index.mjs",
11
+ "types": "./index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./index.d.ts",
15
+ "import": "./index.mjs",
16
+ "require": "./index.js",
17
+ "default": "./index.js"
18
+ }
19
+ },
20
+ "author": "Junmin Dev",
21
+ "bugs": {
22
+ "url": "https://github.com/egose/web-ts-toolkit/issues"
23
+ },
24
+ "engines": {
25
+ "node": ">=20"
26
+ },
27
+ "license": "Apache-2.0",
28
+ "repository": {
29
+ "type": "git",
30
+ "url": "git+https://github.com/egose/web-ts-toolkit.git",
31
+ "directory": "packages/http-errors"
32
+ }
33
+ }