@zero-server/sdk 0.9.1 → 0.9.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.
Files changed (126) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +460 -443
  3. package/index.js +414 -412
  4. package/lib/app.js +1172 -1172
  5. package/lib/auth/authorize.js +399 -399
  6. package/lib/auth/enrollment.js +367 -367
  7. package/lib/auth/index.js +57 -57
  8. package/lib/auth/jwt.js +731 -731
  9. package/lib/auth/oauth.js +362 -362
  10. package/lib/auth/session.js +588 -588
  11. package/lib/auth/trustedDevice.js +409 -409
  12. package/lib/auth/twoFactor.js +1150 -1150
  13. package/lib/auth/webauthn.js +946 -946
  14. package/lib/body/index.js +14 -14
  15. package/lib/body/json.js +109 -109
  16. package/lib/body/multipart.js +440 -440
  17. package/lib/body/raw.js +71 -71
  18. package/lib/body/rawBuffer.js +160 -160
  19. package/lib/body/sendError.js +25 -25
  20. package/lib/body/text.js +75 -75
  21. package/lib/body/typeMatch.js +41 -41
  22. package/lib/body/urlencoded.js +235 -235
  23. package/lib/cli.js +845 -845
  24. package/lib/cluster.js +666 -666
  25. package/lib/debug.js +372 -372
  26. package/lib/env/index.js +465 -465
  27. package/lib/errors.js +683 -683
  28. package/lib/fetch/index.js +256 -256
  29. package/lib/grpc/balancer.js +378 -378
  30. package/lib/grpc/call.js +708 -708
  31. package/lib/grpc/client.js +764 -764
  32. package/lib/grpc/codec.js +1221 -1221
  33. package/lib/grpc/credentials.js +398 -398
  34. package/lib/grpc/frame.js +262 -262
  35. package/lib/grpc/health.js +287 -287
  36. package/lib/grpc/index.js +121 -121
  37. package/lib/grpc/metadata.js +461 -461
  38. package/lib/grpc/proto.js +821 -821
  39. package/lib/grpc/reflection.js +590 -590
  40. package/lib/grpc/server.js +445 -445
  41. package/lib/grpc/status.js +118 -118
  42. package/lib/grpc/watch.js +173 -173
  43. package/lib/http/index.js +10 -10
  44. package/lib/http/request.js +727 -727
  45. package/lib/http/response.js +799 -799
  46. package/lib/lifecycle.js +557 -557
  47. package/lib/middleware/compress.js +230 -230
  48. package/lib/middleware/cookieParser.js +237 -237
  49. package/lib/middleware/cors.js +93 -93
  50. package/lib/middleware/csrf.js +137 -137
  51. package/lib/middleware/errorHandler.js +101 -101
  52. package/lib/middleware/helmet.js +175 -175
  53. package/lib/middleware/index.js +19 -17
  54. package/lib/middleware/logger.js +74 -74
  55. package/lib/middleware/rateLimit.js +88 -88
  56. package/lib/middleware/requestId.js +53 -53
  57. package/lib/middleware/static.js +326 -326
  58. package/lib/middleware/timeout.js +71 -71
  59. package/lib/middleware/validator.js +255 -255
  60. package/lib/observe/health.js +326 -326
  61. package/lib/observe/index.js +50 -50
  62. package/lib/observe/logger.js +359 -359
  63. package/lib/observe/metrics.js +805 -805
  64. package/lib/observe/tracing.js +592 -592
  65. package/lib/orm/adapters/json.js +290 -290
  66. package/lib/orm/adapters/memory.js +764 -764
  67. package/lib/orm/adapters/mongo.js +764 -764
  68. package/lib/orm/adapters/mysql.js +933 -933
  69. package/lib/orm/adapters/postgres.js +1144 -1144
  70. package/lib/orm/adapters/redis.js +1534 -1534
  71. package/lib/orm/adapters/sql-base.js +212 -212
  72. package/lib/orm/adapters/sqlite.js +858 -858
  73. package/lib/orm/audit.js +649 -649
  74. package/lib/orm/cache.js +394 -394
  75. package/lib/orm/geo.js +387 -387
  76. package/lib/orm/index.js +784 -784
  77. package/lib/orm/migrate.js +432 -432
  78. package/lib/orm/model.js +1706 -1706
  79. package/lib/orm/plugin.js +375 -375
  80. package/lib/orm/procedures.js +836 -836
  81. package/lib/orm/profiler.js +233 -233
  82. package/lib/orm/query.js +1772 -1772
  83. package/lib/orm/replicas.js +241 -241
  84. package/lib/orm/schema.js +307 -307
  85. package/lib/orm/search.js +380 -380
  86. package/lib/orm/seed/data/commerce.js +136 -136
  87. package/lib/orm/seed/data/internet.js +111 -111
  88. package/lib/orm/seed/data/locations.js +204 -204
  89. package/lib/orm/seed/data/names.js +338 -338
  90. package/lib/orm/seed/data/person.js +128 -128
  91. package/lib/orm/seed/data/phone.js +211 -211
  92. package/lib/orm/seed/data/words.js +134 -134
  93. package/lib/orm/seed/factory.js +178 -178
  94. package/lib/orm/seed/fake.js +1186 -1186
  95. package/lib/orm/seed/index.js +18 -18
  96. package/lib/orm/seed/rng.js +70 -70
  97. package/lib/orm/seed/seeder.js +124 -124
  98. package/lib/orm/seed/unique.js +68 -68
  99. package/lib/orm/snapshot.js +366 -366
  100. package/lib/orm/tenancy.js +605 -605
  101. package/lib/orm/views.js +350 -350
  102. package/lib/router/index.js +436 -436
  103. package/lib/sse/index.js +8 -8
  104. package/lib/sse/stream.js +349 -349
  105. package/lib/ws/connection.js +451 -451
  106. package/lib/ws/handshake.js +125 -125
  107. package/lib/ws/index.js +14 -14
  108. package/lib/ws/room.js +223 -223
  109. package/package.json +73 -73
  110. package/types/app.d.ts +223 -223
  111. package/types/auth.d.ts +520 -520
  112. package/types/cluster.d.ts +75 -75
  113. package/types/env.d.ts +80 -80
  114. package/types/errors.d.ts +316 -316
  115. package/types/fetch.d.ts +43 -43
  116. package/types/grpc.d.ts +432 -432
  117. package/types/index.d.ts +384 -384
  118. package/types/lifecycle.d.ts +60 -60
  119. package/types/middleware.d.ts +320 -320
  120. package/types/observe.d.ts +304 -304
  121. package/types/orm.d.ts +1887 -1887
  122. package/types/request.d.ts +109 -109
  123. package/types/response.d.ts +157 -157
  124. package/types/router.d.ts +78 -78
  125. package/types/sse.d.ts +78 -78
  126. package/types/websocket.d.ts +126 -126
package/lib/errors.js CHANGED
@@ -1,683 +1,683 @@
1
- /**
2
- * @module errors
3
- * @description HTTP error classes with status codes, error codes, and structured details.
4
- * Every error extends HttpError which carries a statusCode, code, and optional details.
5
- *
6
- * @example
7
- * const { NotFoundError, ValidationError, createError } = require('@zero-server/sdk');
8
- *
9
- * // Throw a named error class
10
- * throw new NotFoundError('User not found');
11
- *
12
- * // Attach a machine-readable code + details
13
- * throw new NotFoundError('Invoice missing', {
14
- * code: 'INVOICE_NOT_FOUND',
15
- * details: { invoiceId: 'INV-42' },
16
- * });
17
- *
18
- * // Field-level validation errors
19
- * throw new ValidationError('Invalid input', {
20
- * email: 'required',
21
- * age: 'must be >= 18',
22
- * });
23
- *
24
- * // Factory — create by status code
25
- * throw createError(503, 'Try again later');
26
- */
27
-
28
- // --- Status Text Map ---------------------------------------------
29
-
30
- const STATUS_TEXT = {
31
- 400: 'Bad Request',
32
- 401: 'Unauthorized',
33
- 402: 'Payment Required',
34
- 403: 'Forbidden',
35
- 404: 'Not Found',
36
- 405: 'Method Not Allowed',
37
- 406: 'Not Acceptable',
38
- 408: 'Request Timeout',
39
- 409: 'Conflict',
40
- 410: 'Gone',
41
- 413: 'Payload Too Large',
42
- 415: 'Unsupported Media Type',
43
- 418: "I'm a Teapot",
44
- 422: 'Unprocessable Entity',
45
- 429: 'Too Many Requests',
46
- 500: 'Internal Server Error',
47
- 501: 'Not Implemented',
48
- 502: 'Bad Gateway',
49
- 503: 'Service Unavailable',
50
- 504: 'Gateway Timeout',
51
- };
52
-
53
- // --- Base HttpError ----------------------------------------------
54
-
55
- /**
56
- * Base HTTP error class. All framework error classes extend this.
57
- */
58
- class HttpError extends Error
59
- {
60
- /**
61
- * @constructor
62
- * @param {number} statusCode - HTTP status code.
63
- * @param {string} [message] - Human-readable message.
64
- * @param {object} [opts] - Additional error options.
65
- * @param {string} [opts.code] - Machine-readable error code (e.g. 'VALIDATION_FAILED').
66
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
67
- */
68
- constructor(statusCode, message, opts = {})
69
- {
70
- super(message || STATUS_TEXT[statusCode] || 'Error');
71
- this.name = this.constructor.name;
72
- this.statusCode = statusCode;
73
- this.code = opts.code || this._defaultCode();
74
- if (opts.details !== undefined) this.details = opts.details;
75
- Error.captureStackTrace(this, this.constructor);
76
- }
77
-
78
- /** @private */
79
- _defaultCode()
80
- {
81
- return (STATUS_TEXT[this.statusCode] || 'ERROR')
82
- .toUpperCase()
83
- .replace(/[^A-Z0-9]+/g, '_')
84
- .replace(/(^_|_$)/g, '');
85
- }
86
-
87
- /**
88
- * Serialize for JSON responses.
89
- * @returns {{ error: string, code: string, statusCode: number, details?: * }}
90
- */
91
- toJSON()
92
- {
93
- const obj = { error: this.message, code: this.code, statusCode: this.statusCode };
94
- if (this.details !== undefined) obj.details = this.details;
95
- return obj;
96
- }
97
- }
98
-
99
- // --- Specific Error Classes --------------------------------------
100
-
101
- class BadRequestError extends HttpError
102
- {
103
- /**
104
- * @constructor
105
- * @param {string} [message] - Human-readable message.
106
- * @param {object} [opts] - Additional error options.
107
- * @param {string} [opts.code] - Machine-readable error code.
108
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
109
- */
110
- constructor(message, opts) { super(400, message, opts); }
111
- }
112
-
113
- class UnauthorizedError extends HttpError
114
- {
115
- /**
116
- * @constructor
117
- * @param {string} [message] - Human-readable message.
118
- * @param {object} [opts] - Additional error options.
119
- * @param {string} [opts.code] - Machine-readable error code.
120
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
121
- */
122
- constructor(message, opts) { super(401, message, opts); }
123
- }
124
-
125
- class ForbiddenError extends HttpError
126
- {
127
- /**
128
- * @constructor
129
- * @param {string} [message] - Human-readable message.
130
- * @param {object} [opts] - Additional error options.
131
- * @param {string} [opts.code] - Machine-readable error code.
132
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
133
- */
134
- constructor(message, opts) { super(403, message, opts); }
135
- }
136
-
137
- class NotFoundError extends HttpError
138
- {
139
- /**
140
- * @constructor
141
- * @param {string} [message] - Human-readable message.
142
- * @param {object} [opts] - Additional error options.
143
- * @param {string} [opts.code] - Machine-readable error code.
144
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
145
- */
146
- constructor(message, opts) { super(404, message, opts); }
147
- }
148
-
149
- class MethodNotAllowedError extends HttpError
150
- {
151
- /**
152
- * @constructor
153
- * @param {string} [message] - Human-readable message.
154
- * @param {object} [opts] - Additional error options.
155
- * @param {string} [opts.code] - Machine-readable error code.
156
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
157
- */
158
- constructor(message, opts) { super(405, message, opts); }
159
- }
160
-
161
- class ConflictError extends HttpError
162
- {
163
- /**
164
- * @constructor
165
- * @param {string} [message] - Human-readable message.
166
- * @param {object} [opts] - Additional error options.
167
- * @param {string} [opts.code] - Machine-readable error code.
168
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
169
- */
170
- constructor(message, opts) { super(409, message, opts); }
171
- }
172
-
173
- class GoneError extends HttpError
174
- {
175
- /**
176
- * @constructor
177
- * @param {string} [message] - Human-readable message.
178
- * @param {object} [opts] - Additional error options.
179
- * @param {string} [opts.code] - Machine-readable error code.
180
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
181
- */
182
- constructor(message, opts) { super(410, message, opts); }
183
- }
184
-
185
- class PayloadTooLargeError extends HttpError
186
- {
187
- /**
188
- * @constructor
189
- * @param {string} [message] - Human-readable message.
190
- * @param {object} [opts] - Additional error options.
191
- * @param {string} [opts.code] - Machine-readable error code.
192
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
193
- */
194
- constructor(message, opts) { super(413, message, opts); }
195
- }
196
-
197
- class UnprocessableEntityError extends HttpError
198
- {
199
- /**
200
- * @constructor
201
- * @param {string} [message] - Human-readable message.
202
- * @param {object} [opts] - Additional error options.
203
- * @param {string} [opts.code] - Machine-readable error code.
204
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
205
- */
206
- constructor(message, opts) { super(422, message, opts); }
207
- }
208
-
209
- /**
210
- * Validation error with field-level details.
211
- */
212
- class ValidationError extends HttpError
213
- {
214
- /**
215
- * @constructor
216
- * @param {string} [message] - Summary message.
217
- * @param {object|Array} [errors] - Field errors, e.g. { email: 'required', age: 'must be >= 18' }.
218
- * @param {object} [opts] - Additional error options.
219
- * @param {string} [opts.code] - Machine-readable error code.
220
- */
221
- constructor(message, errors, opts = {})
222
- {
223
- super(422, message || 'Validation Failed', { code: 'VALIDATION_FAILED', ...opts, details: errors });
224
- this.errors = errors || {};
225
- }
226
- }
227
-
228
- class TooManyRequestsError extends HttpError
229
- {
230
- /**
231
- * @constructor
232
- * @param {string} [message] - Human-readable message.
233
- * @param {object} [opts] - Additional error options.
234
- * @param {string} [opts.code] - Machine-readable error code.
235
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
236
- */
237
- constructor(message, opts) { super(429, message, opts); }
238
- }
239
-
240
- class InternalError extends HttpError
241
- {
242
- /**
243
- * @constructor
244
- * @param {string} [message] - Human-readable message.
245
- * @param {object} [opts] - Additional error options.
246
- * @param {string} [opts.code] - Machine-readable error code.
247
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
248
- */
249
- constructor(message, opts) { super(500, message, opts); }
250
- }
251
-
252
- class NotImplementedError extends HttpError
253
- {
254
- /**
255
- * @constructor
256
- * @param {string} [message] - Human-readable message.
257
- * @param {object} [opts] - Additional error options.
258
- * @param {string} [opts.code] - Machine-readable error code.
259
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
260
- */
261
- constructor(message, opts) { super(501, message, opts); }
262
- }
263
-
264
- class BadGatewayError extends HttpError
265
- {
266
- /**
267
- * @constructor
268
- * @param {string} [message] - Human-readable message.
269
- * @param {object} [opts] - Additional error options.
270
- * @param {string} [opts.code] - Machine-readable error code.
271
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
272
- */
273
- constructor(message, opts) { super(502, message, opts); }
274
- }
275
-
276
- class ServiceUnavailableError extends HttpError
277
- {
278
- /**
279
- * @constructor
280
- * @param {string} [message] - Human-readable message.
281
- * @param {object} [opts] - Additional error options.
282
- * @param {string} [opts.code] - Machine-readable error code.
283
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
284
- */
285
- constructor(message, opts) { super(503, message, opts); }
286
- }
287
-
288
- // --- Framework Error Classes -------------------------------------
289
-
290
- /**
291
- * Database / ORM error — wraps adapter-level failures.
292
- */
293
- class DatabaseError extends HttpError
294
- {
295
- /**
296
- * @constructor
297
- * @param {string} [message] - Description of the DB failure.
298
- * @param {object} [opts] - Additional error options.
299
- * @param {string} [opts.query] - The query or operation that failed.
300
- * @param {string} [opts.adapter] - Adapter name (sqlite, mysql, etc.).
301
- */
302
- constructor(message, opts = {})
303
- {
304
- super(500, message || 'Database Error', { code: 'DATABASE_ERROR', ...opts });
305
- if (opts.query) this.query = opts.query;
306
- if (opts.adapter) this.adapter = opts.adapter;
307
- }
308
- }
309
-
310
- /**
311
- * Configuration error — thrown when app/adapter configuration is invalid.
312
- */
313
- class ConfigurationError extends HttpError
314
- {
315
- /**
316
- * @constructor
317
- * @param {string} [message] - What misconfiguration was detected.
318
- * @param {object} [opts] - Additional error options.
319
- * @param {string} [opts.setting] - The setting key that's invalid.
320
- */
321
- constructor(message, opts = {})
322
- {
323
- super(500, message || 'Configuration Error', { code: 'CONFIGURATION_ERROR', ...opts });
324
- if (opts.setting) this.setting = opts.setting;
325
- }
326
- }
327
-
328
- /**
329
- * Middleware error — a middleware function failed unexpectedly.
330
- */
331
- class MiddlewareError extends HttpError
332
- {
333
- /**
334
- * @constructor
335
- * @param {string} [message] - Description.
336
- * @param {object} [opts] - Additional error options.
337
- * @param {string} [opts.middleware] - Middleware name.
338
- */
339
- constructor(message, opts = {})
340
- {
341
- super(500, message || 'Middleware Error', { code: 'MIDDLEWARE_ERROR', ...opts });
342
- if (opts.middleware) this.middleware = opts.middleware;
343
- }
344
- }
345
-
346
- /**
347
- * Routing error — thrown when route resolution fails.
348
- */
349
- class RoutingError extends HttpError
350
- {
351
- /**
352
- * @constructor
353
- * @param {string} [message] - Description.
354
- * @param {object} [opts] - Additional error options.
355
- * @param {string} [opts.path] - The route path.
356
- * @param {string} [opts.method] - The HTTP method.
357
- */
358
- constructor(message, opts = {})
359
- {
360
- super(500, message || 'Routing Error', { code: 'ROUTING_ERROR', ...opts });
361
- if (opts.path) this.path = opts.path;
362
- if (opts.method) this.method = opts.method;
363
- }
364
- }
365
-
366
- /**
367
- * Timeout error — operation exceeded allowed time.
368
- */
369
- class TimeoutError extends HttpError
370
- {
371
- /**
372
- * @constructor
373
- * @param {string} [message] - Description.
374
- * @param {object} [opts] - Additional error options.
375
- * @param {number} [opts.timeout] - The timeout limit in ms.
376
- */
377
- constructor(message, opts = {})
378
- {
379
- super(408, message || 'Request Timeout', { code: 'TIMEOUT', ...opts });
380
- if (opts.timeout) this.timeout = opts.timeout;
381
- }
382
- }
383
-
384
- // --- ORM-Specific Error Classes ----------------------------------
385
-
386
- /**
387
- * Connection error — database connection failures with retry context.
388
- */
389
- class ConnectionError extends DatabaseError
390
- {
391
- /**
392
- * @constructor
393
- * @param {string} [message] - Description.
394
- * @param {object} [opts] - Additional error options.
395
- * @param {string} [opts.adapter] - Adapter name.
396
- * @param {number} [opts.attempt] - Retry attempt number.
397
- * @param {number} [opts.maxRetries] - Maximum retries configured.
398
- * @param {string} [opts.host] - Target host.
399
- * @param {number} [opts.port] - Target port.
400
- */
401
- constructor(message, opts = {})
402
- {
403
- super(message || 'Connection Failed', { code: 'CONNECTION_ERROR', ...opts });
404
- if (opts.attempt !== undefined) this.attempt = opts.attempt;
405
- if (opts.maxRetries !== undefined) this.maxRetries = opts.maxRetries;
406
- if (opts.host) this.host = opts.host;
407
- if (opts.port) this.port = opts.port;
408
- }
409
- }
410
-
411
- /**
412
- * Migration error — migration execution failures.
413
- */
414
- class MigrationError extends DatabaseError
415
- {
416
- /**
417
- * @constructor
418
- * @param {string} [message] - Description.
419
- * @param {object} [opts] - Additional error options.
420
- * @param {string} [opts.migration] - Migration name that failed.
421
- * @param {string} [opts.direction] - 'up' or 'down'.
422
- * @param {number} [opts.batch] - Batch number.
423
- */
424
- constructor(message, opts = {})
425
- {
426
- super(message || 'Migration Failed', { code: 'MIGRATION_ERROR', ...opts });
427
- if (opts.migration) this.migration = opts.migration;
428
- if (opts.direction) this.direction = opts.direction;
429
- if (opts.batch !== undefined) this.batch = opts.batch;
430
- }
431
- }
432
-
433
- /**
434
- * Transaction error — transaction commit/rollback failures.
435
- */
436
- class TransactionError extends DatabaseError
437
- {
438
- /**
439
- * @constructor
440
- * @param {string} [message] - Description.
441
- * @param {object} [opts] - Additional error options.
442
- * @param {string} [opts.phase] - 'begin', 'commit', or 'rollback'.
443
- */
444
- constructor(message, opts = {})
445
- {
446
- super(message || 'Transaction Failed', { code: 'TRANSACTION_ERROR', ...opts });
447
- if (opts.phase) this.phase = opts.phase;
448
- }
449
- }
450
-
451
- /**
452
- * Query error — query execution failures with SQL context.
453
- */
454
- class QueryError extends DatabaseError
455
- {
456
- /**
457
- * @constructor
458
- * @param {string} [message] - Description.
459
- * @param {object} [opts] - Additional error options.
460
- * @param {string} [opts.sql] - The SQL that failed (if applicable).
461
- * @param {Array} [opts.params] - Query parameters.
462
- * @param {string} [opts.table] - Target table.
463
- */
464
- constructor(message, opts = {})
465
- {
466
- super(message || 'Query Failed', { code: 'QUERY_ERROR', ...opts });
467
- if (opts.sql) this.sql = opts.sql;
468
- if (opts.params) this.params = opts.params;
469
- if (opts.table) this.table = opts.table;
470
- }
471
- }
472
-
473
- /**
474
- * Adapter error — adapter-level issues (driver not found, unsupported operation).
475
- */
476
- class AdapterError extends DatabaseError
477
- {
478
- /**
479
- * @constructor
480
- * @param {string} [message] - Description.
481
- * @param {object} [opts] - Additional error options.
482
- * @param {string} [opts.adapter] - Adapter name.
483
- * @param {string} [opts.operation] - The operation that failed.
484
- */
485
- constructor(message, opts = {})
486
- {
487
- super(message || 'Adapter Error', { code: 'ADAPTER_ERROR', ...opts });
488
- if (opts.operation) this.operation = opts.operation;
489
- }
490
- }
491
-
492
- /**
493
- * Cache error — caching layer failures.
494
- */
495
- class CacheError extends HttpError
496
- {
497
- /**
498
- * @constructor
499
- * @param {string} [message] - Description.
500
- * @param {object} [opts] - Additional error options.
501
- * @param {string} [opts.operation] - Cache operation that failed.
502
- * @param {string} [opts.key] - Cache key involved.
503
- */
504
- constructor(message, opts = {})
505
- {
506
- super(500, message || 'Cache Error', { code: 'CACHE_ERROR', ...opts });
507
- if (opts.operation) this.operation = opts.operation;
508
- if (opts.key) this.key = opts.key;
509
- }
510
- }
511
-
512
- // --- Phase 4 Error Classes ---------------------------------------
513
-
514
- /**
515
- * Tenancy error — multi-tenancy operation failures.
516
- */
517
- class TenancyError extends DatabaseError
518
- {
519
- /**
520
- * @constructor
521
- * @param {string} [message] - Description.
522
- * @param {object} [opts] - Additional error options.
523
- * @param {string} [opts.tenant] - Tenant identifier.
524
- * @param {string} [opts.strategy] - Tenancy strategy (row, schema).
525
- */
526
- constructor(message, opts = {})
527
- {
528
- super(message || 'Tenancy Error', { code: 'TENANCY_ERROR', ...opts });
529
- if (opts.tenant) this.tenant = opts.tenant;
530
- if (opts.strategy) this.strategy = opts.strategy;
531
- }
532
- }
533
-
534
- /**
535
- * Audit error — audit logging failures.
536
- */
537
- class AuditError extends DatabaseError
538
- {
539
- /**
540
- * @constructor
541
- * @param {string} [message] - Description.
542
- * @param {object} [opts] - Additional error options.
543
- * @param {string} [opts.action] - Audit action that failed.
544
- * @param {string} [opts.table] - Target table.
545
- */
546
- constructor(message, opts = {})
547
- {
548
- super(message || 'Audit Error', { code: 'AUDIT_ERROR', ...opts });
549
- if (opts.action) this.action = opts.action;
550
- if (opts.table) this.table = opts.table;
551
- }
552
- }
553
-
554
- /**
555
- * Plugin error — plugin registration or lifecycle failures.
556
- */
557
- class PluginError extends HttpError
558
- {
559
- /**
560
- * @constructor
561
- * @param {string} [message] - Description.
562
- * @param {object} [opts] - Additional error options.
563
- * @param {string} [opts.plugin] - Plugin name.
564
- * @param {string} [opts.phase] - Lifecycle phase (install, boot, uninstall).
565
- */
566
- constructor(message, opts = {})
567
- {
568
- super(500, message || 'Plugin Error', { code: 'PLUGIN_ERROR', ...opts });
569
- if (opts.plugin) this.plugin = opts.plugin;
570
- if (opts.phase) this.phase = opts.phase;
571
- }
572
- }
573
-
574
- /**
575
- * Procedure error — stored procedure/function failures.
576
- */
577
- class ProcedureError extends DatabaseError
578
- {
579
- /**
580
- * @constructor
581
- * @param {string} [message] - Description.
582
- * @param {object} [opts] - Additional error options.
583
- * @param {string} [opts.procedure] - Procedure or function name.
584
- * @param {string} [opts.operation] - Operation (create, drop, execute).
585
- */
586
- constructor(message, opts = {})
587
- {
588
- super(message || 'Procedure Error', { code: 'PROCEDURE_ERROR', ...opts });
589
- if (opts.procedure) this.procedure = opts.procedure;
590
- if (opts.operation) this.operation = opts.operation;
591
- }
592
- }
593
-
594
- // --- Factory -----------------------------------------------------
595
-
596
- /**
597
- * Create an HttpError by status code.
598
- *
599
- * @param {number} statusCode - HTTP status code.
600
- * @param {string} [message] - Human-readable error message.
601
- * @param {object} [opts] - Additional error options.
602
- * @param {string} [opts.code] - Machine-readable error code.
603
- * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
604
- * @returns {HttpError} Matching HttpError subclass instance.
605
- *
606
- * @example
607
- * throw createError(404, 'User not found');
608
- * throw createError(422, 'Invalid input', { details: { email: 'required' } });
609
- */
610
- function createError(statusCode, message, opts)
611
- {
612
- const map = {
613
- 400: BadRequestError,
614
- 401: UnauthorizedError,
615
- 403: ForbiddenError,
616
- 404: NotFoundError,
617
- 405: MethodNotAllowedError,
618
- 408: TimeoutError,
619
- 409: ConflictError,
620
- 410: GoneError,
621
- 413: PayloadTooLargeError,
622
- 422: UnprocessableEntityError,
623
- 429: TooManyRequestsError,
624
- 500: InternalError,
625
- 501: NotImplementedError,
626
- 502: BadGatewayError,
627
- 503: ServiceUnavailableError,
628
- };
629
-
630
- const Cls = map[statusCode];
631
- if (Cls) return new Cls(message, opts);
632
- return new HttpError(statusCode, message, opts);
633
- }
634
-
635
- /**
636
- * Check if a value is an HttpError (or duck-typed equivalent).
637
- * @param {*} err - Error object.
638
- * @returns {boolean} True if the value is an HttpError.
639
- */
640
- function isHttpError(err)
641
- {
642
- if (!err || !(err instanceof Error)) return false;
643
- return err instanceof HttpError || typeof err.statusCode === 'number';
644
- }
645
-
646
- module.exports = {
647
- HttpError,
648
- BadRequestError,
649
- UnauthorizedError,
650
- ForbiddenError,
651
- NotFoundError,
652
- MethodNotAllowedError,
653
- ConflictError,
654
- GoneError,
655
- PayloadTooLargeError,
656
- UnprocessableEntityError,
657
- ValidationError,
658
- TooManyRequestsError,
659
- InternalError,
660
- NotImplementedError,
661
- BadGatewayError,
662
- ServiceUnavailableError,
663
- // Framework-specific errors
664
- DatabaseError,
665
- ConfigurationError,
666
- MiddlewareError,
667
- RoutingError,
668
- TimeoutError,
669
- // ORM-specific errors
670
- ConnectionError,
671
- MigrationError,
672
- TransactionError,
673
- QueryError,
674
- AdapterError,
675
- CacheError,
676
- // Phase 4 errors
677
- TenancyError,
678
- AuditError,
679
- PluginError,
680
- ProcedureError,
681
- createError,
682
- isHttpError,
683
- };
1
+ /**
2
+ * @module errors
3
+ * @description HTTP error classes with status codes, error codes, and structured details.
4
+ * Every error extends HttpError which carries a statusCode, code, and optional details.
5
+ *
6
+ * @example
7
+ * const { NotFoundError, ValidationError, createError } = require('@zero-server/sdk');
8
+ *
9
+ * // Throw a named error class
10
+ * throw new NotFoundError('User not found');
11
+ *
12
+ * // Attach a machine-readable code + details
13
+ * throw new NotFoundError('Invoice missing', {
14
+ * code: 'INVOICE_NOT_FOUND',
15
+ * details: { invoiceId: 'INV-42' },
16
+ * });
17
+ *
18
+ * // Field-level validation errors
19
+ * throw new ValidationError('Invalid input', {
20
+ * email: 'required',
21
+ * age: 'must be >= 18',
22
+ * });
23
+ *
24
+ * // Factory — create by status code
25
+ * throw createError(503, 'Try again later');
26
+ */
27
+
28
+ // --- Status Text Map ---------------------------------------------
29
+
30
+ const STATUS_TEXT = {
31
+ 400: 'Bad Request',
32
+ 401: 'Unauthorized',
33
+ 402: 'Payment Required',
34
+ 403: 'Forbidden',
35
+ 404: 'Not Found',
36
+ 405: 'Method Not Allowed',
37
+ 406: 'Not Acceptable',
38
+ 408: 'Request Timeout',
39
+ 409: 'Conflict',
40
+ 410: 'Gone',
41
+ 413: 'Payload Too Large',
42
+ 415: 'Unsupported Media Type',
43
+ 418: "I'm a Teapot",
44
+ 422: 'Unprocessable Entity',
45
+ 429: 'Too Many Requests',
46
+ 500: 'Internal Server Error',
47
+ 501: 'Not Implemented',
48
+ 502: 'Bad Gateway',
49
+ 503: 'Service Unavailable',
50
+ 504: 'Gateway Timeout',
51
+ };
52
+
53
+ // --- Base HttpError ----------------------------------------------
54
+
55
+ /**
56
+ * Base HTTP error class. All framework error classes extend this.
57
+ */
58
+ class HttpError extends Error
59
+ {
60
+ /**
61
+ * @constructor
62
+ * @param {number} statusCode - HTTP status code.
63
+ * @param {string} [message] - Human-readable message.
64
+ * @param {object} [opts] - Additional error options.
65
+ * @param {string} [opts.code] - Machine-readable error code (e.g. 'VALIDATION_FAILED').
66
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
67
+ */
68
+ constructor(statusCode, message, opts = {})
69
+ {
70
+ super(message || STATUS_TEXT[statusCode] || 'Error');
71
+ this.name = this.constructor.name;
72
+ this.statusCode = statusCode;
73
+ this.code = opts.code || this._defaultCode();
74
+ if (opts.details !== undefined) this.details = opts.details;
75
+ Error.captureStackTrace(this, this.constructor);
76
+ }
77
+
78
+ /** @private */
79
+ _defaultCode()
80
+ {
81
+ return (STATUS_TEXT[this.statusCode] || 'ERROR')
82
+ .toUpperCase()
83
+ .replace(/[^A-Z0-9]+/g, '_')
84
+ .replace(/(^_|_$)/g, '');
85
+ }
86
+
87
+ /**
88
+ * Serialize for JSON responses.
89
+ * @returns {{ error: string, code: string, statusCode: number, details?: * }}
90
+ */
91
+ toJSON()
92
+ {
93
+ const obj = { error: this.message, code: this.code, statusCode: this.statusCode };
94
+ if (this.details !== undefined) obj.details = this.details;
95
+ return obj;
96
+ }
97
+ }
98
+
99
+ // --- Specific Error Classes --------------------------------------
100
+
101
+ class BadRequestError extends HttpError
102
+ {
103
+ /**
104
+ * @constructor
105
+ * @param {string} [message] - Human-readable message.
106
+ * @param {object} [opts] - Additional error options.
107
+ * @param {string} [opts.code] - Machine-readable error code.
108
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
109
+ */
110
+ constructor(message, opts) { super(400, message, opts); }
111
+ }
112
+
113
+ class UnauthorizedError extends HttpError
114
+ {
115
+ /**
116
+ * @constructor
117
+ * @param {string} [message] - Human-readable message.
118
+ * @param {object} [opts] - Additional error options.
119
+ * @param {string} [opts.code] - Machine-readable error code.
120
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
121
+ */
122
+ constructor(message, opts) { super(401, message, opts); }
123
+ }
124
+
125
+ class ForbiddenError extends HttpError
126
+ {
127
+ /**
128
+ * @constructor
129
+ * @param {string} [message] - Human-readable message.
130
+ * @param {object} [opts] - Additional error options.
131
+ * @param {string} [opts.code] - Machine-readable error code.
132
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
133
+ */
134
+ constructor(message, opts) { super(403, message, opts); }
135
+ }
136
+
137
+ class NotFoundError extends HttpError
138
+ {
139
+ /**
140
+ * @constructor
141
+ * @param {string} [message] - Human-readable message.
142
+ * @param {object} [opts] - Additional error options.
143
+ * @param {string} [opts.code] - Machine-readable error code.
144
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
145
+ */
146
+ constructor(message, opts) { super(404, message, opts); }
147
+ }
148
+
149
+ class MethodNotAllowedError extends HttpError
150
+ {
151
+ /**
152
+ * @constructor
153
+ * @param {string} [message] - Human-readable message.
154
+ * @param {object} [opts] - Additional error options.
155
+ * @param {string} [opts.code] - Machine-readable error code.
156
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
157
+ */
158
+ constructor(message, opts) { super(405, message, opts); }
159
+ }
160
+
161
+ class ConflictError extends HttpError
162
+ {
163
+ /**
164
+ * @constructor
165
+ * @param {string} [message] - Human-readable message.
166
+ * @param {object} [opts] - Additional error options.
167
+ * @param {string} [opts.code] - Machine-readable error code.
168
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
169
+ */
170
+ constructor(message, opts) { super(409, message, opts); }
171
+ }
172
+
173
+ class GoneError extends HttpError
174
+ {
175
+ /**
176
+ * @constructor
177
+ * @param {string} [message] - Human-readable message.
178
+ * @param {object} [opts] - Additional error options.
179
+ * @param {string} [opts.code] - Machine-readable error code.
180
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
181
+ */
182
+ constructor(message, opts) { super(410, message, opts); }
183
+ }
184
+
185
+ class PayloadTooLargeError extends HttpError
186
+ {
187
+ /**
188
+ * @constructor
189
+ * @param {string} [message] - Human-readable message.
190
+ * @param {object} [opts] - Additional error options.
191
+ * @param {string} [opts.code] - Machine-readable error code.
192
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
193
+ */
194
+ constructor(message, opts) { super(413, message, opts); }
195
+ }
196
+
197
+ class UnprocessableEntityError extends HttpError
198
+ {
199
+ /**
200
+ * @constructor
201
+ * @param {string} [message] - Human-readable message.
202
+ * @param {object} [opts] - Additional error options.
203
+ * @param {string} [opts.code] - Machine-readable error code.
204
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
205
+ */
206
+ constructor(message, opts) { super(422, message, opts); }
207
+ }
208
+
209
+ /**
210
+ * Validation error with field-level details.
211
+ */
212
+ class ValidationError extends HttpError
213
+ {
214
+ /**
215
+ * @constructor
216
+ * @param {string} [message] - Summary message.
217
+ * @param {object|Array} [errors] - Field errors, e.g. { email: 'required', age: 'must be >= 18' }.
218
+ * @param {object} [opts] - Additional error options.
219
+ * @param {string} [opts.code] - Machine-readable error code.
220
+ */
221
+ constructor(message, errors, opts = {})
222
+ {
223
+ super(422, message || 'Validation Failed', { code: 'VALIDATION_FAILED', ...opts, details: errors });
224
+ this.errors = errors || {};
225
+ }
226
+ }
227
+
228
+ class TooManyRequestsError extends HttpError
229
+ {
230
+ /**
231
+ * @constructor
232
+ * @param {string} [message] - Human-readable message.
233
+ * @param {object} [opts] - Additional error options.
234
+ * @param {string} [opts.code] - Machine-readable error code.
235
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
236
+ */
237
+ constructor(message, opts) { super(429, message, opts); }
238
+ }
239
+
240
+ class InternalError extends HttpError
241
+ {
242
+ /**
243
+ * @constructor
244
+ * @param {string} [message] - Human-readable message.
245
+ * @param {object} [opts] - Additional error options.
246
+ * @param {string} [opts.code] - Machine-readable error code.
247
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
248
+ */
249
+ constructor(message, opts) { super(500, message, opts); }
250
+ }
251
+
252
+ class NotImplementedError extends HttpError
253
+ {
254
+ /**
255
+ * @constructor
256
+ * @param {string} [message] - Human-readable message.
257
+ * @param {object} [opts] - Additional error options.
258
+ * @param {string} [opts.code] - Machine-readable error code.
259
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
260
+ */
261
+ constructor(message, opts) { super(501, message, opts); }
262
+ }
263
+
264
+ class BadGatewayError extends HttpError
265
+ {
266
+ /**
267
+ * @constructor
268
+ * @param {string} [message] - Human-readable message.
269
+ * @param {object} [opts] - Additional error options.
270
+ * @param {string} [opts.code] - Machine-readable error code.
271
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
272
+ */
273
+ constructor(message, opts) { super(502, message, opts); }
274
+ }
275
+
276
+ class ServiceUnavailableError extends HttpError
277
+ {
278
+ /**
279
+ * @constructor
280
+ * @param {string} [message] - Human-readable message.
281
+ * @param {object} [opts] - Additional error options.
282
+ * @param {string} [opts.code] - Machine-readable error code.
283
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
284
+ */
285
+ constructor(message, opts) { super(503, message, opts); }
286
+ }
287
+
288
+ // --- Framework Error Classes -------------------------------------
289
+
290
+ /**
291
+ * Database / ORM error — wraps adapter-level failures.
292
+ */
293
+ class DatabaseError extends HttpError
294
+ {
295
+ /**
296
+ * @constructor
297
+ * @param {string} [message] - Description of the DB failure.
298
+ * @param {object} [opts] - Additional error options.
299
+ * @param {string} [opts.query] - The query or operation that failed.
300
+ * @param {string} [opts.adapter] - Adapter name (sqlite, mysql, etc.).
301
+ */
302
+ constructor(message, opts = {})
303
+ {
304
+ super(500, message || 'Database Error', { code: 'DATABASE_ERROR', ...opts });
305
+ if (opts.query) this.query = opts.query;
306
+ if (opts.adapter) this.adapter = opts.adapter;
307
+ }
308
+ }
309
+
310
+ /**
311
+ * Configuration error — thrown when app/adapter configuration is invalid.
312
+ */
313
+ class ConfigurationError extends HttpError
314
+ {
315
+ /**
316
+ * @constructor
317
+ * @param {string} [message] - What misconfiguration was detected.
318
+ * @param {object} [opts] - Additional error options.
319
+ * @param {string} [opts.setting] - The setting key that's invalid.
320
+ */
321
+ constructor(message, opts = {})
322
+ {
323
+ super(500, message || 'Configuration Error', { code: 'CONFIGURATION_ERROR', ...opts });
324
+ if (opts.setting) this.setting = opts.setting;
325
+ }
326
+ }
327
+
328
+ /**
329
+ * Middleware error — a middleware function failed unexpectedly.
330
+ */
331
+ class MiddlewareError extends HttpError
332
+ {
333
+ /**
334
+ * @constructor
335
+ * @param {string} [message] - Description.
336
+ * @param {object} [opts] - Additional error options.
337
+ * @param {string} [opts.middleware] - Middleware name.
338
+ */
339
+ constructor(message, opts = {})
340
+ {
341
+ super(500, message || 'Middleware Error', { code: 'MIDDLEWARE_ERROR', ...opts });
342
+ if (opts.middleware) this.middleware = opts.middleware;
343
+ }
344
+ }
345
+
346
+ /**
347
+ * Routing error — thrown when route resolution fails.
348
+ */
349
+ class RoutingError extends HttpError
350
+ {
351
+ /**
352
+ * @constructor
353
+ * @param {string} [message] - Description.
354
+ * @param {object} [opts] - Additional error options.
355
+ * @param {string} [opts.path] - The route path.
356
+ * @param {string} [opts.method] - The HTTP method.
357
+ */
358
+ constructor(message, opts = {})
359
+ {
360
+ super(500, message || 'Routing Error', { code: 'ROUTING_ERROR', ...opts });
361
+ if (opts.path) this.path = opts.path;
362
+ if (opts.method) this.method = opts.method;
363
+ }
364
+ }
365
+
366
+ /**
367
+ * Timeout error — operation exceeded allowed time.
368
+ */
369
+ class TimeoutError extends HttpError
370
+ {
371
+ /**
372
+ * @constructor
373
+ * @param {string} [message] - Description.
374
+ * @param {object} [opts] - Additional error options.
375
+ * @param {number} [opts.timeout] - The timeout limit in ms.
376
+ */
377
+ constructor(message, opts = {})
378
+ {
379
+ super(408, message || 'Request Timeout', { code: 'TIMEOUT', ...opts });
380
+ if (opts.timeout) this.timeout = opts.timeout;
381
+ }
382
+ }
383
+
384
+ // --- ORM-Specific Error Classes ----------------------------------
385
+
386
+ /**
387
+ * Connection error — database connection failures with retry context.
388
+ */
389
+ class ConnectionError extends DatabaseError
390
+ {
391
+ /**
392
+ * @constructor
393
+ * @param {string} [message] - Description.
394
+ * @param {object} [opts] - Additional error options.
395
+ * @param {string} [opts.adapter] - Adapter name.
396
+ * @param {number} [opts.attempt] - Retry attempt number.
397
+ * @param {number} [opts.maxRetries] - Maximum retries configured.
398
+ * @param {string} [opts.host] - Target host.
399
+ * @param {number} [opts.port] - Target port.
400
+ */
401
+ constructor(message, opts = {})
402
+ {
403
+ super(message || 'Connection Failed', { code: 'CONNECTION_ERROR', ...opts });
404
+ if (opts.attempt !== undefined) this.attempt = opts.attempt;
405
+ if (opts.maxRetries !== undefined) this.maxRetries = opts.maxRetries;
406
+ if (opts.host) this.host = opts.host;
407
+ if (opts.port) this.port = opts.port;
408
+ }
409
+ }
410
+
411
+ /**
412
+ * Migration error — migration execution failures.
413
+ */
414
+ class MigrationError extends DatabaseError
415
+ {
416
+ /**
417
+ * @constructor
418
+ * @param {string} [message] - Description.
419
+ * @param {object} [opts] - Additional error options.
420
+ * @param {string} [opts.migration] - Migration name that failed.
421
+ * @param {string} [opts.direction] - 'up' or 'down'.
422
+ * @param {number} [opts.batch] - Batch number.
423
+ */
424
+ constructor(message, opts = {})
425
+ {
426
+ super(message || 'Migration Failed', { code: 'MIGRATION_ERROR', ...opts });
427
+ if (opts.migration) this.migration = opts.migration;
428
+ if (opts.direction) this.direction = opts.direction;
429
+ if (opts.batch !== undefined) this.batch = opts.batch;
430
+ }
431
+ }
432
+
433
+ /**
434
+ * Transaction error — transaction commit/rollback failures.
435
+ */
436
+ class TransactionError extends DatabaseError
437
+ {
438
+ /**
439
+ * @constructor
440
+ * @param {string} [message] - Description.
441
+ * @param {object} [opts] - Additional error options.
442
+ * @param {string} [opts.phase] - 'begin', 'commit', or 'rollback'.
443
+ */
444
+ constructor(message, opts = {})
445
+ {
446
+ super(message || 'Transaction Failed', { code: 'TRANSACTION_ERROR', ...opts });
447
+ if (opts.phase) this.phase = opts.phase;
448
+ }
449
+ }
450
+
451
+ /**
452
+ * Query error — query execution failures with SQL context.
453
+ */
454
+ class QueryError extends DatabaseError
455
+ {
456
+ /**
457
+ * @constructor
458
+ * @param {string} [message] - Description.
459
+ * @param {object} [opts] - Additional error options.
460
+ * @param {string} [opts.sql] - The SQL that failed (if applicable).
461
+ * @param {Array} [opts.params] - Query parameters.
462
+ * @param {string} [opts.table] - Target table.
463
+ */
464
+ constructor(message, opts = {})
465
+ {
466
+ super(message || 'Query Failed', { code: 'QUERY_ERROR', ...opts });
467
+ if (opts.sql) this.sql = opts.sql;
468
+ if (opts.params) this.params = opts.params;
469
+ if (opts.table) this.table = opts.table;
470
+ }
471
+ }
472
+
473
+ /**
474
+ * Adapter error — adapter-level issues (driver not found, unsupported operation).
475
+ */
476
+ class AdapterError extends DatabaseError
477
+ {
478
+ /**
479
+ * @constructor
480
+ * @param {string} [message] - Description.
481
+ * @param {object} [opts] - Additional error options.
482
+ * @param {string} [opts.adapter] - Adapter name.
483
+ * @param {string} [opts.operation] - The operation that failed.
484
+ */
485
+ constructor(message, opts = {})
486
+ {
487
+ super(message || 'Adapter Error', { code: 'ADAPTER_ERROR', ...opts });
488
+ if (opts.operation) this.operation = opts.operation;
489
+ }
490
+ }
491
+
492
+ /**
493
+ * Cache error — caching layer failures.
494
+ */
495
+ class CacheError extends HttpError
496
+ {
497
+ /**
498
+ * @constructor
499
+ * @param {string} [message] - Description.
500
+ * @param {object} [opts] - Additional error options.
501
+ * @param {string} [opts.operation] - Cache operation that failed.
502
+ * @param {string} [opts.key] - Cache key involved.
503
+ */
504
+ constructor(message, opts = {})
505
+ {
506
+ super(500, message || 'Cache Error', { code: 'CACHE_ERROR', ...opts });
507
+ if (opts.operation) this.operation = opts.operation;
508
+ if (opts.key) this.key = opts.key;
509
+ }
510
+ }
511
+
512
+ // --- Phase 4 Error Classes ---------------------------------------
513
+
514
+ /**
515
+ * Tenancy error — multi-tenancy operation failures.
516
+ */
517
+ class TenancyError extends DatabaseError
518
+ {
519
+ /**
520
+ * @constructor
521
+ * @param {string} [message] - Description.
522
+ * @param {object} [opts] - Additional error options.
523
+ * @param {string} [opts.tenant] - Tenant identifier.
524
+ * @param {string} [opts.strategy] - Tenancy strategy (row, schema).
525
+ */
526
+ constructor(message, opts = {})
527
+ {
528
+ super(message || 'Tenancy Error', { code: 'TENANCY_ERROR', ...opts });
529
+ if (opts.tenant) this.tenant = opts.tenant;
530
+ if (opts.strategy) this.strategy = opts.strategy;
531
+ }
532
+ }
533
+
534
+ /**
535
+ * Audit error — audit logging failures.
536
+ */
537
+ class AuditError extends DatabaseError
538
+ {
539
+ /**
540
+ * @constructor
541
+ * @param {string} [message] - Description.
542
+ * @param {object} [opts] - Additional error options.
543
+ * @param {string} [opts.action] - Audit action that failed.
544
+ * @param {string} [opts.table] - Target table.
545
+ */
546
+ constructor(message, opts = {})
547
+ {
548
+ super(message || 'Audit Error', { code: 'AUDIT_ERROR', ...opts });
549
+ if (opts.action) this.action = opts.action;
550
+ if (opts.table) this.table = opts.table;
551
+ }
552
+ }
553
+
554
+ /**
555
+ * Plugin error — plugin registration or lifecycle failures.
556
+ */
557
+ class PluginError extends HttpError
558
+ {
559
+ /**
560
+ * @constructor
561
+ * @param {string} [message] - Description.
562
+ * @param {object} [opts] - Additional error options.
563
+ * @param {string} [opts.plugin] - Plugin name.
564
+ * @param {string} [opts.phase] - Lifecycle phase (install, boot, uninstall).
565
+ */
566
+ constructor(message, opts = {})
567
+ {
568
+ super(500, message || 'Plugin Error', { code: 'PLUGIN_ERROR', ...opts });
569
+ if (opts.plugin) this.plugin = opts.plugin;
570
+ if (opts.phase) this.phase = opts.phase;
571
+ }
572
+ }
573
+
574
+ /**
575
+ * Procedure error — stored procedure/function failures.
576
+ */
577
+ class ProcedureError extends DatabaseError
578
+ {
579
+ /**
580
+ * @constructor
581
+ * @param {string} [message] - Description.
582
+ * @param {object} [opts] - Additional error options.
583
+ * @param {string} [opts.procedure] - Procedure or function name.
584
+ * @param {string} [opts.operation] - Operation (create, drop, execute).
585
+ */
586
+ constructor(message, opts = {})
587
+ {
588
+ super(message || 'Procedure Error', { code: 'PROCEDURE_ERROR', ...opts });
589
+ if (opts.procedure) this.procedure = opts.procedure;
590
+ if (opts.operation) this.operation = opts.operation;
591
+ }
592
+ }
593
+
594
+ // --- Factory -----------------------------------------------------
595
+
596
+ /**
597
+ * Create an HttpError by status code.
598
+ *
599
+ * @param {number} statusCode - HTTP status code.
600
+ * @param {string} [message] - Human-readable error message.
601
+ * @param {object} [opts] - Additional error options.
602
+ * @param {string} [opts.code] - Machine-readable error code.
603
+ * @param {*} [opts.details] - Extra data (field errors, debug info, etc.).
604
+ * @returns {HttpError} Matching HttpError subclass instance.
605
+ *
606
+ * @example
607
+ * throw createError(404, 'User not found');
608
+ * throw createError(422, 'Invalid input', { details: { email: 'required' } });
609
+ */
610
+ function createError(statusCode, message, opts)
611
+ {
612
+ const map = {
613
+ 400: BadRequestError,
614
+ 401: UnauthorizedError,
615
+ 403: ForbiddenError,
616
+ 404: NotFoundError,
617
+ 405: MethodNotAllowedError,
618
+ 408: TimeoutError,
619
+ 409: ConflictError,
620
+ 410: GoneError,
621
+ 413: PayloadTooLargeError,
622
+ 422: UnprocessableEntityError,
623
+ 429: TooManyRequestsError,
624
+ 500: InternalError,
625
+ 501: NotImplementedError,
626
+ 502: BadGatewayError,
627
+ 503: ServiceUnavailableError,
628
+ };
629
+
630
+ const Cls = map[statusCode];
631
+ if (Cls) return new Cls(message, opts);
632
+ return new HttpError(statusCode, message, opts);
633
+ }
634
+
635
+ /**
636
+ * Check if a value is an HttpError (or duck-typed equivalent).
637
+ * @param {*} err - Error object.
638
+ * @returns {boolean} True if the value is an HttpError.
639
+ */
640
+ function isHttpError(err)
641
+ {
642
+ if (!err || !(err instanceof Error)) return false;
643
+ return err instanceof HttpError || typeof err.statusCode === 'number';
644
+ }
645
+
646
+ module.exports = {
647
+ HttpError,
648
+ BadRequestError,
649
+ UnauthorizedError,
650
+ ForbiddenError,
651
+ NotFoundError,
652
+ MethodNotAllowedError,
653
+ ConflictError,
654
+ GoneError,
655
+ PayloadTooLargeError,
656
+ UnprocessableEntityError,
657
+ ValidationError,
658
+ TooManyRequestsError,
659
+ InternalError,
660
+ NotImplementedError,
661
+ BadGatewayError,
662
+ ServiceUnavailableError,
663
+ // Framework-specific errors
664
+ DatabaseError,
665
+ ConfigurationError,
666
+ MiddlewareError,
667
+ RoutingError,
668
+ TimeoutError,
669
+ // ORM-specific errors
670
+ ConnectionError,
671
+ MigrationError,
672
+ TransactionError,
673
+ QueryError,
674
+ AdapterError,
675
+ CacheError,
676
+ // Phase 4 errors
677
+ TenancyError,
678
+ AuditError,
679
+ PluginError,
680
+ ProcedureError,
681
+ createError,
682
+ isHttpError,
683
+ };