@terreno/api 0.27.0 → 0.28.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,6 +1,7 @@
1
1
  // noExplicitAny: test mock typing
2
2
  // biome-ignore-all lint/suspicious/noExplicitAny: test mock typing
3
- import {beforeEach, describe, expect, it} from "bun:test";
3
+ import {beforeEach, describe, expect, it, type mock} from "bun:test";
4
+ import * as Sentry from "@sentry/bun";
4
5
  import type express from "express";
5
6
  import supertest from "supertest";
6
7
  import type TestAgent from "supertest/lib/agent";
@@ -11,6 +12,8 @@ import {APIError} from "./errors";
11
12
  import {Permissions} from "./permissions";
12
13
  import {authAsUser, type Food, FoodModel, getBaseServer, setupDb, UserModel} from "./tests";
13
14
 
15
+ const captureExceptionMock = Sentry.captureException as unknown as ReturnType<typeof mock>;
16
+
14
17
  describe("pre and post hooks", () => {
15
18
  let server: TestAgent;
16
19
  let app: express.Application;
@@ -22,6 +25,7 @@ describe("pre and post hooks", () => {
22
25
  setupAuth(app, UserModel as any);
23
26
  addAuthRoutes(app, UserModel as any);
24
27
  agent = await authAsUser(app, "notAdmin");
28
+ captureExceptionMock.mockClear?.();
25
29
  });
26
30
 
27
31
  it("pre hooks change data", async () => {
@@ -230,7 +234,10 @@ describe("pre and post hooks", () => {
230
234
  .expect(400);
231
235
 
232
236
  expect(res.body.title).toBe("Custom preCreate error");
237
+ // Serialized when true so the frontend can suppress duplicate Sentry reporting; also
238
+ // suppresses Sentry capture in apiErrorMiddleware.
233
239
  expect(res.body.disableExternalErrorTracking).toBe(true);
240
+ expect(captureExceptionMock).not.toHaveBeenCalled();
234
241
  });
235
242
 
236
243
  it("preCreate hook preserves disableExternalErrorTracking on non-APIError", async () => {
@@ -263,7 +270,10 @@ describe("pre and post hooks", () => {
263
270
  .expect(400);
264
271
 
265
272
  expect(res.body.title).toContain("preCreate hook error");
273
+ // Serialized when true so the frontend can suppress duplicate Sentry reporting; also
274
+ // suppresses Sentry capture in apiErrorMiddleware.
266
275
  expect(res.body.disableExternalErrorTracking).toBe(true);
276
+ expect(captureExceptionMock).not.toHaveBeenCalled();
267
277
  });
268
278
 
269
279
  it("preUpdate hook preserves disableExternalErrorTracking on APIError", async () => {
@@ -311,7 +321,10 @@ describe("pre and post hooks", () => {
311
321
  .expect(400);
312
322
 
313
323
  expect(res.body.title).toBe("Custom preUpdate error");
324
+ // Serialized when true so the frontend can suppress duplicate Sentry reporting; also
325
+ // suppresses Sentry capture in apiErrorMiddleware.
314
326
  expect(res.body.disableExternalErrorTracking).toBe(true);
327
+ expect(captureExceptionMock).not.toHaveBeenCalled();
315
328
  });
316
329
 
317
330
  it("preUpdate hook preserves disableExternalErrorTracking on non-APIError", async () => {
@@ -357,7 +370,10 @@ describe("pre and post hooks", () => {
357
370
  .expect(400);
358
371
 
359
372
  expect(res.body.title).toContain("preUpdate hook error");
373
+ // Serialized when true so the frontend can suppress duplicate Sentry reporting; also
374
+ // suppresses Sentry capture in apiErrorMiddleware.
360
375
  expect(res.body.disableExternalErrorTracking).toBe(true);
376
+ expect(captureExceptionMock).not.toHaveBeenCalled();
361
377
  });
362
378
 
363
379
  it("preDelete hook preserves disableExternalErrorTracking on non-APIError", async () => {
@@ -398,7 +414,10 @@ describe("pre and post hooks", () => {
398
414
  const res = await agent.delete(`/food/${spinach._id}`).expect(403);
399
415
 
400
416
  expect(res.body.title).toContain("preDelete hook error");
417
+ // Serialized when true so the frontend can suppress duplicate Sentry reporting; also
418
+ // suppresses Sentry capture in apiErrorMiddleware.
401
419
  expect(res.body.disableExternalErrorTracking).toBe(true);
420
+ expect(captureExceptionMock).not.toHaveBeenCalled();
402
421
  });
403
422
  });
404
423
 
@@ -426,6 +445,7 @@ describe("hook error handling", () => {
426
445
  app = getBaseServer();
427
446
  setupAuth(app, UserModel as any);
428
447
  addAuthRoutes(app, UserModel as any);
448
+ captureExceptionMock.mockClear?.();
429
449
  });
430
450
 
431
451
  it("preCreate returning undefined throws error", async () => {
@@ -701,6 +721,9 @@ describe("hook error handling", () => {
701
721
 
702
722
  const res = await agent.delete(`/food/${spinach._id}`).expect(400);
703
723
  expect(res.body.title).toBe("Custom preDelete APIError");
724
+ // Serialized when true so the frontend can suppress duplicate Sentry reporting; also
725
+ // suppresses Sentry capture in apiErrorMiddleware.
704
726
  expect(res.body.disableExternalErrorTracking).toBe(true);
727
+ expect(captureExceptionMock).not.toHaveBeenCalled();
705
728
  });
706
729
  });
package/src/api.test.ts CHANGED
@@ -626,7 +626,8 @@ describe("@terreno/api", () => {
626
626
  server = supertest(app);
627
627
 
628
628
  const res = await server.post("/required").send({about: "test"}).expect(400);
629
- expect(res.body.title).toContain("Required");
629
+ expect(res.body.title).toBe("Create error");
630
+ expect(res.body.detail).toContain("Required");
630
631
  });
631
632
 
632
633
  it("preDelete hook throwing APIError is re-thrown", async () => {
@@ -655,6 +656,7 @@ describe("@terreno/api", () => {
655
656
 
656
657
  const res = await agent.delete(`/food/${spinach._id}`).expect(400);
657
658
  expect(res.body.title).toBe("Custom preDelete APIError");
659
+ // Serialized when true so the frontend can suppress duplicate Sentry reporting.
658
660
  expect(res.body.disableExternalErrorTracking).toBe(true);
659
661
  });
660
662
  });
@@ -1379,7 +1381,8 @@ describe("@terreno/api", () => {
1379
1381
 
1380
1382
  // Send without required 'name' field
1381
1383
  const res = await server.post("/required").send({about: "test"}).expect(400);
1382
- expect(res.body.title).toContain("Required");
1384
+ expect(res.body.title).toBe("Create error");
1385
+ expect(res.body.detail).toContain("Required");
1383
1386
  });
1384
1387
 
1385
1388
  it("preDelete hook throwing APIError is re-thrown", async () => {
@@ -1408,6 +1411,7 @@ describe("@terreno/api", () => {
1408
1411
 
1409
1412
  const res = await agent.delete(`/food/${spinach._id}`).expect(400);
1410
1413
  expect(res.body.title).toBe("Custom preDelete APIError");
1414
+ // Serialized when true so the frontend can suppress duplicate Sentry reporting.
1411
1415
  expect(res.body.disableExternalErrorTracking).toBe(true);
1412
1416
  });
1413
1417
  });
@@ -2166,7 +2170,8 @@ describe("@terreno/api", () => {
2166
2170
  .send({calories: 10, name: "New Food", ownerId: admin._id})
2167
2171
  .expect(400);
2168
2172
 
2169
- expect(res.body.title).toContain("generic transform error");
2173
+ expect(res.body.title).toBe("Transform error");
2174
+ expect(res.body.detail).toContain("generic transform error");
2170
2175
  });
2171
2176
  });
2172
2177
 
@@ -2212,7 +2217,8 @@ describe("@terreno/api", () => {
2212
2217
  it("wraps non-APIError transform errors in a 403 APIError on update", async () => {
2213
2218
  const res = await agent.patch(`/food/${spinach._id}`).send({name: "Updated"}).expect(403);
2214
2219
 
2215
- expect(res.body.title).toContain("update transform error");
2220
+ expect(res.body.title).toBe("PATCH failed");
2221
+ expect(res.body.detail).toContain("update transform error");
2216
2222
  });
2217
2223
  });
2218
2224
  });
package/src/api.ts CHANGED
@@ -20,7 +20,6 @@ import {
20
20
  APIError,
21
21
  apiErrorMiddleware,
22
22
  errorMessage,
23
- errorStack,
24
23
  getDisableExternalErrorTracking,
25
24
  isAPIError,
26
25
  } from "./errors";
@@ -377,7 +376,7 @@ const mergeDateRangeQueryParams = <T>(
377
376
  }
378
377
  const baseField = match[1];
379
378
  const path = schema.path(baseField);
380
- if (!path || path.instance !== "Date") {
379
+ if (path?.instance !== "Date") {
381
380
  continue;
382
381
  }
383
382
  dateRangeBases.add(baseField);
@@ -655,10 +654,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
655
654
  throw error;
656
655
  }
657
656
  throw new APIError({
657
+ cause: error,
658
+ code: "transform-error",
659
+ detail: errorMessage(error),
658
660
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
659
- error,
660
661
  status: 400,
661
- title: errorMessage(error),
662
+ title: "Transform error",
662
663
  });
663
664
  }
664
665
  if (options.preCreate) {
@@ -669,10 +670,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
669
670
  throw error;
670
671
  }
671
672
  throw new APIError({
673
+ cause: error,
674
+ code: "pre-create-hook-error",
675
+ detail: errorMessage(error),
672
676
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
673
- error,
674
677
  status: 400,
675
- title: `preCreate hook error: ${errorMessage(error)}`,
678
+ title: "preCreate hook error",
676
679
  });
677
680
  }
678
681
  if (body === undefined) {
@@ -702,10 +705,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
702
705
  data = (await model.create(body as T)) as Document<unknown, unknown, unknown> & T;
703
706
  } catch (error: unknown) {
704
707
  throw new APIError({
708
+ cause: error,
709
+ code: "create-error",
710
+ detail: errorMessage(error),
705
711
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
706
- error,
707
712
  status: 400,
708
- title: errorMessage(error),
713
+ title: "Create error",
709
714
  });
710
715
  }
711
716
 
@@ -718,10 +723,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
718
723
  data = await populateQuery.exec();
719
724
  } catch (error: unknown) {
720
725
  throw new APIError({
726
+ cause: error,
727
+ code: "populate-error",
728
+ detail: errorMessage(error),
721
729
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
722
- error,
723
730
  status: 400,
724
- title: `Populate error: ${errorMessage(error)}`,
731
+ title: "Populate error",
725
732
  });
726
733
  }
727
734
  }
@@ -731,10 +738,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
731
738
  await options.postCreate(data, req);
732
739
  } catch (error: unknown) {
733
740
  throw new APIError({
741
+ cause: error,
742
+ code: "post-create-hook-error",
743
+ detail: errorMessage(error),
734
744
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
735
- error,
736
745
  status: 400,
737
- title: `postCreate hook error: ${errorMessage(error)}`,
746
+ title: "postCreate hook error",
738
747
  });
739
748
  }
740
749
  }
@@ -743,9 +752,11 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
743
752
  return res.status(201).json({data: serialized});
744
753
  } catch (error: unknown) {
745
754
  throw new APIError({
755
+ cause: error,
756
+ code: "response-handler-error",
757
+ detail: errorMessage(error),
746
758
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
747
- error,
748
- title: `responseHandler error: ${errorMessage(error)}`,
759
+ title: "responseHandler error",
749
760
  });
750
761
  }
751
762
  })
@@ -800,10 +811,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
800
811
  queryFilter = await options.queryFilter(req.user, query);
801
812
  } catch (error: unknown) {
802
813
  throw new APIError({
814
+ cause: error,
815
+ code: "query-filter-error",
816
+ detail: String(error),
803
817
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
804
- error,
805
818
  status: 400,
806
- title: `Query filter error: ${error}`,
819
+ title: "Query filter error",
807
820
  });
808
821
  }
809
822
 
@@ -852,9 +865,11 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
852
865
  data = (await populatedQuery.exec()) as (Document<unknown, unknown, unknown> & T)[];
853
866
  } catch (error: unknown) {
854
867
  throw new APIError({
868
+ cause: error,
869
+ code: "list-error",
870
+ detail: errorMessage(error),
855
871
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
856
- error,
857
- title: `List error: ${errorStack(error)}`,
872
+ title: "List error",
858
873
  });
859
874
  }
860
875
 
@@ -864,9 +879,11 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
864
879
  serialized = await responseHandler(data, "list", req, options);
865
880
  } catch (error: unknown) {
866
881
  throw new APIError({
882
+ cause: error,
883
+ code: "response-handler-error",
884
+ detail: errorMessage(error),
867
885
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
868
- error,
869
- title: `responseHandler error: ${errorMessage(error)}`,
886
+ title: "responseHandler error",
870
887
  });
871
888
  }
872
889
 
@@ -899,9 +916,11 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
899
916
  return res.json({data: serialized});
900
917
  } catch (error: unknown) {
901
918
  throw new APIError({
919
+ cause: error,
920
+ code: "serialization-error",
921
+ detail: errorMessage(error),
902
922
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
903
- error,
904
- title: `Serialization error: ${errorMessage(error)}`,
923
+ title: "Serialization error",
905
924
  });
906
925
  }
907
926
  })
@@ -922,9 +941,11 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
922
941
  return res.json({data: serialized});
923
942
  } catch (error: unknown) {
924
943
  throw new APIError({
944
+ cause: error,
945
+ code: "response-handler-error",
946
+ detail: errorMessage(error),
925
947
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
926
- error,
927
- title: `responseHandler error: ${errorMessage(error)}`,
948
+ title: "responseHandler error",
928
949
  });
929
950
  }
930
951
  })
@@ -961,10 +982,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
961
982
  throw error;
962
983
  }
963
984
  throw new APIError({
985
+ cause: error,
986
+ code: "transform-error",
987
+ detail: `PATCH failed on ${req.params.id} for user ${req.user?.id}: ${errorMessage(error)}`,
964
988
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
965
- error,
966
989
  status: 403,
967
- title: `PATCH failed on ${req.params.id} for user ${req.user?.id}: ${errorMessage(error)}`,
990
+ title: "PATCH failed",
968
991
  });
969
992
  }
970
993
 
@@ -983,10 +1006,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
983
1006
  throw error;
984
1007
  }
985
1008
  throw new APIError({
1009
+ cause: error,
1010
+ code: "pre-update-hook-error",
1011
+ detail: `preUpdate hook error on ${req.params.id}: ${errorMessage(error)}`,
986
1012
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
987
- error,
988
1013
  status: 400,
989
- title: `preUpdate hook error on ${req.params.id}: ${errorMessage(error)}`,
1014
+ title: "preUpdate hook error",
990
1015
  });
991
1016
  }
992
1017
  if (body === undefined) {
@@ -1073,10 +1098,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
1073
1098
  await doc.save();
1074
1099
  } catch (error: unknown) {
1075
1100
  throw new APIError({
1101
+ cause: error,
1102
+ code: "update-save-error",
1103
+ detail: `preUpdate hook save error on ${req.params.id}: ${errorMessage(error)}`,
1076
1104
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
1077
- error,
1078
1105
  status: 400,
1079
- title: `preUpdate hook save error on ${req.params.id}: ${errorMessage(error)}`,
1106
+ title: "preUpdate hook save error",
1080
1107
  });
1081
1108
  }
1082
1109
 
@@ -1093,10 +1120,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
1093
1120
  await options.postUpdate(doc, body, req, prevDoc);
1094
1121
  } catch (error: unknown) {
1095
1122
  throw new APIError({
1123
+ cause: error,
1124
+ code: "post-update-hook-error",
1125
+ detail: `postUpdate hook error on ${req.params.id}: ${errorMessage(error)}`,
1096
1126
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
1097
- error,
1098
1127
  status: 400,
1099
- title: `postUpdate hook error on ${req.params.id}: ${errorMessage(error)}`,
1128
+ title: "postUpdate hook error",
1100
1129
  });
1101
1130
  }
1102
1131
  }
@@ -1106,9 +1135,11 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
1106
1135
  return res.json({data: serialized});
1107
1136
  } catch (error: unknown) {
1108
1137
  throw new APIError({
1138
+ cause: error,
1139
+ code: "response-handler-error",
1140
+ detail: errorMessage(error),
1109
1141
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
1110
- error,
1111
- title: `responseHandler error: ${errorMessage(error)}`,
1142
+ title: "responseHandler error",
1112
1143
  });
1113
1144
  }
1114
1145
  })
@@ -1135,10 +1166,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
1135
1166
  throw error;
1136
1167
  }
1137
1168
  throw new APIError({
1169
+ cause: error,
1170
+ code: "pre-delete-hook-error",
1171
+ detail: `preDelete hook error on ${req.params.id}: ${errorMessage(error)}`,
1138
1172
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
1139
- error,
1140
1173
  status: 403,
1141
- title: `preDelete hook error on ${req.params.id}: ${errorMessage(error)}`,
1174
+ title: "preDelete hook error",
1142
1175
  });
1143
1176
  }
1144
1177
  if (body === undefined) {
@@ -1170,10 +1203,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
1170
1203
  await doc.deleteOne();
1171
1204
  } catch (error: unknown) {
1172
1205
  throw new APIError({
1206
+ cause: error,
1207
+ code: "delete-error",
1208
+ detail: errorMessage(error),
1173
1209
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
1174
- error,
1175
1210
  status: 400,
1176
- title: errorMessage(error),
1211
+ title: "Delete error",
1177
1212
  });
1178
1213
  }
1179
1214
  }
@@ -1183,10 +1218,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
1183
1218
  await options.postDelete(req, doc);
1184
1219
  } catch (error: unknown) {
1185
1220
  throw new APIError({
1221
+ cause: error,
1222
+ code: "post-delete-hook-error",
1223
+ detail: errorMessage(error),
1186
1224
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
1187
- error,
1188
1225
  status: 400,
1189
- title: `postDelete hook error: ${errorMessage(error)}`,
1226
+ title: "postDelete hook error",
1190
1227
  });
1191
1228
  }
1192
1229
  }
@@ -1282,10 +1319,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
1282
1319
  throw error;
1283
1320
  }
1284
1321
  throw new APIError({
1322
+ cause: error,
1323
+ code: "transform-error",
1324
+ detail: errorMessage(error),
1285
1325
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
1286
- error,
1287
1326
  status: 403,
1288
- title: errorMessage(error),
1327
+ title: "Transform error",
1289
1328
  });
1290
1329
  }
1291
1330
 
@@ -1294,10 +1333,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
1294
1333
  body = await options.preUpdate(body, req);
1295
1334
  } catch (error: unknown) {
1296
1335
  throw new APIError({
1336
+ cause: error,
1337
+ code: "pre-update-hook-error",
1338
+ detail: `preUpdate hook error on ${req.params.id}: ${errorMessage(error)}`,
1297
1339
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
1298
- error,
1299
1340
  status: 400,
1300
- title: `preUpdate hook error on ${req.params.id}: ${errorMessage(error)}`,
1341
+ title: "preUpdate hook error",
1301
1342
  });
1302
1343
  }
1303
1344
  if (body === undefined) {
@@ -1323,10 +1364,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
1323
1364
  await doc.save();
1324
1365
  } catch (error: unknown) {
1325
1366
  throw new APIError({
1367
+ cause: error,
1368
+ code: "update-save-error",
1369
+ detail: `PATCH Pre Update error on ${req.params.id}: ${errorMessage(error)}`,
1326
1370
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
1327
- error,
1328
1371
  status: 400,
1329
- title: `PATCH Pre Update error on ${req.params.id}: ${errorMessage(error)}`,
1372
+ title: "PATCH Pre Update error",
1330
1373
  });
1331
1374
  }
1332
1375
 
@@ -1340,10 +1383,12 @@ function _buildModelRouter<T>(model: Model<T>, options: ModelRouterOptions<T>):
1340
1383
  );
1341
1384
  } catch (error: unknown) {
1342
1385
  throw new APIError({
1386
+ cause: error,
1387
+ code: "post-update-hook-error",
1388
+ detail: `PATCH Post Update error on ${req.params.id}: ${errorMessage(error)}`,
1343
1389
  disableExternalErrorTracking: getDisableExternalErrorTracking(error),
1344
- error,
1345
1390
  status: 400,
1346
- title: `PATCH Post Update error on ${req.params.id}: ${errorMessage(error)}`,
1391
+ title: "PATCH Post Update error",
1347
1392
  });
1348
1393
  }
1349
1394
  }
package/src/auth.test.ts CHANGED
@@ -502,7 +502,7 @@ describe("auth tests", () => {
502
502
 
503
503
  it("signup without credentials is a 400, not a fallthrough 500", async () => {
504
504
  const res = await agent.post("/auth/signup").send({email: "new@example.com"}).expect(400);
505
- expect(res.body).toEqual({meta: {}, status: 400, title: "Missing credentials"});
505
+ expect(res.body).toEqual({status: 400, title: "Missing credentials"});
506
506
  });
507
507
 
508
508
  it("signup user with email that is already registered", async () => {
@@ -1,6 +1,7 @@
1
1
  import {afterEach, beforeEach, describe, expect, it} from "bun:test";
2
2
 
3
3
  import {Config} from "./config";
4
+ import {APIError} from "./errors";
4
5
 
5
6
  const KEYS = [
6
7
  "TERRENO_CFG_STRING",
@@ -108,12 +109,26 @@ describe("Config", () => {
108
109
 
109
110
  it("throws on non-numeric values", () => {
110
111
  process.env.TERRENO_CFG_NUM = "not-a-number";
111
- expect(() => Config.getNumber("TERRENO_CFG_NUM")).toThrow(/not a valid number/);
112
+ try {
113
+ Config.getNumber("TERRENO_CFG_NUM");
114
+ expect.unreachable("expected throw");
115
+ } catch (error) {
116
+ expect(error).toBeInstanceOf(APIError);
117
+ expect((error as APIError).title).toBe("Invalid config value");
118
+ expect((error as APIError).detail).toContain("not a valid number");
119
+ }
112
120
  });
113
121
 
114
122
  it("throws on partially-numeric strings like '5000ms'", () => {
115
123
  process.env.TERRENO_CFG_NUM = "5000ms";
116
- expect(() => Config.getNumber("TERRENO_CFG_NUM")).toThrow(/not a valid number/);
124
+ try {
125
+ Config.getNumber("TERRENO_CFG_NUM");
126
+ expect.unreachable("expected throw");
127
+ } catch (error) {
128
+ expect(error).toBeInstanceOf(APIError);
129
+ expect((error as APIError).title).toBe("Invalid config value");
130
+ expect((error as APIError).detail).toContain("not a valid number");
131
+ }
117
132
  });
118
133
 
119
134
  it("supports floats", () => {
@@ -166,7 +181,14 @@ describe("Config", () => {
166
181
 
167
182
  it("throws on malformed JSON instead of silently returning undefined", () => {
168
183
  process.env.TERRENO_CFG_JSON = "{not json";
169
- expect(() => Config.getJSON("TERRENO_CFG_JSON")).toThrow(/not valid JSON/);
184
+ try {
185
+ Config.getJSON("TERRENO_CFG_JSON");
186
+ expect.unreachable("expected throw");
187
+ } catch (error) {
188
+ expect(error).toBeInstanceOf(APIError);
189
+ expect((error as APIError).title).toBe("Invalid config value");
190
+ expect((error as APIError).detail).toContain("not valid JSON");
191
+ }
170
192
  });
171
193
  });
172
194
 
package/src/config.ts CHANGED
@@ -91,9 +91,10 @@ const getNumber = (key: string): number | undefined => {
91
91
  const parsed = Number(raw);
92
92
  if (!Number.isFinite(parsed)) {
93
93
  throw new APIError({
94
- error: new Error(`Config key "${key}" is not a valid number: ${JSON.stringify(raw)}`),
94
+ code: "invalid-config-value",
95
+ detail: `Config key "${key}" is not a valid number: ${JSON.stringify(raw)}`,
95
96
  status: 500,
96
- title: `Config key "${key}" is not a valid number`,
97
+ title: "Invalid config value",
97
98
  });
98
99
  }
99
100
  return parsed;
@@ -122,9 +123,11 @@ const getJSON = <T = unknown>(key: string): T | undefined => {
122
123
  return JSON.parse(raw) as T;
123
124
  } catch (error) {
124
125
  throw new APIError({
125
- error: new Error(`Config key "${key}" is not valid JSON: ${(error as Error).message}`),
126
+ cause: error,
127
+ code: "invalid-config-value",
128
+ detail: `Config key "${key}" is not valid JSON: ${(error as Error).message}`,
126
129
  status: 500,
127
- title: `Config key "${key}" is not valid JSON`,
130
+ title: "Invalid config value",
128
131
  });
129
132
  }
130
133
  };
@@ -52,7 +52,8 @@ describe("loadDocOr404", () => {
52
52
  expect(error).toBeInstanceOf(APIError);
53
53
  const apiError = error as APIError;
54
54
  expect(apiError.status).toBe(404);
55
- expect(apiError.meta).toBeUndefined();
55
+ expect(apiError.title).toBe("Document not found");
56
+ expect(apiError.meta).toEqual({});
56
57
  }
57
58
  });
58
59
  });
package/src/docLoader.ts CHANGED
@@ -1,8 +1,7 @@
1
- import * as Sentry from "@sentry/bun";
2
1
  import mongoose, {type Model} from "mongoose";
3
2
 
4
3
  import {addPopulateToQuery} from "./api";
5
- import {APIError, isAPIError} from "./errors";
4
+ import {APIError, isAPIError, NotFoundError} from "./errors";
6
5
  import type {PopulatePath} from "./populate";
7
6
 
8
7
  /**
@@ -27,9 +26,11 @@ export const loadDocOr404 = async <T>(
27
26
  throw error;
28
27
  }
29
28
  throw new APIError({
30
- error: error as Error,
29
+ cause: error,
30
+ code: "get-error",
31
+ detail: `GET failed on ${id}`,
31
32
  status: 500,
32
- title: `GET failed on ${id}`,
33
+ title: "GET error",
33
34
  });
34
35
  }
35
36
  if (!data) {
@@ -37,14 +38,13 @@ export const loadDocOr404 = async <T>(
37
38
  _id: new mongoose.Types.ObjectId(id),
38
39
  });
39
40
 
41
+ const notFoundDetail = `Document ${id} not found for model ${model.modelName}`;
42
+
40
43
  if (!hiddenDoc) {
41
- Sentry.captureMessage(`Document ${id} not found for model ${model.modelName}`);
42
- const error = new APIError({
43
- status: 404,
44
- title: `Document ${id} not found for model ${model.modelName}`,
44
+ throw new NotFoundError({
45
+ detail: notFoundDetail,
46
+ title: "Document not found",
45
47
  });
46
- error.meta = undefined;
47
- throw error;
48
48
  }
49
49
 
50
50
  let reason: {[key: string]: string} | null = null;
@@ -57,18 +57,16 @@ export const loadDocOr404 = async <T>(
57
57
  }
58
58
 
59
59
  if (!reason) {
60
- const error = new APIError({
61
- status: 404,
62
- title: `Document ${id} not found for model ${model.modelName}`,
60
+ throw new NotFoundError({
61
+ detail: notFoundDetail,
62
+ title: "Document not found",
63
63
  });
64
- error.meta = undefined;
65
- throw error;
66
64
  }
67
- throw new APIError({
65
+ throw new NotFoundError({
66
+ detail: notFoundDetail,
68
67
  disableExternalErrorTracking: true,
69
68
  meta: reason,
70
- status: 404,
71
- title: `Document ${id} not found for model ${model.modelName}`,
69
+ title: "Document not found",
72
70
  });
73
71
  }
74
72