@terreno/api 0.29.0 → 0.30.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 (45) hide show
  1. package/CHANGELOG.md +79 -0
  2. package/dist/actions.js +17 -8
  3. package/dist/actions.test.js +8 -3
  4. package/dist/api.arrayOperations.test.js +16 -8
  5. package/dist/api.coverage.test.js +2 -1
  6. package/dist/api.errorPaths.test.js +522 -1
  7. package/dist/api.errors.test.js +9 -0
  8. package/dist/api.js +137 -170
  9. package/dist/api.query.test.js +20 -8
  10. package/dist/api.test.js +12 -6
  11. package/dist/auth.coverage.test.d.ts +1 -0
  12. package/dist/auth.coverage.test.js +202 -0
  13. package/dist/auth.test.js +159 -0
  14. package/dist/docLoader.js +5 -1
  15. package/dist/envConfigurationPlugin.d.ts +1 -1
  16. package/dist/envConfigurationPlugin.js +0 -2
  17. package/dist/errors.d.ts +14 -0
  18. package/dist/errors.js +24 -2
  19. package/dist/errors.test.js +137 -0
  20. package/dist/httpClient.js +5 -1
  21. package/dist/plugins.d.ts +4 -4
  22. package/dist/plugins.js +51 -9
  23. package/dist/plugins.test.js +61 -17
  24. package/dist/realtime/registry.d.ts +3 -2
  25. package/package.json +2 -2
  26. package/src/actions.test.ts +8 -3
  27. package/src/actions.ts +19 -10
  28. package/src/api.arrayOperations.test.ts +16 -8
  29. package/src/api.coverage.test.ts +2 -1
  30. package/src/api.errorPaths.test.ts +313 -3
  31. package/src/api.errors.test.ts +12 -0
  32. package/src/api.query.test.ts +20 -8
  33. package/src/api.test.ts +12 -6
  34. package/src/api.ts +141 -161
  35. package/src/auth.coverage.test.ts +101 -0
  36. package/src/auth.test.ts +125 -2
  37. package/src/docLoader.ts +6 -2
  38. package/src/envConfigurationPlugin.ts +2 -6
  39. package/src/errors.test.ts +106 -1
  40. package/src/errors.ts +25 -1
  41. package/src/httpClient.ts +6 -2
  42. package/src/plugins.test.ts +45 -9
  43. package/src/plugins.ts +72 -35
  44. package/src/realtime/changeStreamWatcher.ts +8 -1
  45. package/src/realtime/registry.ts +3 -4
package/dist/api.js CHANGED
@@ -141,6 +141,32 @@ var permissions_1 = require("./permissions");
141
141
  var registry_1 = require("./realtime/registry");
142
142
  var transformers_1 = require("./transformers");
143
143
  var utils_1 = require("./utils");
144
+ /**
145
+ * Picks the error to throw for something caught in a hook, transformer, or Mongoose middleware.
146
+ * An `APIError` is passed through untouched so its status, code, detail, and meta reach the client;
147
+ * anything else is wrapped in the given framework error. `wrapper` may override any field,
148
+ * including `detail`, which defaults to the caught error's text.
149
+ */
150
+ var passthroughOrWrap = function (error, wrapper) {
151
+ if ((0, errors_1.isAPIError)(error)) {
152
+ return error;
153
+ }
154
+ return new errors_1.APIError(__assign({ cause: error, detail: (0, errors_1.errorDetail)(error), disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error) }, wrapper));
155
+ };
156
+ /**
157
+ * `passthroughOrWrap` for write paths (`model.create`, `doc.save`). Mongoose validation and cast
158
+ * errors are converted first so their per-field messages survive as `meta.fields` instead of being
159
+ * flattened into the wrapper's `detail`.
160
+ */
161
+ var passthroughOrWrapWrite = function (error, wrapper) {
162
+ if (!(0, errors_1.isAPIError)(error) && error instanceof Error) {
163
+ var converted = (0, errors_1.mongooseErrorToAPIError)(error);
164
+ if (converted) {
165
+ return converted;
166
+ }
167
+ }
168
+ return passthroughOrWrap(error, wrapper);
169
+ };
144
170
  var addPopulateToQuery = function (
145
171
  // noExplicitAny: mongoose Query type parameters vary widely across populated/unpopulated documents — caller passes concrete types
146
172
  // biome-ignore lint/suspicious/noExplicitAny: mongoose Query type parameters vary widely across populated/unpopulated documents — caller passes concrete types
@@ -182,9 +208,11 @@ var COMPLEX_QUERY_PARAMS = ["$and", "$or"];
182
208
  var parseDateRangeBound = function (rawValue, queryKey) {
183
209
  var parsed = luxon_1.DateTime.fromISO(String(rawValue), { zone: "utc" });
184
210
  if (!parsed.isValid) {
185
- throw new errors_1.APIError({
186
- status: 400,
187
- title: "Invalid date for query parameter ".concat(queryKey),
211
+ throw new errors_1.BadRequestError({
212
+ code: "invalid-date-query-parameter",
213
+ detail: "Invalid date for query parameter ".concat(queryKey),
214
+ source: { parameter: queryKey },
215
+ title: "Invalid date query parameter",
188
216
  });
189
217
  }
190
218
  return parsed.toJSDate();
@@ -298,9 +326,11 @@ var checkQueryParamAllowed = function (queryParam, queryParamValue, queryFields)
298
326
  return;
299
327
  }
300
328
  if (!queryFields.includes(queryParam)) {
301
- throw new errors_1.APIError({
302
- status: 400,
303
- title: "".concat(queryParam, " is not allowed as a query param."),
329
+ throw new errors_1.BadRequestError({
330
+ code: "query-param-not-allowed",
331
+ detail: "".concat(queryParam, " is not allowed as a query param."),
332
+ source: { parameter: queryParam },
333
+ title: "Query parameter not allowed",
304
334
  });
305
335
  }
306
336
  };
@@ -418,8 +448,7 @@ function modelRouter(pathOrModel, modelOrOptions, maybeOptions) {
418
448
  }
419
449
  return router;
420
450
  }
421
- function _buildModelRouter(model, options) {
422
- var _this = this;
451
+ var _buildModelRouter = function (model, options) {
423
452
  var _a;
424
453
  var router = express_1.default.Router();
425
454
  (0, actions_1.assertNoActionCollisions)(model, options);
@@ -438,7 +467,7 @@ function _buildModelRouter(model, options) {
438
467
  (0, openApi_1.createOpenApiMiddleware)(model, options),
439
468
  (0, permissions_1.permissionMiddleware)(model, options),
440
469
  createValidation,
441
- ], (0, exports.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
470
+ ], (0, exports.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
442
471
  var body, error_1, data, error_2, populateQuery, error_3, error_4, serialized, error_5;
443
472
  return __generator(this, function (_a) {
444
473
  switch (_a.label) {
@@ -447,14 +476,8 @@ function _buildModelRouter(model, options) {
447
476
  body = (0, transformers_1.transform)(options, req.body, "create", req.user);
448
477
  }
449
478
  catch (error) {
450
- if ((0, errors_1.isAPIError)(error)) {
451
- throw error;
452
- }
453
- throw new errors_1.APIError({
454
- cause: error,
479
+ throw passthroughOrWrap(error, {
455
480
  code: "transform-error",
456
- detail: (0, errors_1.errorMessage)(error),
457
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error),
458
481
  status: 400,
459
482
  title: "Transform error",
460
483
  });
@@ -469,38 +492,32 @@ function _buildModelRouter(model, options) {
469
492
  return [3 /*break*/, 4];
470
493
  case 3:
471
494
  error_1 = _a.sent();
472
- if ((0, errors_1.isAPIError)(error_1)) {
473
- throw error_1;
474
- }
475
- throw new errors_1.APIError({
476
- cause: error_1,
495
+ throw passthroughOrWrap(error_1, {
477
496
  code: "pre-create-hook-error",
478
- detail: (0, errors_1.errorMessage)(error_1),
479
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_1),
480
497
  status: 400,
481
498
  title: "preCreate hook error",
482
499
  });
483
500
  case 4:
484
501
  if (body === undefined) {
485
- throw new errors_1.APIError({
502
+ throw new errors_1.ForbiddenError({
503
+ code: "create-not-allowed",
486
504
  detail: "A body must be returned from preCreate",
487
- status: 403,
488
505
  title: "Create not allowed",
489
506
  });
490
507
  }
491
508
  if (body === null) {
492
- throw new errors_1.APIError({
509
+ throw new errors_1.ForbiddenError({
510
+ code: "create-not-allowed",
493
511
  detail: "preCreate hook returned null",
494
- status: 403,
495
512
  title: "Create not allowed",
496
513
  });
497
514
  }
498
515
  _a.label = 5;
499
516
  case 5:
500
517
  if (body === undefined) {
501
- throw new errors_1.APIError({
518
+ throw new errors_1.BadRequestError({
519
+ code: "invalid-request-body",
502
520
  detail: "Body is undefined",
503
- status: 400,
504
521
  title: "Invalid request body",
505
522
  });
506
523
  }
@@ -513,11 +530,8 @@ function _buildModelRouter(model, options) {
513
530
  return [3 /*break*/, 9];
514
531
  case 8:
515
532
  error_2 = _a.sent();
516
- throw new errors_1.APIError({
517
- cause: error_2,
533
+ throw passthroughOrWrapWrite(error_2, {
518
534
  code: "create-error",
519
- detail: (0, errors_1.errorMessage)(error_2),
520
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_2),
521
535
  status: 400,
522
536
  title: "Create error",
523
537
  });
@@ -534,11 +548,8 @@ function _buildModelRouter(model, options) {
534
548
  return [3 /*break*/, 13];
535
549
  case 12:
536
550
  error_3 = _a.sent();
537
- throw new errors_1.APIError({
538
- cause: error_3,
551
+ throw passthroughOrWrap(error_3, {
539
552
  code: "populate-error",
540
- detail: (0, errors_1.errorMessage)(error_3),
541
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_3),
542
553
  status: 400,
543
554
  title: "Populate error",
544
555
  });
@@ -553,11 +564,8 @@ function _buildModelRouter(model, options) {
553
564
  return [3 /*break*/, 17];
554
565
  case 16:
555
566
  error_4 = _a.sent();
556
- throw new errors_1.APIError({
557
- cause: error_4,
567
+ throw passthroughOrWrap(error_4, {
558
568
  code: "post-create-hook-error",
559
- detail: (0, errors_1.errorMessage)(error_4),
560
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_4),
561
569
  status: 400,
562
570
  title: "postCreate hook error",
563
571
  });
@@ -569,11 +577,8 @@ function _buildModelRouter(model, options) {
569
577
  return [2 /*return*/, res.status(201).json({ data: serialized })];
570
578
  case 19:
571
579
  error_5 = _a.sent();
572
- throw new errors_1.APIError({
573
- cause: error_5,
580
+ throw passthroughOrWrap(error_5, {
574
581
  code: "response-handler-error",
575
- detail: (0, errors_1.errorMessage)(error_5),
576
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_5),
577
582
  title: "responseHandler error",
578
583
  });
579
584
  case 20: return [2 /*return*/];
@@ -586,7 +591,7 @@ function _buildModelRouter(model, options) {
586
591
  (0, permissions_1.permissionMiddleware)(model, options),
587
592
  (0, openApi_1.listOpenApiMiddleware)(model, options),
588
593
  queryValidation,
589
- ], (0, exports.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
594
+ ], (0, exports.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
590
595
  var query, _a, _b, queryParam, _c, _d, queryParam, queryFilter, error_6, limit, builtQuery, total, populatedQuery, data, error_7, serialized, error_8, more, msg;
591
596
  var e_6, _e, e_7, _f;
592
597
  var _g, _h, _j, _k, _l, _m;
@@ -652,11 +657,8 @@ function _buildModelRouter(model, options) {
652
657
  return [3 /*break*/, 4];
653
658
  case 3:
654
659
  error_6 = _o.sent();
655
- throw new errors_1.APIError({
656
- cause: error_6,
660
+ throw passthroughOrWrap(error_6, {
657
661
  code: "query-filter-error",
658
- detail: String(error_6),
659
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_6),
660
662
  status: 400,
661
663
  title: "Query filter error",
662
664
  });
@@ -684,9 +686,11 @@ function _buildModelRouter(model, options) {
684
686
  total = _o.sent();
685
687
  if (req.query.page) {
686
688
  if (Number(req.query.page) === 0 || Number.isNaN(Number(req.query.page))) {
687
- throw new errors_1.APIError({
688
- status: 400,
689
- title: "Invalid page: ".concat(req.query.page),
689
+ throw new errors_1.BadRequestError({
690
+ code: "invalid-page",
691
+ detail: "Invalid page: ".concat(req.query.page),
692
+ source: { parameter: "page" },
693
+ title: "Invalid page",
690
694
  });
691
695
  }
692
696
  builtQuery = builtQuery.skip((Number(req.query.page) - 1) * limit);
@@ -708,11 +712,8 @@ function _buildModelRouter(model, options) {
708
712
  return [3 /*break*/, 10];
709
713
  case 9:
710
714
  error_7 = _o.sent();
711
- throw new errors_1.APIError({
712
- cause: error_7,
715
+ throw passthroughOrWrap(error_7, {
713
716
  code: "list-error",
714
- detail: (0, errors_1.errorMessage)(error_7),
715
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_7),
716
717
  title: "List error",
717
718
  });
718
719
  case 10:
@@ -723,11 +724,8 @@ function _buildModelRouter(model, options) {
723
724
  return [3 /*break*/, 13];
724
725
  case 12:
725
726
  error_8 = _o.sent();
726
- throw new errors_1.APIError({
727
- cause: error_8,
727
+ throw passthroughOrWrap(error_8, {
728
728
  code: "response-handler-error",
729
- detail: (0, errors_1.errorMessage)(error_8),
730
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_8),
731
729
  title: "responseHandler error",
732
730
  });
733
731
  case 13:
@@ -759,11 +757,8 @@ function _buildModelRouter(model, options) {
759
757
  return [2 /*return*/, res.json({ data: serialized })];
760
758
  }
761
759
  catch (error) {
762
- throw new errors_1.APIError({
763
- cause: error,
760
+ throw passthroughOrWrap(error, {
764
761
  code: "serialization-error",
765
- detail: (0, errors_1.errorMessage)(error),
766
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error),
767
762
  title: "Serialization error",
768
763
  });
769
764
  }
@@ -775,7 +770,7 @@ function _buildModelRouter(model, options) {
775
770
  (0, auth_1.authenticateMiddleware)(options.allowAnonymous),
776
771
  (0, openApi_1.getOpenApiMiddleware)(model, options),
777
772
  (0, permissions_1.permissionMiddleware)(model, options),
778
- ], (0, exports.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
773
+ ], (0, exports.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
779
774
  var data, serialized, error_9;
780
775
  return __generator(this, function (_a) {
781
776
  switch (_a.label) {
@@ -790,21 +785,20 @@ function _buildModelRouter(model, options) {
790
785
  return [2 /*return*/, res.json({ data: serialized })];
791
786
  case 3:
792
787
  error_9 = _a.sent();
793
- throw new errors_1.APIError({
794
- cause: error_9,
788
+ throw passthroughOrWrap(error_9, {
795
789
  code: "response-handler-error",
796
- detail: (0, errors_1.errorMessage)(error_9),
797
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_9),
798
790
  title: "responseHandler error",
799
791
  });
800
792
  case 4: return [2 /*return*/];
801
793
  }
802
794
  });
803
795
  }); }));
804
- router.put("/:id", (0, auth_1.authenticateMiddleware)(options.allowAnonymous), (0, exports.asyncHandler)(function (_req, _res) { return __awaiter(_this, void 0, void 0, function () {
796
+ router.put("/:id", (0, auth_1.authenticateMiddleware)(options.allowAnonymous), (0, exports.asyncHandler)(function (_req, _res) { return __awaiter(void 0, void 0, void 0, function () {
805
797
  return __generator(this, function (_a) {
806
798
  // Patch is what we want 90% of the time
807
799
  throw new errors_1.APIError({
800
+ code: "put-not-supported",
801
+ detail: "Use PATCH to update a document.",
808
802
  title: "PUT is not supported.",
809
803
  });
810
804
  });
@@ -814,7 +808,7 @@ function _buildModelRouter(model, options) {
814
808
  (0, openApi_1.patchOpenApiMiddleware)(model, options),
815
809
  (0, permissions_1.permissionMiddleware)(model, options),
816
810
  updateValidation,
817
- ], (0, exports.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
811
+ ], (0, exports.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
818
812
  var doc, body, bodyUpdatedAt, error_10, preciseUnmodifiedSince, httpUnmodifiedSince, timestampValue, httpTimestampValue, usingPreciseHeader, usingHttpHeader, clientTimestamp, docRecord, serverTimestamp, serverTimestampValue, serialized, prevDoc, error_11, populateQuery, error_12, serialized, error_13;
819
813
  var _a, _b;
820
814
  return __generator(this, function (_c) {
@@ -825,14 +819,9 @@ function _buildModelRouter(model, options) {
825
819
  body = (0, transformers_1.transform)(options, req.body, "update", req.user);
826
820
  }
827
821
  catch (error) {
828
- if ((0, errors_1.isAPIError)(error)) {
829
- throw error;
830
- }
831
- throw new errors_1.APIError({
832
- cause: error,
822
+ throw passthroughOrWrap(error, {
833
823
  code: "transform-error",
834
- detail: "PATCH failed on ".concat(req.params.id, " for user ").concat((_a = req.user) === null || _a === void 0 ? void 0 : _a.id, ": ").concat((0, errors_1.errorMessage)(error)),
835
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error),
824
+ detail: "PATCH failed on ".concat(req.params.id, " for user ").concat((_a = req.user) === null || _a === void 0 ? void 0 : _a.id, ": ").concat((0, errors_1.errorDetail)(error)),
836
825
  status: 403,
837
826
  title: "PATCH failed",
838
827
  });
@@ -852,29 +841,24 @@ function _buildModelRouter(model, options) {
852
841
  return [3 /*break*/, 4];
853
842
  case 3:
854
843
  error_10 = _c.sent();
855
- if ((0, errors_1.isAPIError)(error_10)) {
856
- throw error_10;
857
- }
858
- throw new errors_1.APIError({
859
- cause: error_10,
844
+ throw passthroughOrWrap(error_10, {
860
845
  code: "pre-update-hook-error",
861
- detail: "preUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_10)),
862
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_10),
846
+ detail: "preUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorDetail)(error_10)),
863
847
  status: 400,
864
848
  title: "preUpdate hook error",
865
849
  });
866
850
  case 4:
867
851
  if (body === undefined) {
868
- throw new errors_1.APIError({
852
+ throw new errors_1.ForbiddenError({
853
+ code: "update-not-allowed",
869
854
  detail: "A body must be returned from preUpdate",
870
- status: 403,
871
855
  title: "Update not allowed",
872
856
  });
873
857
  }
874
858
  if (body === null) {
875
- throw new errors_1.APIError({
859
+ throw new errors_1.ForbiddenError({
860
+ code: "update-not-allowed",
876
861
  detail: "preUpdate hook on ".concat(req.params.id, " returned null"),
877
- status: 403,
878
862
  title: "Update not allowed",
879
863
  });
880
864
  }
@@ -897,13 +881,13 @@ function _buildModelRouter(model, options) {
897
881
  ? luxon_1.DateTime.fromHTTP(httpTimestampValue)
898
882
  : luxon_1.DateTime.fromISO(bodyUpdatedAt);
899
883
  if (!clientTimestamp.isValid) {
900
- throw new errors_1.APIError({
884
+ throw new errors_1.BadRequestError({
885
+ code: "invalid-conflict-detection-timestamp",
901
886
  detail: usingPreciseHeader
902
887
  ? "X-Unmodified-Since-ISO header could not be parsed as an ISO date"
903
888
  : usingHttpHeader
904
889
  ? "If-Unmodified-Since header could not be parsed as an HTTP date"
905
890
  : "_updatedAt body field could not be parsed as an ISO date",
906
- status: 400,
907
891
  title: "Invalid conflict-detection timestamp",
908
892
  });
909
893
  }
@@ -917,9 +901,9 @@ function _buildModelRouter(model, options) {
917
901
  serverTimestamp = luxon_1.DateTime.fromISO(serverTimestampValue);
918
902
  }
919
903
  if (serverTimestamp && !serverTimestamp.isValid) {
920
- throw new errors_1.APIError({
904
+ throw new errors_1.BadRequestError({
905
+ code: "invalid-server-timestamp",
921
906
  detail: "Document timestamp could not be parsed as a date",
922
- status: 400,
923
907
  title: "Invalid server timestamp",
924
908
  });
925
909
  }
@@ -944,11 +928,9 @@ function _buildModelRouter(model, options) {
944
928
  return [3 /*break*/, 11];
945
929
  case 10:
946
930
  error_11 = _c.sent();
947
- throw new errors_1.APIError({
948
- cause: error_11,
931
+ throw passthroughOrWrapWrite(error_11, {
949
932
  code: "update-save-error",
950
- detail: "preUpdate hook save error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_11)),
951
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_11),
933
+ detail: "preUpdate hook save error on ".concat(req.params.id, ": ").concat((0, errors_1.errorDetail)(error_11)),
952
934
  status: 400,
953
935
  title: "preUpdate hook save error",
954
936
  });
@@ -971,11 +953,9 @@ function _buildModelRouter(model, options) {
971
953
  return [3 /*break*/, 17];
972
954
  case 16:
973
955
  error_12 = _c.sent();
974
- throw new errors_1.APIError({
975
- cause: error_12,
956
+ throw passthroughOrWrap(error_12, {
976
957
  code: "post-update-hook-error",
977
- detail: "postUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_12)),
978
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_12),
958
+ detail: "postUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorDetail)(error_12)),
979
959
  status: 400,
980
960
  title: "postUpdate hook error",
981
961
  });
@@ -987,11 +967,8 @@ function _buildModelRouter(model, options) {
987
967
  return [2 /*return*/, res.json({ data: serialized })];
988
968
  case 19:
989
969
  error_13 = _c.sent();
990
- throw new errors_1.APIError({
991
- cause: error_13,
970
+ throw passthroughOrWrap(error_13, {
992
971
  code: "response-handler-error",
993
- detail: (0, errors_1.errorMessage)(error_13),
994
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_13),
995
972
  title: "responseHandler error",
996
973
  });
997
974
  case 20: return [2 /*return*/];
@@ -1002,7 +979,7 @@ function _buildModelRouter(model, options) {
1002
979
  (0, auth_1.authenticateMiddleware)(options.allowAnonymous),
1003
980
  (0, openApi_1.deleteOpenApiMiddleware)(model, options),
1004
981
  (0, permissions_1.permissionMiddleware)(model, options),
1005
- ], (0, exports.asyncHandler)(function (req, res) { return __awaiter(_this, void 0, void 0, function () {
982
+ ], (0, exports.asyncHandler)(function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
1006
983
  var doc, body, error_14, error_15, error_16;
1007
984
  return __generator(this, function (_a) {
1008
985
  switch (_a.label) {
@@ -1019,29 +996,24 @@ function _buildModelRouter(model, options) {
1019
996
  return [3 /*break*/, 4];
1020
997
  case 3:
1021
998
  error_14 = _a.sent();
1022
- if ((0, errors_1.isAPIError)(error_14)) {
1023
- throw error_14;
1024
- }
1025
- throw new errors_1.APIError({
1026
- cause: error_14,
999
+ throw passthroughOrWrap(error_14, {
1027
1000
  code: "pre-delete-hook-error",
1028
- detail: "preDelete hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_14)),
1029
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_14),
1001
+ detail: "preDelete hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorDetail)(error_14)),
1030
1002
  status: 403,
1031
1003
  title: "preDelete hook error",
1032
1004
  });
1033
1005
  case 4:
1034
1006
  if (body === undefined) {
1035
- throw new errors_1.APIError({
1007
+ throw new errors_1.ForbiddenError({
1008
+ code: "delete-not-allowed",
1036
1009
  detail: "A body must be returned from preDelete",
1037
- status: 403,
1038
1010
  title: "Delete not allowed",
1039
1011
  });
1040
1012
  }
1041
1013
  if (body === null) {
1042
- throw new errors_1.APIError({
1014
+ throw new errors_1.ForbiddenError({
1015
+ code: "delete-not-allowed",
1043
1016
  detail: "preDelete hook for ".concat(req.params.id, " returned null"),
1044
- status: 403,
1045
1017
  title: "Delete not allowed",
1046
1018
  });
1047
1019
  }
@@ -1062,11 +1034,8 @@ function _buildModelRouter(model, options) {
1062
1034
  return [3 /*break*/, 10];
1063
1035
  case 9:
1064
1036
  error_15 = _a.sent();
1065
- throw new errors_1.APIError({
1066
- cause: error_15,
1037
+ throw passthroughOrWrap(error_15, {
1067
1038
  code: "delete-error",
1068
- detail: (0, errors_1.errorMessage)(error_15),
1069
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_15),
1070
1039
  status: 400,
1071
1040
  title: "Delete error",
1072
1041
  });
@@ -1081,11 +1050,8 @@ function _buildModelRouter(model, options) {
1081
1050
  return [3 /*break*/, 14];
1082
1051
  case 13:
1083
1052
  error_16 = _a.sent();
1084
- throw new errors_1.APIError({
1085
- cause: error_16,
1053
+ throw passthroughOrWrap(error_16, {
1086
1054
  code: "post-delete-hook-error",
1087
- detail: (0, errors_1.errorMessage)(error_16),
1088
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_16),
1089
1055
  status: 400,
1090
1056
  title: "postDelete hook error",
1091
1057
  });
@@ -1093,7 +1059,7 @@ function _buildModelRouter(model, options) {
1093
1059
  }
1094
1060
  });
1095
1061
  }); }));
1096
- var arrayOperation = function (req, res, operation) { return __awaiter(_this, void 0, void 0, function () {
1062
+ var arrayOperation = function (req, res, operation) { return __awaiter(void 0, void 0, void 0, function () {
1097
1063
  var doc, prevDoc, field, itemId, array, index, body, error_17, error_18, error_19;
1098
1064
  var _a;
1099
1065
  var _b, _c;
@@ -1104,8 +1070,11 @@ function _buildModelRouter(model, options) {
1104
1070
  // TODO Combine array operations and .patch(), as they are very similar.
1105
1071
  if (!(_d.sent())) {
1106
1072
  throw new errors_1.APIError({
1073
+ code: "array-update-not-allowed",
1074
+ detail: "Access to PATCH on ".concat(model.modelName, " denied for ").concat((_b = req.user) === null || _b === void 0 ? void 0 : _b.id),
1075
+ meta: { method: "PATCH", model: model.modelName },
1107
1076
  status: 405,
1108
- title: "Access to PATCH on ".concat(model.modelName, " denied for ").concat((_b = req.user) === null || _b === void 0 ? void 0 : _b.id),
1077
+ title: "Access denied",
1109
1078
  });
1110
1079
  }
1111
1080
  return [4 /*yield*/, model.findById(req.params.id)];
@@ -1113,17 +1082,21 @@ function _buildModelRouter(model, options) {
1113
1082
  doc = _d.sent();
1114
1083
  prevDoc = (0, cloneDeep_1.default)(doc);
1115
1084
  if (!doc) {
1116
- throw new errors_1.APIError({
1117
- status: 404,
1118
- title: "Could not find document to PATCH: ".concat(req.params.id),
1085
+ throw new errors_1.NotFoundError({
1086
+ code: "document-not-found",
1087
+ detail: "Could not find document to PATCH: ".concat(req.params.id),
1088
+ meta: { model: model.modelName },
1089
+ title: "Document not found",
1119
1090
  });
1120
1091
  }
1121
1092
  return [4 /*yield*/, (0, permissions_1.checkPermissions)("update", options.permissions.update, req.user, doc)];
1122
1093
  case 3:
1123
1094
  if (!(_d.sent())) {
1124
- throw new errors_1.APIError({
1125
- status: 403,
1126
- title: "Patch not allowed for user ".concat((_c = req.user) === null || _c === void 0 ? void 0 : _c.id, " on doc ").concat(doc._id),
1095
+ throw new errors_1.ForbiddenError({
1096
+ code: "update-not-allowed",
1097
+ detail: "Patch not allowed for user ".concat((_c = req.user) === null || _c === void 0 ? void 0 : _c.id, " on doc ").concat(doc._id),
1098
+ meta: { model: model.modelName },
1099
+ title: "Update not allowed",
1127
1100
  });
1128
1101
  }
1129
1102
  field = req.params.field;
@@ -1131,9 +1104,11 @@ function _buildModelRouter(model, options) {
1131
1104
  // We apply the operation *before* the hooks. As far as the callers are concerned, this should
1132
1105
  // be like PATCHing the field and replacing the whole thing.
1133
1106
  if (operation !== "DELETE" && req.body[field] === undefined) {
1134
- throw new errors_1.APIError({
1135
- status: 400,
1136
- title: "Malformed body, array operations should have a single, top level key, got: ".concat(Object.keys(req.body).join(",")),
1107
+ throw new errors_1.BadRequestError({
1108
+ code: "malformed-array-operation-body",
1109
+ detail: "Malformed body, array operations should have a single, top level key, got: ".concat(Object.keys(req.body).join(",")),
1110
+ meta: { field: field },
1111
+ title: "Malformed array operation body",
1137
1112
  });
1138
1113
  }
1139
1114
  array = __spreadArray([], __read(doc[field]), false);
@@ -1149,9 +1124,11 @@ function _buildModelRouter(model, options) {
1149
1124
  index = array.indexOf(itemId);
1150
1125
  }
1151
1126
  if (index === -1) {
1152
- throw new errors_1.APIError({
1153
- status: 404,
1154
- title: "Could not find ".concat(field, "/").concat(itemId),
1127
+ throw new errors_1.NotFoundError({
1128
+ code: "array-item-not-found",
1129
+ detail: "Could not find ".concat(field, "/").concat(itemId),
1130
+ meta: { field: field, itemId: itemId },
1131
+ title: "Array item not found",
1155
1132
  });
1156
1133
  }
1157
1134
  // For PATCHing an item by ID, we need to merge the objects so we don't override the _id or
@@ -1168,9 +1145,11 @@ function _buildModelRouter(model, options) {
1168
1145
  }
1169
1146
  }
1170
1147
  else {
1171
- throw new errors_1.APIError({
1172
- status: 400,
1173
- title: "Invalid array operation: ".concat(operation),
1148
+ throw new errors_1.BadRequestError({
1149
+ code: "invalid-array-operation",
1150
+ detail: "Invalid array operation: ".concat(operation),
1151
+ meta: { operation: operation },
1152
+ title: "Invalid array operation",
1174
1153
  });
1175
1154
  }
1176
1155
  body = (_a = {}, _a[field] = array, _a);
@@ -1178,14 +1157,8 @@ function _buildModelRouter(model, options) {
1178
1157
  body = (0, transformers_1.transform)(options, body, "update", req.user);
1179
1158
  }
1180
1159
  catch (error) {
1181
- if ((0, errors_1.isAPIError)(error)) {
1182
- throw error;
1183
- }
1184
- throw new errors_1.APIError({
1185
- cause: error,
1160
+ throw passthroughOrWrap(error, {
1186
1161
  code: "transform-error",
1187
- detail: (0, errors_1.errorMessage)(error),
1188
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error),
1189
1162
  status: 403,
1190
1163
  title: "Transform error",
1191
1164
  });
@@ -1200,26 +1173,24 @@ function _buildModelRouter(model, options) {
1200
1173
  return [3 /*break*/, 7];
1201
1174
  case 6:
1202
1175
  error_17 = _d.sent();
1203
- throw new errors_1.APIError({
1204
- cause: error_17,
1176
+ throw passthroughOrWrap(error_17, {
1205
1177
  code: "pre-update-hook-error",
1206
- detail: "preUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_17)),
1207
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_17),
1178
+ detail: "preUpdate hook error on ".concat(req.params.id, ": ").concat((0, errors_1.errorDetail)(error_17)),
1208
1179
  status: 400,
1209
1180
  title: "preUpdate hook error",
1210
1181
  });
1211
1182
  case 7:
1212
1183
  if (body === undefined) {
1213
- throw new errors_1.APIError({
1184
+ throw new errors_1.ForbiddenError({
1185
+ code: "update-not-allowed",
1214
1186
  detail: "A body must be returned from preUpdate",
1215
- status: 403,
1216
1187
  title: "Update not allowed",
1217
1188
  });
1218
1189
  }
1219
1190
  if (body === null) {
1220
- throw new errors_1.APIError({
1191
+ throw new errors_1.ForbiddenError({
1192
+ code: "update-not-allowed",
1221
1193
  detail: "preUpdate hook on ".concat(req.params.id, " returned null"),
1222
- status: 403,
1223
1194
  title: "Update not allowed",
1224
1195
  });
1225
1196
  }
@@ -1233,11 +1204,9 @@ function _buildModelRouter(model, options) {
1233
1204
  return [3 /*break*/, 11];
1234
1205
  case 10:
1235
1206
  error_18 = _d.sent();
1236
- throw new errors_1.APIError({
1237
- cause: error_18,
1207
+ throw passthroughOrWrapWrite(error_18, {
1238
1208
  code: "update-save-error",
1239
- detail: "PATCH Pre Update error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_18)),
1240
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_18),
1209
+ detail: "PATCH Pre Update error on ".concat(req.params.id, ": ").concat((0, errors_1.errorDetail)(error_18)),
1241
1210
  status: 400,
1242
1211
  title: "PATCH Pre Update error",
1243
1212
  });
@@ -1252,11 +1221,9 @@ function _buildModelRouter(model, options) {
1252
1221
  return [3 /*break*/, 15];
1253
1222
  case 14:
1254
1223
  error_19 = _d.sent();
1255
- throw new errors_1.APIError({
1256
- cause: error_19,
1224
+ throw passthroughOrWrap(error_19, {
1257
1225
  code: "post-update-hook-error",
1258
- detail: "PATCH Post Update error on ".concat(req.params.id, ": ").concat((0, errors_1.errorMessage)(error_19)),
1259
- disableExternalErrorTracking: (0, errors_1.getDisableExternalErrorTracking)(error_19),
1226
+ detail: "PATCH Post Update error on ".concat(req.params.id, ": ").concat((0, errors_1.errorDetail)(error_19)),
1260
1227
  status: 400,
1261
1228
  title: "PATCH Post Update error",
1262
1229
  });
@@ -1266,17 +1233,17 @@ function _buildModelRouter(model, options) {
1266
1233
  }
1267
1234
  });
1268
1235
  }); };
1269
- var arrayPost = function (req, res) { return __awaiter(_this, void 0, void 0, function () {
1236
+ var arrayPost = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
1270
1237
  return __generator(this, function (_a) {
1271
1238
  return [2 /*return*/, arrayOperation(req, res, "POST")];
1272
1239
  });
1273
1240
  }); };
1274
- var arrayPatch = function (req, res) { return __awaiter(_this, void 0, void 0, function () {
1241
+ var arrayPatch = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
1275
1242
  return __generator(this, function (_a) {
1276
1243
  return [2 /*return*/, arrayOperation(req, res, "PATCH")];
1277
1244
  });
1278
1245
  }); };
1279
- var arrayDelete = function (req, res) { return __awaiter(_this, void 0, void 0, function () {
1246
+ var arrayDelete = function (req, res) { return __awaiter(void 0, void 0, void 0, function () {
1280
1247
  return __generator(this, function (_a) {
1281
1248
  return [2 /*return*/, arrayOperation(req, res, "DELETE")];
1282
1249
  });
@@ -1289,7 +1256,7 @@ function _buildModelRouter(model, options) {
1289
1256
  }
1290
1257
  router.use(errors_1.apiErrorMiddleware);
1291
1258
  return router;
1292
- }
1259
+ };
1293
1260
  var asyncHandler = function (fn, options) {
1294
1261
  var _a, _b;
1295
1262
  // If no validation options, return simple handler