@terreno/api 0.27.0 → 0.29.0

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 (50) hide show
  1. package/dist/actions.test.js +2 -1
  2. package/dist/api.coverage.test.js +4 -2
  3. package/dist/api.errorPaths.test.js +5 -3
  4. package/dist/api.errors.test.js +6 -7
  5. package/dist/api.hooks.test.js +65 -8
  6. package/dist/api.js +93 -47
  7. package/dist/api.test.js +10 -4
  8. package/dist/auth.test.js +1 -1
  9. package/dist/config.js +7 -4
  10. package/dist/config.test.js +28 -3
  11. package/dist/docLoader.js +15 -51
  12. package/dist/docLoader.test.js +2 -1
  13. package/dist/errors.d.ts +72 -5
  14. package/dist/errors.js +265 -67
  15. package/dist/errors.test.js +163 -13
  16. package/dist/httpClient.d.ts +2 -3
  17. package/dist/httpClient.js +4 -4
  18. package/dist/httpClient.test.js +3 -2
  19. package/dist/notifiers/googleChatNotifier.test.js +2 -1
  20. package/dist/notifiers/slackNotifier.test.js +1 -1
  21. package/dist/notifiers/zoomNotifier.test.js +2 -1
  22. package/dist/permissions.js +0 -2
  23. package/dist/permissions.middleware.test.js +7 -42
  24. package/dist/plugins.test.js +4 -2
  25. package/dist/transformers.js +4 -2
  26. package/package.json +2 -2
  27. package/src/actions.test.ts +2 -1
  28. package/src/api.coverage.test.ts +4 -2
  29. package/src/api.errorPaths.test.ts +5 -3
  30. package/src/api.errors.test.ts +6 -7
  31. package/src/api.hooks.test.ts +24 -1
  32. package/src/api.test.ts +10 -4
  33. package/src/api.ts +93 -48
  34. package/src/auth.test.ts +1 -1
  35. package/src/config.test.ts +25 -3
  36. package/src/config.ts +7 -4
  37. package/src/docLoader.test.ts +2 -1
  38. package/src/docLoader.ts +16 -18
  39. package/src/errors.test.ts +178 -13
  40. package/src/errors.ts +278 -79
  41. package/src/httpClient.test.ts +3 -2
  42. package/src/httpClient.ts +4 -4
  43. package/src/notifiers/googleChatNotifier.test.ts +2 -2
  44. package/src/notifiers/slackNotifier.test.ts +2 -2
  45. package/src/notifiers/zoomNotifier.test.ts +2 -2
  46. package/src/permissions.middleware.test.ts +7 -11
  47. package/src/permissions.ts +0 -2
  48. package/src/plugins.test.ts +4 -2
  49. package/src/transformers.ts +4 -2
  50. package/tsconfig.json +1 -1
@@ -1,4 +1,19 @@
1
1
  "use strict";
2
+ var __extends = (this && this.__extends) || (function () {
3
+ var extendStatics = function (d, b) {
4
+ extendStatics = Object.setPrototypeOf ||
5
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
+ return extendStatics(d, b);
8
+ };
9
+ return function (d, b) {
10
+ if (typeof b !== "function" && b !== null)
11
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
+ extendStatics(d, b);
13
+ function __() { this.constructor = d; }
14
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
+ };
16
+ })();
2
17
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
18
  if (k2 === undefined) k2 = k;
4
19
  var desc = Object.getOwnPropertyDescriptor(m, k);
@@ -58,7 +73,6 @@ var buildResponse = function () {
58
73
  title: "Validation failed",
59
74
  });
60
75
  (0, bun_test_1.expect)(error).toBeInstanceOf(Error);
61
- (0, bun_test_1.expect)(error.name).toBe("APIError");
62
76
  (0, bun_test_1.expect)(error.title).toBe("Validation failed");
63
77
  (0, bun_test_1.expect)(error.detail).toBe("Email is invalid");
64
78
  (0, bun_test_1.expect)(error.code).toBe("validation-failed");
@@ -75,16 +89,42 @@ var buildResponse = function () {
75
89
  });
76
90
  (0, bun_test_1.expect)(error.meta).toEqual({ requestId: "req-1" });
77
91
  });
78
- (0, bun_test_1.it)("includes the title and detail in the error message", function () {
92
+ (0, bun_test_1.it)("uses the title as the message, without detail or stacks", function () {
79
93
  var error = new errors_1.APIError({ detail: "Something exploded", title: "Boom" });
80
- (0, bun_test_1.expect)(error.message).toBe("Boom: Something exploded");
94
+ (0, bun_test_1.expect)(error.message).toBe("Boom");
95
+ (0, bun_test_1.expect)(error.title).toBe("Boom");
81
96
  });
82
- (0, bun_test_1.it)("includes the wrapped error stack in the message", function () {
83
- var _a;
97
+ (0, bun_test_1.it)("exposes the wrapped error as the standard cause", function () {
98
+ var wrapped = new Error("inner");
99
+ var error = new errors_1.APIError({ cause: wrapped, title: "Outer" });
100
+ (0, bun_test_1.expect)(error.message).toBe("Outer");
101
+ (0, bun_test_1.expect)(error.cause).toBe(wrapped);
102
+ });
103
+ (0, bun_test_1.it)("supports the deprecated error option as a cause alias", function () {
84
104
  var wrapped = new Error("inner");
85
105
  var error = new errors_1.APIError({ error: wrapped, title: "Outer" });
86
- (0, bun_test_1.expect)(error.message).toContain("Outer");
87
- (0, bun_test_1.expect)(error.message).toContain((_a = wrapped.stack) !== null && _a !== void 0 ? _a : "");
106
+ (0, bun_test_1.expect)(error.cause).toBe(wrapped);
107
+ (0, bun_test_1.expect)(error.error).toBe(wrapped);
108
+ });
109
+ (0, bun_test_1.it)("derives the name from the status code", function () {
110
+ (0, bun_test_1.expect)(new errors_1.APIError({ status: 400, title: "T" }).name).toBe("BadRequestError");
111
+ (0, bun_test_1.expect)(new errors_1.APIError({ status: 401, title: "T" }).name).toBe("UnauthorizedError");
112
+ (0, bun_test_1.expect)(new errors_1.APIError({ status: 403, title: "T" }).name).toBe("ForbiddenError");
113
+ (0, bun_test_1.expect)(new errors_1.APIError({ status: 404, title: "T" }).name).toBe("NotFoundError");
114
+ (0, bun_test_1.expect)(new errors_1.APIError({ status: 409, title: "T" }).name).toBe("ConflictError");
115
+ (0, bun_test_1.expect)(new errors_1.APIError({ status: 500, title: "T" }).name).toBe("InternalServerError");
116
+ });
117
+ (0, bun_test_1.it)("falls back to APIError for unmapped statuses", function () {
118
+ (0, bun_test_1.expect)(new errors_1.APIError({ status: 418, title: "T" }).name).toBe("APIError");
119
+ });
120
+ (0, bun_test_1.it)("derives the name from the code when provided", function () {
121
+ var error = new errors_1.APIError({ code: "update-admin-error", status: 403, title: "T" });
122
+ (0, bun_test_1.expect)(error.name).toBe("UpdateAdminError");
123
+ });
124
+ (0, bun_test_1.it)("keeps title in sync with message via the getter", function () {
125
+ var error = new errors_1.APIError({ title: "Original" });
126
+ error.message = "Changed";
127
+ (0, bun_test_1.expect)(error.title).toBe("Changed");
88
128
  });
89
129
  (0, bun_test_1.it)("defaults status to 500 when status is omitted", function () {
90
130
  var error = new errors_1.APIError({ title: "No status" });
@@ -120,6 +160,54 @@ var buildResponse = function () {
120
160
  (0, bun_test_1.expect)(untrackedError.disableExternalErrorTracking).toBe(true);
121
161
  });
122
162
  });
163
+ (0, bun_test_1.describe)("APIError subclasses", function () {
164
+ (0, bun_test_1.it)("sets the status and name from the subclass", function () {
165
+ var error = new errors_1.NotFoundError("Todo not found");
166
+ (0, bun_test_1.expect)(error.status).toBe(404);
167
+ (0, bun_test_1.expect)(error.name).toBe("NotFoundError");
168
+ (0, bun_test_1.expect)(error.message).toBe("Todo not found");
169
+ (0, bun_test_1.expect)(error).toBeInstanceOf(errors_1.APIError);
170
+ (0, bun_test_1.expect)((0, errors_1.isAPIError)(error)).toBe(true);
171
+ });
172
+ (0, bun_test_1.it)("accepts full options", function () {
173
+ var cause = new Error("inner");
174
+ var error = new errors_1.ForbiddenError({ cause: cause, detail: "Admins only", title: "Not allowed" });
175
+ (0, bun_test_1.expect)(error.status).toBe(403);
176
+ (0, bun_test_1.expect)(error.name).toBe("ForbiddenError");
177
+ (0, bun_test_1.expect)(error.detail).toBe("Admins only");
178
+ (0, bun_test_1.expect)(error.cause).toBe(cause);
179
+ });
180
+ (0, bun_test_1.it)("keeps the subclass name even when a code is provided", function () {
181
+ var error = new errors_1.BadRequestError({ code: "invalid-cursor", title: "Bad cursor" });
182
+ (0, bun_test_1.expect)(error.name).toBe("BadRequestError");
183
+ (0, bun_test_1.expect)(error.status).toBe(400);
184
+ });
185
+ (0, bun_test_1.it)("uses ValidationError with a 400 status", function () {
186
+ var _a;
187
+ var error = new errors_1.ValidationError({ fields: { email: "Required" }, title: "Validation failed" });
188
+ (0, bun_test_1.expect)(error.status).toBe(400);
189
+ (0, bun_test_1.expect)(error.name).toBe("ValidationError");
190
+ (0, bun_test_1.expect)((_a = error.meta) === null || _a === void 0 ? void 0 : _a.fields).toEqual({ email: "Required" });
191
+ });
192
+ (0, bun_test_1.it)("derives name from custom subclasses via new.target", function () {
193
+ var PaymentError = /** @class */ (function (_super) {
194
+ __extends(PaymentError, _super);
195
+ function PaymentError(title) {
196
+ return _super.call(this, { status: 402, title: title }) || this;
197
+ }
198
+ return PaymentError;
199
+ }(errors_1.APIError));
200
+ (0, bun_test_1.expect)(new PaymentError("declined").name).toBe("PaymentError");
201
+ });
202
+ (0, bun_test_1.it)("covers the remaining status subclasses", function () {
203
+ (0, bun_test_1.expect)(new errors_1.UnauthorizedError("U").status).toBe(401);
204
+ (0, bun_test_1.expect)(new errors_1.UnauthorizedError("U").name).toBe("UnauthorizedError");
205
+ (0, bun_test_1.expect)(new errors_1.ConflictError("C").status).toBe(409);
206
+ (0, bun_test_1.expect)(new errors_1.ConflictError("C").name).toBe("ConflictError");
207
+ (0, bun_test_1.expect)(new errors_1.InternalServerError("I").status).toBe(500);
208
+ (0, bun_test_1.expect)(new errors_1.InternalServerError("I").name).toBe("InternalServerError");
209
+ });
210
+ });
123
211
  (0, bun_test_1.describe)("isAPIError", function () {
124
212
  (0, bun_test_1.it)("returns true for an APIError instance", function () {
125
213
  (0, bun_test_1.expect)((0, errors_1.isAPIError)(new errors_1.APIError({ title: "Boom" }))).toBe(true);
@@ -127,11 +215,19 @@ var buildResponse = function () {
127
215
  (0, bun_test_1.it)("returns false for a regular Error", function () {
128
216
  (0, bun_test_1.expect)((0, errors_1.isAPIError)(new Error("nope"))).toBe(false);
129
217
  });
130
- (0, bun_test_1.it)("returns true for any error whose name is APIError", function () {
218
+ (0, bun_test_1.it)("returns true for any error whose name is APIError (transition fallback)", function () {
131
219
  var err = new Error("custom");
132
220
  err.name = "APIError";
133
221
  (0, bun_test_1.expect)((0, errors_1.isAPIError)(err)).toBe(true);
134
222
  });
223
+ (0, bun_test_1.it)("returns true for a branded error from a duplicate package copy", function () {
224
+ var err = new Error("branded");
225
+ err.isTerrenoAPIError = true;
226
+ (0, bun_test_1.expect)((0, errors_1.isAPIError)(err)).toBe(true);
227
+ });
228
+ (0, bun_test_1.it)("returns true for subclasses", function () {
229
+ (0, bun_test_1.expect)((0, errors_1.isAPIError)(new errors_1.NotFoundError("missing"))).toBe(true);
230
+ });
135
231
  });
136
232
  (0, bun_test_1.describe)("getDisableExternalErrorTracking", function () {
137
233
  (0, bun_test_1.it)("returns the flag from an APIError", function () {
@@ -184,9 +280,9 @@ var buildResponse = function () {
184
280
  (0, bun_test_1.it)("returns title and status by default", function () {
185
281
  var error = new errors_1.APIError({ status: 404, title: "Not Found" });
186
282
  var body = (0, errors_1.getAPIErrorBody)(error);
187
- (0, bun_test_1.expect)(body).toEqual({ meta: {}, status: 404, title: "Not Found" });
283
+ (0, bun_test_1.expect)(body).toEqual({ status: 404, title: "Not Found" });
188
284
  });
189
- (0, bun_test_1.it)("includes optional fields when set", function () {
285
+ (0, bun_test_1.it)("includes optional fields when set, including disableExternalErrorTracking when true", function () {
190
286
  var error = new errors_1.APIError({
191
287
  code: "not-found",
192
288
  detail: "Could not find resource",
@@ -204,17 +300,48 @@ var buildResponse = function () {
204
300
  disableExternalErrorTracking: true,
205
301
  id: "err-1",
206
302
  links: { about: "https://example.com/help" },
207
- meta: {},
208
303
  source: { pointer: "/data/id" },
209
304
  status: 404,
210
305
  title: "Not Found",
211
306
  });
212
307
  });
308
+ (0, bun_test_1.it)("matches toJSON and excludes name, stack, and cause", function () {
309
+ var error = new errors_1.APIError({
310
+ cause: new Error("inner"),
311
+ detail: "d",
312
+ status: 404,
313
+ title: "Not Found",
314
+ });
315
+ var body = error.toJSON();
316
+ (0, bun_test_1.expect)(body).toEqual((0, errors_1.getAPIErrorBody)(error));
317
+ (0, bun_test_1.expect)(body.name).toBeUndefined();
318
+ (0, bun_test_1.expect)(body.stack).toBeUndefined();
319
+ (0, bun_test_1.expect)(body.cause).toBeUndefined();
320
+ (0, bun_test_1.expect)(body.disableExternalErrorTracking).toBeUndefined();
321
+ (0, bun_test_1.expect)(JSON.parse(JSON.stringify(error))).toEqual(JSON.parse(JSON.stringify(body)));
322
+ });
323
+ (0, bun_test_1.it)("exposes deprecated error getter as alias for cause", function () {
324
+ var cause = new Error("inner");
325
+ var error = new errors_1.APIError({ cause: cause, title: "Wrapped" });
326
+ (0, bun_test_1.expect)(error.error).toBe(cause);
327
+ (0, bun_test_1.expect)(error.cause).toBe(cause);
328
+ });
329
+ (0, bun_test_1.it)("prefers explicit title over message when serializing legacy instances", function () {
330
+ var legacy = new Error("polluted message with stack");
331
+ legacy.status = 404;
332
+ legacy.title = "Clean title";
333
+ var body = (0, errors_1.getAPIErrorBody)(legacy);
334
+ (0, bun_test_1.expect)(body.title).toBe("Clean title");
335
+ });
336
+ (0, bun_test_1.it)("truncates fractional and invalid status codes to 500", function () {
337
+ (0, bun_test_1.expect)(new errors_1.APIError({ status: 404.7, title: "x" }).status).toBe(404);
338
+ (0, bun_test_1.expect)(new errors_1.APIError({ status: Number.NaN, title: "x" }).status).toBe(500);
339
+ (0, bun_test_1.expect)(new errors_1.APIError({ status: 200, title: "x" }).status).toBe(500);
340
+ });
213
341
  (0, bun_test_1.it)("omits empty meta and unset optional fields", function () {
214
342
  var error = new errors_1.APIError({ status: 400, title: "Bad" });
215
- // meta defaults to {} which is truthy, so it is included.
216
343
  var body = (0, errors_1.getAPIErrorBody)(error);
217
- (0, bun_test_1.expect)(body.meta).toEqual({});
344
+ (0, bun_test_1.expect)(body.meta).toBeUndefined();
218
345
  (0, bun_test_1.expect)(body.code).toBeUndefined();
219
346
  (0, bun_test_1.expect)(body.detail).toBeUndefined();
220
347
  (0, bun_test_1.expect)(body.id).toBeUndefined();
@@ -286,6 +413,29 @@ var buildResponse = function () {
286
413
  (0, errors_1.apiErrorMiddleware)(err, req, res, next);
287
414
  (0, bun_test_1.expect)(captureExceptionSpy).toHaveBeenCalledWith(err);
288
415
  });
416
+ (0, bun_test_1.it)("fingerprints Sentry captures on the logical error type", function () {
417
+ var scope = {
418
+ setContext: (0, bun_test_1.mock)(function () { }),
419
+ setFingerprint: (0, bun_test_1.mock)(function () { }),
420
+ setTag: (0, bun_test_1.mock)(function () { }),
421
+ };
422
+ var withScopeSpy = (0, bun_test_1.spyOn)(Sentry, "withScope").mockImplementation((function (callback) { return callback(scope); }));
423
+ try {
424
+ var err = new errors_1.APIError({ code: "todo-sync-failed", status: 502, title: "Sync failed" });
425
+ (0, errors_1.apiErrorMiddleware)(err, req, res, next);
426
+ (0, bun_test_1.expect)(scope.setFingerprint).toHaveBeenCalledWith([
427
+ "TodoSyncFailed",
428
+ "todo-sync-failed",
429
+ "502",
430
+ ]);
431
+ (0, bun_test_1.expect)(scope.setTag).toHaveBeenCalledWith("http.status_code", "502");
432
+ (0, bun_test_1.expect)(scope.setTag).toHaveBeenCalledWith("api_error.code", "todo-sync-failed");
433
+ (0, bun_test_1.expect)(scope.setContext).toHaveBeenCalledWith("apiError", bun_test_1.expect.objectContaining({ status: 502, title: "Sync failed" }));
434
+ }
435
+ finally {
436
+ withScopeSpy.mockRestore();
437
+ }
438
+ });
289
439
  (0, bun_test_1.it)("does not capture the exception when disableExternalErrorTracking is true", function () {
290
440
  var err = new errors_1.APIError({
291
441
  disableExternalErrorTracking: true,
@@ -59,9 +59,8 @@ export interface WithApiErrorHandlingOptions {
59
59
  *
60
60
  * In "raw" mode (default) the wrapper logs the normalized shape via the injected logger
61
61
  * and rethrows the original error. In "apiError" mode the wrapper throws a terreno
62
- * APIError — whose constructor already logs — so the wrapper itself stays silent to
63
- * preserve the log-once contract. The APIError title is stable per JSONAPI convention;
64
- * per-occurrence text goes in `detail`, built from the (redacted) normalized messages.
62
+ * APIError with a stable title (per-occurrence text goes in `detail`) and stays silent
63
+ * itself — apiErrorMiddleware logs APIErrors, so logging here too would double-log.
65
64
  */
66
65
  export declare const withApiErrorHandling: <T>(fn: () => Promise<T>, options: WithApiErrorHandlingOptions) => Promise<T>;
67
66
  export type AuthStrategy = {
@@ -182,9 +182,8 @@ exports.normalizeApiError = normalizeApiError;
182
182
  *
183
183
  * In "raw" mode (default) the wrapper logs the normalized shape via the injected logger
184
184
  * and rethrows the original error. In "apiError" mode the wrapper throws a terreno
185
- * APIError — whose constructor already logs — so the wrapper itself stays silent to
186
- * preserve the log-once contract. The APIError title is stable per JSONAPI convention;
187
- * per-occurrence text goes in `detail`, built from the (redacted) normalized messages.
185
+ * APIError with a stable title (per-occurrence text goes in `detail`) and stays silent
186
+ * itself — apiErrorMiddleware logs APIErrors, so logging here too would double-log.
188
187
  */
189
188
  var withApiErrorHandling = function (fn, options) { return __awaiter(void 0, void 0, void 0, function () {
190
189
  var log, error_1, normalized, statusCode;
@@ -210,8 +209,9 @@ var withApiErrorHandling = function (fn, options) { return __awaiter(void 0, voi
210
209
  if (options.rethrowAs === "apiError") {
211
210
  statusCode = normalized.statusCode;
212
211
  throw new errors_1.APIError({
212
+ cause: error_1,
213
+ code: "http-client-error",
213
214
  detail: (_b = normalized.messages[0]) !== null && _b !== void 0 ? _b : "unknown error",
214
- error: error_1,
215
215
  meta: { classification: normalized.classification },
216
216
  status: statusCode !== undefined && statusCode >= 400 ? statusCode : 500,
217
217
  title: "".concat(options.apiName, " ").concat(options.operation, " request failed"),
@@ -313,7 +313,7 @@ var buildAxiosError = function (_a) {
313
313
  }
314
314
  });
315
315
  }); });
316
- (0, bun_test_1.it)("converts to an APIError when rethrowAs is apiError, logging via APIError only", function () { return __awaiter(void 0, void 0, void 0, function () {
316
+ (0, bun_test_1.it)("converts to an APIError when rethrowAs is apiError without logging itself", function () { return __awaiter(void 0, void 0, void 0, function () {
317
317
  var _a, logger, entries, original, caught, error_2, apiError;
318
318
  var _b;
319
319
  return __generator(this, function (_c) {
@@ -344,7 +344,8 @@ var buildAxiosError = function (_a) {
344
344
  (0, bun_test_1.expect)(apiError.detail).toBe("Rate limit exceeded");
345
345
  (0, bun_test_1.expect)(apiError.error).toBe(original);
346
346
  (0, bun_test_1.expect)((_b = apiError.meta) === null || _b === void 0 ? void 0 : _b.classification).toBe("rateLimited");
347
- // Logged exactly once: the APIError constructor logs, so the wrapper must not.
347
+ // The wrapper stays silent — apiErrorMiddleware logs the APIError, so logging here
348
+ // too would produce two log entries for the same failure.
348
349
  (0, bun_test_1.expect)(entries).toHaveLength(0);
349
350
  return [2 /*return*/];
350
351
  }
@@ -102,6 +102,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
102
102
  var bun_test_1 = require("bun:test");
103
103
  var Sentry = __importStar(require("@sentry/bun"));
104
104
  var axios_1 = __importDefault(require("axios"));
105
+ var errors_1 = require("../errors");
105
106
  var googleChatNotifier_1 = require("./googleChatNotifier");
106
107
  (0, bun_test_1.describe)("sendToGoogleChat", function () {
107
108
  var mockAxiosPost;
@@ -232,7 +233,7 @@ var googleChatNotifier_1 = require("./googleChatNotifier");
232
233
  throw new Error("Expected sendToGoogleChat to throw APIError");
233
234
  case 3:
234
235
  error_1 = _a.sent();
235
- (0, bun_test_1.expect)(error_1.name).toBe("APIError");
236
+ (0, bun_test_1.expect)((0, errors_1.isAPIError)(error_1)).toBe(true);
236
237
  (0, bun_test_1.expect)(error_1.title).toMatch(/Error posting to Google Chat/i);
237
238
  return [3 /*break*/, 4];
238
239
  case 4:
@@ -269,7 +269,7 @@ var slackNotifier_1 = require("./slackNotifier");
269
269
  case 3:
270
270
  error_1 = _a.sent();
271
271
  apiError = error_1;
272
- (0, bun_test_1.expect)(apiError.name).toBe("APIError");
272
+ (0, bun_test_1.expect)((0, errors_1.isAPIError)(apiError)).toBe(true);
273
273
  (0, bun_test_1.expect)(apiError.title).toMatch(/Error posting to slack/i);
274
274
  return [3 /*break*/, 4];
275
275
  case 4:
@@ -102,6 +102,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
102
102
  var bun_test_1 = require("bun:test");
103
103
  var Sentry = __importStar(require("@sentry/bun"));
104
104
  var axios_1 = __importDefault(require("axios"));
105
+ var errors_1 = require("../errors");
105
106
  var zoomNotifier_1 = require("./zoomNotifier");
106
107
  (0, bun_test_1.describe)("sendToZoom", function () {
107
108
  var mockAxiosPost;
@@ -341,7 +342,7 @@ var zoomNotifier_1 = require("./zoomNotifier");
341
342
  throw new Error("Expected sendToZoom to throw APIError");
342
343
  case 3:
343
344
  error_1 = _a.sent();
344
- (0, bun_test_1.expect)(error_1.name).toBe("APIError");
345
+ (0, bun_test_1.expect)((0, errors_1.isAPIError)(error_1)).toBe(true);
345
346
  (0, bun_test_1.expect)(error_1.title).toMatch(/Error posting to Zoom/i);
346
347
  return [3 /*break*/, 4];
347
348
  case 4:
@@ -50,7 +50,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
50
50
  exports.permissionMiddleware = exports.checkPermissions = exports.Permissions = exports.OwnerQueryFilter = void 0;
51
51
  var docLoader_1 = require("./docLoader");
52
52
  var errors_1 = require("./errors");
53
- var logger_1 = require("./logger");
54
53
  var OwnerQueryFilter = function (user) {
55
54
  if (user) {
56
55
  return { ownerId: user === null || user === void 0 ? void 0 : user.id };
@@ -219,7 +218,6 @@ var permissionMiddleware = function (model, options) {
219
218
  return [2 /*return*/, next()];
220
219
  case 5:
221
220
  error_1 = _c.sent();
222
- logger_1.logger.error("Permissions error: ".concat(error_1 instanceof Error ? error_1.message : error_1));
223
221
  return [2 /*return*/, next(error_1)];
224
222
  case 6: return [2 /*return*/];
225
223
  }
@@ -10,39 +10,6 @@ var __assign = (this && this.__assign) || function () {
10
10
  };
11
11
  return __assign.apply(this, arguments);
12
12
  };
13
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
- if (k2 === undefined) k2 = k;
15
- var desc = Object.getOwnPropertyDescriptor(m, k);
16
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
- desc = { enumerable: true, get: function() { return m[k]; } };
18
- }
19
- Object.defineProperty(o, k2, desc);
20
- }) : (function(o, m, k, k2) {
21
- if (k2 === undefined) k2 = k;
22
- o[k2] = m[k];
23
- }));
24
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
- Object.defineProperty(o, "default", { enumerable: true, value: v });
26
- }) : function(o, v) {
27
- o["default"] = v;
28
- });
29
- var __importStar = (this && this.__importStar) || (function () {
30
- var ownKeys = function(o) {
31
- ownKeys = Object.getOwnPropertyNames || function (o) {
32
- var ar = [];
33
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
34
- return ar;
35
- };
36
- return ownKeys(o);
37
- };
38
- return function (mod) {
39
- if (mod && mod.__esModule) return mod;
40
- var result = {};
41
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
42
- __setModuleDefault(result, mod);
43
- return result;
44
- };
45
- })();
46
13
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
47
14
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
48
15
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -99,7 +66,6 @@ Object.defineProperty(exports, "__esModule", { value: true });
99
66
  // noExplicitAny: test mock typing
100
67
  // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
101
68
  var bun_test_1 = require("bun:test");
102
- var Sentry = __importStar(require("@sentry/bun"));
103
69
  var errors_1 = require("./errors");
104
70
  var permissions_1 = require("./permissions");
105
71
  (0, bun_test_1.describe)("permissionMiddleware", function () {
@@ -194,17 +160,17 @@ var permissions_1 = require("./permissions");
194
160
  _a = __read(next.mock.calls[0], 1), error = _a[0];
195
161
  (0, bun_test_1.expect)(error).toBeInstanceOf(errors_1.APIError);
196
162
  (0, bun_test_1.expect)(error.status).toBe(500);
197
- (0, bun_test_1.expect)(error.title).toContain("GET failed on 507f1f77bcf86cd799439011");
163
+ (0, bun_test_1.expect)(error.title).toBe("GET error");
164
+ (0, bun_test_1.expect)(error.detail).toContain("GET failed on 507f1f77bcf86cd799439011");
198
165
  return [2 /*return*/];
199
166
  }
200
167
  });
201
168
  }); });
202
- (0, bun_test_1.it)("captures sentry message when document does not exist", function () { return __awaiter(void 0, void 0, void 0, function () {
203
- var captureMessageSpy, model, middleware, next, _a, error;
169
+ (0, bun_test_1.it)("returns plain not found when document does not exist", function () { return __awaiter(void 0, void 0, void 0, function () {
170
+ var model, middleware, next, _a, error;
204
171
  return __generator(this, function (_b) {
205
172
  switch (_b.label) {
206
173
  case 0:
207
- captureMessageSpy = (0, bun_test_1.spyOn)(Sentry, "captureMessage");
208
174
  model = {
209
175
  collection: { findOne: (0, bun_test_1.mock)(function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) {
210
176
  return [2 /*return*/, null];
@@ -219,12 +185,11 @@ var permissions_1 = require("./permissions");
219
185
  return [4 /*yield*/, middleware(buildReq({ method: "GET", params: { id: "507f1f77bcf86cd799439011" } }), {}, next)];
220
186
  case 1:
221
187
  _b.sent();
222
- (0, bun_test_1.expect)(captureMessageSpy).toHaveBeenCalledWith("Document 507f1f77bcf86cd799439011 not found for model MockModel");
223
188
  _a = __read(next.mock.calls[0], 1), error = _a[0];
224
189
  (0, bun_test_1.expect)(error).toBeInstanceOf(errors_1.APIError);
225
190
  (0, bun_test_1.expect)(error.status).toBe(404);
226
- (0, bun_test_1.expect)(error.meta).toBeUndefined();
227
- captureMessageSpy.mockRestore();
191
+ (0, bun_test_1.expect)(error.title).toBe("Document not found");
192
+ (0, bun_test_1.expect)(error.detail).toContain("507f1f77bcf86cd799439011");
228
193
  return [2 /*return*/];
229
194
  }
230
195
  });
@@ -335,7 +300,7 @@ var permissions_1 = require("./permissions");
335
300
  _a = __read(next.mock.calls[0], 1), error = _a[0];
336
301
  (0, bun_test_1.expect)(error).toBeInstanceOf(errors_1.APIError);
337
302
  (0, bun_test_1.expect)(error.status).toBe(404);
338
- (0, bun_test_1.expect)(error.meta).toBeUndefined();
303
+ (0, bun_test_1.expect)(error.title).toBe("Document not found");
339
304
  return [2 /*return*/];
340
305
  }
341
306
  });
@@ -819,7 +819,8 @@ var BareThingModel = (0, mongoose_1.model)("BareThing", bareThingSchema);
819
819
  return [4 /*yield*/, agent.get("/stuff/".concat(doc._id)).expect(404)];
820
820
  case 2:
821
821
  res = _a.sent();
822
- (0, bun_test_1.expect)(res.body.title).toBe("Document ".concat(doc._id, " not found for model Stuff"));
822
+ (0, bun_test_1.expect)(res.body.title).toBe("Document not found");
823
+ (0, bun_test_1.expect)(res.body.detail).toContain(String(doc._id));
823
824
  (0, bun_test_1.expect)(res.body.meta).toEqual({ deleted: "true" });
824
825
  return [2 /*return*/];
825
826
  }
@@ -834,7 +835,8 @@ var BareThingModel = (0, mongoose_1.model)("BareThing", bareThingSchema);
834
835
  return [4 /*yield*/, agent.get("/stuff/".concat(nonExistentId)).expect(404)];
835
836
  case 1:
836
837
  res = _a.sent();
837
- (0, bun_test_1.expect)(res.body.title).toBe("Document ".concat(nonExistentId, " not found for model Stuff"));
838
+ (0, bun_test_1.expect)(res.body.title).toBe("Document not found");
839
+ (0, bun_test_1.expect)(res.body.detail).toContain(nonExistentId);
838
840
  (0, bun_test_1.expect)(res.body.meta).toBeUndefined();
839
841
  return [2 /*return*/];
840
842
  }
@@ -211,9 +211,11 @@ var defaultResponseHandler = function (doc, method, request, options) { return _
211
211
  catch (error) {
212
212
  errorObj = error;
213
213
  throw new errors_1.APIError({
214
- error: errorObj,
214
+ cause: errorObj,
215
+ code: "serialization-error",
216
+ detail: errorObj.message,
215
217
  status: 400,
216
- title: "Error serializing ".concat(method, " response: ").concat(errorObj.message),
218
+ title: "Error serializing ".concat(method, " response"),
217
219
  });
218
220
  }
219
221
  return [2 /*return*/];
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "@asteasolutions/zod-to-openapi": "^8.5.0",
8
8
  "@sentry/bun": "^10.39.0",
9
9
  "@sentry/profiling-node": "^10.25.0",
10
- "@terreno/test": "0.27.0",
10
+ "@terreno/test": "0.29.0",
11
11
  "@types/qs": "^6.14.0",
12
12
  "ajv": "8.20.0",
13
13
  "ajv-formats": "^3.0.1",
@@ -123,5 +123,5 @@
123
123
  "updateSnapshot": "bun test --update-snapshots"
124
124
  },
125
125
  "types": "dist/index.d.ts",
126
- "version": "0.27.0"
126
+ "version": "0.29.0"
127
127
  }
@@ -274,7 +274,8 @@ describe("modelRouter actions", () => {
274
274
  });
275
275
  const missingId = "507f1f77bcf86cd799439011";
276
276
  const res = await server.get(`/food/${missingId}/peek`).expect(404);
277
- expect(res.body.title).toContain(missingId);
277
+ expect(res.body.title).toBe("Document not found");
278
+ expect(res.body.detail).toContain(missingId);
278
279
  expect(res.body.meta).toBeUndefined();
279
280
  });
280
281
 
@@ -70,7 +70,8 @@ describe("modelRouter error path coverage", () => {
70
70
  server = supertest(app);
71
71
 
72
72
  const res = await server.post(`/food/${apple._id}/tags`).send({tags: "organic"}).expect(403);
73
- expect(res.body.title).toContain("array transform boom");
73
+ expect(res.body.title).toBe("Transform error");
74
+ expect(res.body.detail).toContain("array transform boom");
74
75
  });
75
76
 
76
77
  it("returns 400 when saving an array operation fails validation", async () => {
@@ -116,6 +117,7 @@ describe("modelRouter error path coverage", () => {
116
117
  server = supertest(app);
117
118
 
118
119
  const res = await server.delete(`/explosive/${doc._id}`).expect(400);
119
- expect(res.body.title).toContain("deleteOne exploded");
120
+ expect(res.body.title).toBe("Delete error");
121
+ expect(res.body.detail).toContain("deleteOne exploded");
120
122
  });
121
123
  });
@@ -75,7 +75,7 @@ describe("modelRouter error paths", () => {
75
75
  .send({calories: 5, name: "Broccoli", ownerId: new mongoose.Types.ObjectId()})
76
76
  .expect(400);
77
77
  assert.include(res.body.title, "Populate error");
78
- assert.include(res.body.title, "populate exploded");
78
+ assert.include(res.body.detail, "populate exploded");
79
79
  });
80
80
 
81
81
  it("wraps list query failures in a List error", async () => {
@@ -132,7 +132,8 @@ describe("modelRouter error paths", () => {
132
132
 
133
133
  const created = await BrittleModel.create({name: "doomed"});
134
134
  const res = await server.delete(`/brittle/${created._id}`).expect(400);
135
- assert.include(res.body.title, "deleteOne exploded");
135
+ assert.equal(res.body.title, "Delete error");
136
+ assert.include(res.body.detail, "deleteOne exploded");
136
137
  });
137
138
 
138
139
  it("wraps non-APIError transformer failures during array operations", async () => {
@@ -150,7 +151,8 @@ describe("modelRouter error paths", () => {
150
151
  } as Partial<Food>);
151
152
 
152
153
  const res = await server.post(`/food/${food._id}/tags`).send({tags: "vegetable"}).expect(403);
153
- assert.include(res.body.title, "transform exploded");
154
+ assert.equal(res.body.title, "Transform error");
155
+ assert.include(res.body.detail, "transform exploded");
154
156
  });
155
157
 
156
158
  it("wraps save failures during array operations", async () => {
@@ -29,24 +29,23 @@ describe("errors module", () => {
29
29
  expect(error.status).toBe(500);
30
30
  });
31
31
 
32
- it("includes error stack in message when error is provided", () => {
32
+ it("exposes a wrapped error as the cause, keeping the message stable", () => {
33
33
  const originalError = new Error("Original error");
34
34
  const apiError = new APIError({
35
35
  error: originalError,
36
36
  title: "Wrapped error",
37
37
  });
38
- expect(apiError.message).toContain("Wrapped error");
39
- expect(originalError.stack).toBeDefined();
40
- expect(apiError.message).toContain(originalError.stack as string);
38
+ expect(apiError.message).toBe("Wrapped error");
39
+ expect(apiError.cause).toBe(originalError);
41
40
  });
42
41
 
43
- it("includes detail in message when provided", () => {
42
+ it("keeps detail out of the message", () => {
44
43
  const error = new APIError({
45
44
  detail: "More details here",
46
45
  title: "Test error",
47
46
  });
48
- expect(error.message).toContain("Test error");
49
- expect(error.message).toContain("More details here");
47
+ expect(error.message).toBe("Test error");
48
+ expect(error.detail).toBe("More details here");
50
49
  });
51
50
 
52
51
  it("sets fields in meta when provided", () => {