@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,4 @@
1
- import {beforeEach, describe, expect, it, mock} from "bun:test";
1
+ import {beforeEach, describe, expect, it, mock, spyOn} from "bun:test";
2
2
  import * as Sentry from "@sentry/bun";
3
3
  import type {NextFunction, Request, Response} from "express";
4
4
  import mongoose, {Schema} from "mongoose";
@@ -7,12 +7,19 @@ import {
7
7
  APIError,
8
8
  apiErrorMiddleware,
9
9
  apiUnauthorizedMiddleware,
10
+ BadRequestError,
11
+ ConflictError,
10
12
  errorMessage,
11
13
  errorStack,
12
14
  errorsPlugin,
15
+ ForbiddenError,
13
16
  getAPIErrorBody,
14
17
  getDisableExternalErrorTracking,
18
+ InternalServerError,
15
19
  isAPIError,
20
+ NotFoundError,
21
+ UnauthorizedError,
22
+ ValidationError,
16
23
  } from "./errors";
17
24
 
18
25
  interface MockResponse {
@@ -44,7 +51,6 @@ describe("APIError", () => {
44
51
  });
45
52
 
46
53
  expect(error).toBeInstanceOf(Error);
47
- expect(error.name).toBe("APIError");
48
54
  expect(error.title).toBe("Validation failed");
49
55
  expect(error.detail).toBe("Email is invalid");
50
56
  expect(error.code).toBe("validation-failed");
@@ -62,16 +68,48 @@ describe("APIError", () => {
62
68
  expect(error.meta).toEqual({requestId: "req-1"});
63
69
  });
64
70
 
65
- it("includes the title and detail in the error message", () => {
71
+ it("uses the title as the message, without detail or stacks", () => {
66
72
  const error = new APIError({detail: "Something exploded", title: "Boom"});
67
- expect(error.message).toBe("Boom: Something exploded");
73
+ expect(error.message).toBe("Boom");
74
+ expect(error.title).toBe("Boom");
68
75
  });
69
76
 
70
- it("includes the wrapped error stack in the message", () => {
77
+ it("exposes the wrapped error as the standard cause", () => {
78
+ const wrapped = new Error("inner");
79
+ const error = new APIError({cause: wrapped, title: "Outer"});
80
+ expect(error.message).toBe("Outer");
81
+ expect(error.cause).toBe(wrapped);
82
+ });
83
+
84
+ it("supports the deprecated error option as a cause alias", () => {
71
85
  const wrapped = new Error("inner");
72
86
  const error = new APIError({error: wrapped, title: "Outer"});
73
- expect(error.message).toContain("Outer");
74
- expect(error.message).toContain(wrapped.stack ?? "");
87
+ expect(error.cause).toBe(wrapped);
88
+ expect(error.error).toBe(wrapped);
89
+ });
90
+
91
+ it("derives the name from the status code", () => {
92
+ expect(new APIError({status: 400, title: "T"}).name).toBe("BadRequestError");
93
+ expect(new APIError({status: 401, title: "T"}).name).toBe("UnauthorizedError");
94
+ expect(new APIError({status: 403, title: "T"}).name).toBe("ForbiddenError");
95
+ expect(new APIError({status: 404, title: "T"}).name).toBe("NotFoundError");
96
+ expect(new APIError({status: 409, title: "T"}).name).toBe("ConflictError");
97
+ expect(new APIError({status: 500, title: "T"}).name).toBe("InternalServerError");
98
+ });
99
+
100
+ it("falls back to APIError for unmapped statuses", () => {
101
+ expect(new APIError({status: 418, title: "T"}).name).toBe("APIError");
102
+ });
103
+
104
+ it("derives the name from the code when provided", () => {
105
+ const error = new APIError({code: "update-admin-error", status: 403, title: "T"});
106
+ expect(error.name).toBe("UpdateAdminError");
107
+ });
108
+
109
+ it("keeps title in sync with message via the getter", () => {
110
+ const error = new APIError({title: "Original"});
111
+ error.message = "Changed";
112
+ expect(error.title).toBe("Changed");
75
113
  });
76
114
 
77
115
  it("defaults status to 500 when status is omitted", () => {
@@ -113,6 +151,57 @@ describe("APIError", () => {
113
151
  });
114
152
  });
115
153
 
154
+ describe("APIError subclasses", () => {
155
+ it("sets the status and name from the subclass", () => {
156
+ const error = new NotFoundError("Todo not found");
157
+ expect(error.status).toBe(404);
158
+ expect(error.name).toBe("NotFoundError");
159
+ expect(error.message).toBe("Todo not found");
160
+ expect(error).toBeInstanceOf(APIError);
161
+ expect(isAPIError(error)).toBe(true);
162
+ });
163
+
164
+ it("accepts full options", () => {
165
+ const cause = new Error("inner");
166
+ const error = new ForbiddenError({cause, detail: "Admins only", title: "Not allowed"});
167
+ expect(error.status).toBe(403);
168
+ expect(error.name).toBe("ForbiddenError");
169
+ expect(error.detail).toBe("Admins only");
170
+ expect(error.cause).toBe(cause);
171
+ });
172
+
173
+ it("keeps the subclass name even when a code is provided", () => {
174
+ const error = new BadRequestError({code: "invalid-cursor", title: "Bad cursor"});
175
+ expect(error.name).toBe("BadRequestError");
176
+ expect(error.status).toBe(400);
177
+ });
178
+
179
+ it("uses ValidationError with a 400 status", () => {
180
+ const error = new ValidationError({fields: {email: "Required"}, title: "Validation failed"});
181
+ expect(error.status).toBe(400);
182
+ expect(error.name).toBe("ValidationError");
183
+ expect(error.meta?.fields).toEqual({email: "Required"});
184
+ });
185
+
186
+ it("derives name from custom subclasses via new.target", () => {
187
+ class PaymentError extends APIError {
188
+ constructor(title: string) {
189
+ super({status: 402, title});
190
+ }
191
+ }
192
+ expect(new PaymentError("declined").name).toBe("PaymentError");
193
+ });
194
+
195
+ it("covers the remaining status subclasses", () => {
196
+ expect(new UnauthorizedError("U").status).toBe(401);
197
+ expect(new UnauthorizedError("U").name).toBe("UnauthorizedError");
198
+ expect(new ConflictError("C").status).toBe(409);
199
+ expect(new ConflictError("C").name).toBe("ConflictError");
200
+ expect(new InternalServerError("I").status).toBe(500);
201
+ expect(new InternalServerError("I").name).toBe("InternalServerError");
202
+ });
203
+ });
204
+
116
205
  describe("isAPIError", () => {
117
206
  it("returns true for an APIError instance", () => {
118
207
  expect(isAPIError(new APIError({title: "Boom"}))).toBe(true);
@@ -122,11 +211,21 @@ describe("isAPIError", () => {
122
211
  expect(isAPIError(new Error("nope"))).toBe(false);
123
212
  });
124
213
 
125
- it("returns true for any error whose name is APIError", () => {
214
+ it("returns true for any error whose name is APIError (transition fallback)", () => {
126
215
  const err = new Error("custom");
127
216
  err.name = "APIError";
128
217
  expect(isAPIError(err)).toBe(true);
129
218
  });
219
+
220
+ it("returns true for a branded error from a duplicate package copy", () => {
221
+ const err = new Error("branded") as Error & {isTerrenoAPIError?: boolean};
222
+ err.isTerrenoAPIError = true;
223
+ expect(isAPIError(err)).toBe(true);
224
+ });
225
+
226
+ it("returns true for subclasses", () => {
227
+ expect(isAPIError(new NotFoundError("missing"))).toBe(true);
228
+ });
130
229
  });
131
230
 
132
231
  describe("getDisableExternalErrorTracking", () => {
@@ -190,10 +289,10 @@ describe("getAPIErrorBody", () => {
190
289
  it("returns title and status by default", () => {
191
290
  const error = new APIError({status: 404, title: "Not Found"});
192
291
  const body = getAPIErrorBody(error);
193
- expect(body).toEqual({meta: {}, status: 404, title: "Not Found"});
292
+ expect(body).toEqual({status: 404, title: "Not Found"});
194
293
  });
195
294
 
196
- it("includes optional fields when set", () => {
295
+ it("includes optional fields when set, including disableExternalErrorTracking when true", () => {
197
296
  const error = new APIError({
198
297
  code: "not-found",
199
298
  detail: "Could not find resource",
@@ -211,18 +310,56 @@ describe("getAPIErrorBody", () => {
211
310
  disableExternalErrorTracking: true,
212
311
  id: "err-1",
213
312
  links: {about: "https://example.com/help"},
214
- meta: {},
215
313
  source: {pointer: "/data/id"},
216
314
  status: 404,
217
315
  title: "Not Found",
218
316
  });
219
317
  });
220
318
 
319
+ it("matches toJSON and excludes name, stack, and cause", () => {
320
+ const error = new APIError({
321
+ cause: new Error("inner"),
322
+ detail: "d",
323
+ status: 404,
324
+ title: "Not Found",
325
+ });
326
+ const body = error.toJSON() as unknown as Record<string, unknown>;
327
+ expect(body).toEqual(getAPIErrorBody(error) as unknown as typeof body);
328
+ expect(body.name).toBeUndefined();
329
+ expect(body.stack).toBeUndefined();
330
+ expect(body.cause).toBeUndefined();
331
+ expect(body.disableExternalErrorTracking).toBeUndefined();
332
+ expect(JSON.parse(JSON.stringify(error))).toEqual(JSON.parse(JSON.stringify(body)));
333
+ });
334
+
335
+ it("exposes deprecated error getter as alias for cause", () => {
336
+ const cause = new Error("inner");
337
+ const error = new APIError({cause, title: "Wrapped"});
338
+ expect(error.error).toBe(cause);
339
+ expect(error.cause).toBe(cause);
340
+ });
341
+
342
+ it("prefers explicit title over message when serializing legacy instances", () => {
343
+ const legacy = new Error("polluted message with stack") as Error & {
344
+ status: number;
345
+ title: string;
346
+ };
347
+ legacy.status = 404;
348
+ legacy.title = "Clean title";
349
+ const body = getAPIErrorBody(legacy as unknown as APIError);
350
+ expect(body.title).toBe("Clean title");
351
+ });
352
+
353
+ it("truncates fractional and invalid status codes to 500", () => {
354
+ expect(new APIError({status: 404.7, title: "x"}).status).toBe(404);
355
+ expect(new APIError({status: Number.NaN, title: "x"}).status).toBe(500);
356
+ expect(new APIError({status: 200, title: "x"}).status).toBe(500);
357
+ });
358
+
221
359
  it("omits empty meta and unset optional fields", () => {
222
360
  const error = new APIError({status: 400, title: "Bad"});
223
- // meta defaults to {} which is truthy, so it is included.
224
361
  const body = getAPIErrorBody(error);
225
- expect(body.meta).toEqual({});
362
+ expect(body.meta).toBeUndefined();
226
363
  expect(body.code).toBeUndefined();
227
364
  expect(body.detail).toBeUndefined();
228
365
  expect(body.id).toBeUndefined();
@@ -314,6 +451,34 @@ describe("apiErrorMiddleware", () => {
314
451
  expect(captureExceptionSpy).toHaveBeenCalledWith(err);
315
452
  });
316
453
 
454
+ it("fingerprints Sentry captures on the logical error type", () => {
455
+ const scope = {
456
+ setContext: mock(() => {}),
457
+ setFingerprint: mock(() => {}),
458
+ setTag: mock(() => {}),
459
+ };
460
+ const withScopeSpy = spyOn(Sentry, "withScope").mockImplementation(((
461
+ callback: (s: unknown) => void
462
+ ) => callback(scope)) as unknown as typeof Sentry.withScope);
463
+ try {
464
+ const err = new APIError({code: "todo-sync-failed", status: 502, title: "Sync failed"});
465
+ apiErrorMiddleware(err, req, res as unknown as Response, next as unknown as NextFunction);
466
+ expect(scope.setFingerprint).toHaveBeenCalledWith([
467
+ "TodoSyncFailed",
468
+ "todo-sync-failed",
469
+ "502",
470
+ ]);
471
+ expect(scope.setTag).toHaveBeenCalledWith("http.status_code", "502");
472
+ expect(scope.setTag).toHaveBeenCalledWith("api_error.code", "todo-sync-failed");
473
+ expect(scope.setContext).toHaveBeenCalledWith(
474
+ "apiError",
475
+ expect.objectContaining({status: 502, title: "Sync failed"})
476
+ );
477
+ } finally {
478
+ withScopeSpy.mockRestore();
479
+ }
480
+ });
481
+
317
482
  it("does not capture the exception when disableExternalErrorTracking is true", () => {
318
483
  const err = new APIError({
319
484
  disableExternalErrorTracking: true,