@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.
- package/dist/actions.test.js +2 -1
- package/dist/api.coverage.test.js +4 -2
- package/dist/api.errorPaths.test.js +5 -3
- package/dist/api.errors.test.js +6 -7
- package/dist/api.hooks.test.js +65 -8
- package/dist/api.js +93 -47
- package/dist/api.test.js +10 -4
- package/dist/auth.test.js +1 -1
- package/dist/config.js +7 -4
- package/dist/config.test.js +28 -3
- package/dist/docLoader.js +15 -51
- package/dist/docLoader.test.js +2 -1
- package/dist/errors.d.ts +72 -5
- package/dist/errors.js +265 -67
- package/dist/errors.test.js +163 -13
- package/dist/httpClient.d.ts +2 -3
- package/dist/httpClient.js +4 -4
- package/dist/httpClient.test.js +3 -2
- package/dist/notifiers/googleChatNotifier.test.js +2 -1
- package/dist/notifiers/slackNotifier.test.js +1 -1
- package/dist/notifiers/zoomNotifier.test.js +2 -1
- package/dist/permissions.js +0 -2
- package/dist/permissions.middleware.test.js +7 -42
- package/dist/plugins.test.js +4 -2
- package/dist/transformers.js +4 -2
- package/package.json +2 -2
- package/src/actions.test.ts +2 -1
- package/src/api.coverage.test.ts +4 -2
- package/src/api.errorPaths.test.ts +5 -3
- package/src/api.errors.test.ts +6 -7
- package/src/api.hooks.test.ts +24 -1
- package/src/api.test.ts +10 -4
- package/src/api.ts +93 -48
- package/src/auth.test.ts +1 -1
- package/src/config.test.ts +25 -3
- package/src/config.ts +7 -4
- package/src/docLoader.test.ts +2 -1
- package/src/docLoader.ts +16 -18
- package/src/errors.test.ts +178 -13
- package/src/errors.ts +278 -79
- package/src/httpClient.test.ts +3 -2
- package/src/httpClient.ts +4 -4
- package/src/notifiers/googleChatNotifier.test.ts +2 -2
- package/src/notifiers/slackNotifier.test.ts +2 -2
- package/src/notifiers/zoomNotifier.test.ts +2 -2
- package/src/permissions.middleware.test.ts +7 -11
- package/src/permissions.ts +0 -2
- package/src/plugins.test.ts +4 -2
- package/src/transformers.ts +4 -2
- package/tsconfig.json +1 -1
package/dist/errors.js
CHANGED
|
@@ -14,6 +14,17 @@ var __extends = (this && this.__extends) || (function () {
|
|
|
14
14
|
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
15
|
};
|
|
16
16
|
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
17
28
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
18
29
|
if (k2 === undefined) k2 = k;
|
|
19
30
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -75,16 +86,62 @@ var __read = (this && this.__read) || function (o, n) {
|
|
|
75
86
|
return ar;
|
|
76
87
|
};
|
|
77
88
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
78
|
-
exports.apiFallthroughErrorMiddleware = exports.apiErrorMiddleware = exports.mongooseErrorToAPIError = exports.apiUnauthorizedMiddleware = exports.getAPIErrorBody = exports.getDisableExternalErrorTracking = exports.errorStack = exports.errorMessage = exports.isAPIError = exports.errorsPlugin = exports.APIError = void 0;
|
|
89
|
+
exports.apiFallthroughErrorMiddleware = exports.apiErrorMiddleware = exports.mongooseErrorToAPIError = exports.apiUnauthorizedMiddleware = exports.getAPIErrorBody = exports.getDisableExternalErrorTracking = exports.errorStack = exports.errorMessage = exports.isAPIError = exports.errorsPlugin = exports.InternalServerError = exports.ValidationError = exports.ConflictError = exports.NotFoundError = exports.ForbiddenError = exports.UnauthorizedError = exports.BadRequestError = exports.APIError = void 0;
|
|
79
90
|
// https://jsonapi.org/format/#errors
|
|
80
91
|
var Sentry = __importStar(require("@sentry/bun"));
|
|
81
92
|
var mongoose_1 = __importStar(require("mongoose"));
|
|
82
93
|
var logger_1 = require("./logger");
|
|
94
|
+
// Maps common HTTP statuses to a standard Error name so external tools (Sentry, log explorers)
|
|
95
|
+
// show a meaningful error type instead of a generic "APIError".
|
|
96
|
+
var STATUS_ERROR_NAMES = {
|
|
97
|
+
400: "BadRequestError",
|
|
98
|
+
401: "UnauthorizedError",
|
|
99
|
+
403: "ForbiddenError",
|
|
100
|
+
404: "NotFoundError",
|
|
101
|
+
405: "MethodNotAllowedError",
|
|
102
|
+
409: "ConflictError",
|
|
103
|
+
410: "GoneError",
|
|
104
|
+
412: "PreconditionFailedError",
|
|
105
|
+
413: "PayloadTooLargeError",
|
|
106
|
+
415: "UnsupportedMediaTypeError",
|
|
107
|
+
422: "ValidationError",
|
|
108
|
+
429: "TooManyRequestsError",
|
|
109
|
+
500: "InternalServerError",
|
|
110
|
+
501: "NotImplementedError",
|
|
111
|
+
502: "BadGatewayError",
|
|
112
|
+
503: "ServiceUnavailableError",
|
|
113
|
+
504: "GatewayTimeoutError",
|
|
114
|
+
};
|
|
115
|
+
// Converts an application error code like "update-admin-error" into a PascalCase Error name
|
|
116
|
+
// like "UpdateAdminError" so each logical error gets its own type in external tools.
|
|
117
|
+
var errorNameFromCode = function (code) {
|
|
118
|
+
var words = code.split(/[^a-zA-Z0-9]+/).filter(Boolean);
|
|
119
|
+
if (words.length === 0) {
|
|
120
|
+
return undefined;
|
|
121
|
+
}
|
|
122
|
+
return words.map(function (word) { return word.charAt(0).toUpperCase() + word.slice(1); }).join("");
|
|
123
|
+
};
|
|
124
|
+
var normalizeStatus = function (status) {
|
|
125
|
+
if (status === undefined || status === null) {
|
|
126
|
+
return 500;
|
|
127
|
+
}
|
|
128
|
+
var truncated = Math.trunc(status);
|
|
129
|
+
if (!Number.isFinite(truncated) || truncated < 400 || truncated > 599) {
|
|
130
|
+
return 500;
|
|
131
|
+
}
|
|
132
|
+
return truncated;
|
|
133
|
+
};
|
|
83
134
|
/**
|
|
84
135
|
* APIError is a simple way to throw an error in an API route and control what is shown and the
|
|
85
136
|
* HTTP code displayed. It follows the JSONAPI spec to standardize the fields,
|
|
86
137
|
* allowing the UI to show more consistent, better error messages.
|
|
87
138
|
*
|
|
139
|
+
* It uses the standard `Error` fields the way external tools (e.g. Sentry) expect:
|
|
140
|
+
* - `message` is exactly `title` — a stable, human-readable summary of the problem type.
|
|
141
|
+
* - `name` is the error type, derived from the subclass name, `code`, or `status`
|
|
142
|
+
* (e.g. a 404 becomes "NotFoundError").
|
|
143
|
+
* - `cause` holds the wrapped original error, so linked exceptions keep their own stack.
|
|
144
|
+
*
|
|
88
145
|
* ```ts
|
|
89
146
|
* throw new APIError({
|
|
90
147
|
* title: "Only an admin can update that!",
|
|
@@ -96,46 +153,144 @@ var logger_1 = require("./logger");
|
|
|
96
153
|
*/
|
|
97
154
|
var APIError = /** @class */ (function (_super) {
|
|
98
155
|
__extends(APIError, _super);
|
|
99
|
-
function APIError(
|
|
156
|
+
function APIError(options) {
|
|
157
|
+
var _newTarget = this.constructor;
|
|
100
158
|
var _this = this;
|
|
101
|
-
var
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
_this
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
_this.
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
_this.
|
|
119
|
-
|
|
120
|
-
_this.
|
|
121
|
-
_this.
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
}
|
|
125
|
-
_this.
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (data.disableExternalErrorTracking) {
|
|
129
|
-
logger_1.logger.warn(logMessage);
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
logger_1.logger.error(logMessage);
|
|
159
|
+
var _a, _b, _c, _d;
|
|
160
|
+
var cause = (_a = options.cause) !== null && _a !== void 0 ? _a : options.error;
|
|
161
|
+
// message is exactly the stable title; detail and cause stacks are separate fields.
|
|
162
|
+
_this = _super.call(this, options.title, cause === undefined ? undefined : { cause: cause }) || this;
|
|
163
|
+
// Brand for detection across duplicate copies of @terreno/api in node_modules, where
|
|
164
|
+
// `instanceof` checks fail. See isAPIError.
|
|
165
|
+
_this.isTerrenoAPIError = true;
|
|
166
|
+
// The package compiles to ES5, where extending built-ins loses the prototype chain (the
|
|
167
|
+
// original reason isAPIError checked `name` instead of using `instanceof`). Repair it so
|
|
168
|
+
// getters, methods, and instanceof work on compiled output too.
|
|
169
|
+
Object.setPrototypeOf(_this, _newTarget.prototype);
|
|
170
|
+
_this.status = normalizeStatus(options.status);
|
|
171
|
+
_this.code = options.code;
|
|
172
|
+
// name precedence: subclass name (from new.target) > code > status-derived > "APIError".
|
|
173
|
+
var subclassName = _newTarget !== APIError && _newTarget.name && _newTarget.name !== "APIError"
|
|
174
|
+
? _newTarget.name
|
|
175
|
+
: undefined;
|
|
176
|
+
_this.name =
|
|
177
|
+
(_c = (_b = subclassName !== null && subclassName !== void 0 ? subclassName : (options.code ? errorNameFromCode(options.code) : undefined)) !== null && _b !== void 0 ? _b : STATUS_ERROR_NAMES[_this.status]) !== null && _c !== void 0 ? _c : "APIError";
|
|
178
|
+
_this.id = options.id;
|
|
179
|
+
_this.links = options.links;
|
|
180
|
+
_this.detail = options.detail;
|
|
181
|
+
_this.source = options.source;
|
|
182
|
+
_this.meta = (_d = options.meta) !== null && _d !== void 0 ? _d : {};
|
|
183
|
+
_this.disableExternalErrorTracking = options.disableExternalErrorTracking;
|
|
184
|
+
if (options.fields) {
|
|
185
|
+
_this.meta.fields = options.fields;
|
|
133
186
|
}
|
|
134
187
|
return _this;
|
|
135
188
|
}
|
|
189
|
+
Object.defineProperty(APIError.prototype, "error", {
|
|
190
|
+
/** @deprecated Use the standard `cause` field instead. */
|
|
191
|
+
get: function () {
|
|
192
|
+
return this.cause;
|
|
193
|
+
},
|
|
194
|
+
enumerable: false,
|
|
195
|
+
configurable: true
|
|
196
|
+
});
|
|
197
|
+
Object.defineProperty(APIError.prototype, "title", {
|
|
198
|
+
// JSONAPI title is the same stable summary as the standard Error message. A getter keeps the
|
|
199
|
+
// two from ever drifting.
|
|
200
|
+
get: function () {
|
|
201
|
+
return this.message;
|
|
202
|
+
},
|
|
203
|
+
enumerable: false,
|
|
204
|
+
configurable: true
|
|
205
|
+
});
|
|
206
|
+
// Client-facing JSONAPI body. Excludes name/stack/cause; includes
|
|
207
|
+
// disableExternalErrorTracking only when true (client Sentry suppression).
|
|
208
|
+
APIError.prototype.toJSON = function () {
|
|
209
|
+
return serializeAPIError(this);
|
|
210
|
+
};
|
|
136
211
|
return APIError;
|
|
137
212
|
}(Error));
|
|
138
213
|
exports.APIError = APIError;
|
|
214
|
+
var withStatus = function (options, status) {
|
|
215
|
+
return typeof options === "string" ? { status: status, title: options } : __assign(__assign({}, options), { status: status });
|
|
216
|
+
};
|
|
217
|
+
/** 400 Bad Request. */
|
|
218
|
+
var BadRequestError = /** @class */ (function (_super) {
|
|
219
|
+
__extends(BadRequestError, _super);
|
|
220
|
+
function BadRequestError(options) {
|
|
221
|
+
var _this = _super.call(this, withStatus(options, 400)) || this;
|
|
222
|
+
_this.name = "BadRequestError";
|
|
223
|
+
return _this;
|
|
224
|
+
}
|
|
225
|
+
return BadRequestError;
|
|
226
|
+
}(APIError));
|
|
227
|
+
exports.BadRequestError = BadRequestError;
|
|
228
|
+
/** 401 Unauthorized. */
|
|
229
|
+
var UnauthorizedError = /** @class */ (function (_super) {
|
|
230
|
+
__extends(UnauthorizedError, _super);
|
|
231
|
+
function UnauthorizedError(options) {
|
|
232
|
+
var _this = _super.call(this, withStatus(options, 401)) || this;
|
|
233
|
+
_this.name = "UnauthorizedError";
|
|
234
|
+
return _this;
|
|
235
|
+
}
|
|
236
|
+
return UnauthorizedError;
|
|
237
|
+
}(APIError));
|
|
238
|
+
exports.UnauthorizedError = UnauthorizedError;
|
|
239
|
+
/** 403 Forbidden. */
|
|
240
|
+
var ForbiddenError = /** @class */ (function (_super) {
|
|
241
|
+
__extends(ForbiddenError, _super);
|
|
242
|
+
function ForbiddenError(options) {
|
|
243
|
+
var _this = _super.call(this, withStatus(options, 403)) || this;
|
|
244
|
+
_this.name = "ForbiddenError";
|
|
245
|
+
return _this;
|
|
246
|
+
}
|
|
247
|
+
return ForbiddenError;
|
|
248
|
+
}(APIError));
|
|
249
|
+
exports.ForbiddenError = ForbiddenError;
|
|
250
|
+
/** 404 Not Found. */
|
|
251
|
+
var NotFoundError = /** @class */ (function (_super) {
|
|
252
|
+
__extends(NotFoundError, _super);
|
|
253
|
+
function NotFoundError(options) {
|
|
254
|
+
var _this = _super.call(this, withStatus(options, 404)) || this;
|
|
255
|
+
_this.name = "NotFoundError";
|
|
256
|
+
return _this;
|
|
257
|
+
}
|
|
258
|
+
return NotFoundError;
|
|
259
|
+
}(APIError));
|
|
260
|
+
exports.NotFoundError = NotFoundError;
|
|
261
|
+
/** 409 Conflict. */
|
|
262
|
+
var ConflictError = /** @class */ (function (_super) {
|
|
263
|
+
__extends(ConflictError, _super);
|
|
264
|
+
function ConflictError(options) {
|
|
265
|
+
var _this = _super.call(this, withStatus(options, 409)) || this;
|
|
266
|
+
_this.name = "ConflictError";
|
|
267
|
+
return _this;
|
|
268
|
+
}
|
|
269
|
+
return ConflictError;
|
|
270
|
+
}(APIError));
|
|
271
|
+
exports.ConflictError = ConflictError;
|
|
272
|
+
/** 400 Bad Request with a "ValidationError" type, for request/schema validation failures. */
|
|
273
|
+
var ValidationError = /** @class */ (function (_super) {
|
|
274
|
+
__extends(ValidationError, _super);
|
|
275
|
+
function ValidationError(options) {
|
|
276
|
+
var _this = _super.call(this, withStatus(options, 400)) || this;
|
|
277
|
+
_this.name = "ValidationError";
|
|
278
|
+
return _this;
|
|
279
|
+
}
|
|
280
|
+
return ValidationError;
|
|
281
|
+
}(APIError));
|
|
282
|
+
exports.ValidationError = ValidationError;
|
|
283
|
+
/** 500 Internal Server Error. */
|
|
284
|
+
var InternalServerError = /** @class */ (function (_super) {
|
|
285
|
+
__extends(InternalServerError, _super);
|
|
286
|
+
function InternalServerError(options) {
|
|
287
|
+
var _this = _super.call(this, withStatus(options, 500)) || this;
|
|
288
|
+
_this.name = "InternalServerError";
|
|
289
|
+
return _this;
|
|
290
|
+
}
|
|
291
|
+
return InternalServerError;
|
|
292
|
+
}(APIError));
|
|
293
|
+
exports.InternalServerError = InternalServerError;
|
|
139
294
|
// This can be attached to any schema to store errors compatible with the JSONAPI spec.
|
|
140
295
|
// Lazily initialize to avoid module loading order issues with Bun where mongoose
|
|
141
296
|
// may not be fully initialized when this module loads.
|
|
@@ -169,7 +324,16 @@ var errorsPlugin = function (schema) {
|
|
|
169
324
|
};
|
|
170
325
|
exports.errorsPlugin = errorsPlugin;
|
|
171
326
|
var isAPIError = function (error) {
|
|
172
|
-
|
|
327
|
+
if (error instanceof APIError) {
|
|
328
|
+
return true;
|
|
329
|
+
}
|
|
330
|
+
if (error instanceof Error) {
|
|
331
|
+
// Brand check survives duplicate copies of @terreno/api in node_modules; the name check is a
|
|
332
|
+
// transition fallback for instances from older versions of the package.
|
|
333
|
+
return (error.isTerrenoAPIError === true ||
|
|
334
|
+
error.name === "APIError");
|
|
335
|
+
}
|
|
336
|
+
return false;
|
|
173
337
|
};
|
|
174
338
|
exports.isAPIError = isAPIError;
|
|
175
339
|
/** Extract a human-readable message from an unknown error. */
|
|
@@ -205,27 +369,32 @@ var getDisableExternalErrorTracking = function (error) {
|
|
|
205
369
|
return undefined;
|
|
206
370
|
};
|
|
207
371
|
exports.getDisableExternalErrorTracking = getDisableExternalErrorTracking;
|
|
208
|
-
//
|
|
209
|
-
//
|
|
210
|
-
|
|
211
|
-
var getAPIErrorBody = function (error) {
|
|
372
|
+
// Builds the client-facing JSONAPI body from any APIError-shaped error, including instances from
|
|
373
|
+
// older/duplicate copies of @terreno/api that may not have toJSON.
|
|
374
|
+
var serializeAPIError = function (error) {
|
|
212
375
|
var e_1, _a;
|
|
213
|
-
var errorData = { status: error.status, title: error.title };
|
|
214
376
|
var indexable = error;
|
|
377
|
+
// Prefer an explicit title property for legacy/duplicate-package instances whose message was
|
|
378
|
+
// polluted with detail/cause stacks but still carry a clean title field.
|
|
379
|
+
var title = typeof indexable.title === "string" && indexable.title.length > 0
|
|
380
|
+
? indexable.title
|
|
381
|
+
: error.message;
|
|
382
|
+
var body = {
|
|
383
|
+
status: typeof indexable.status === "number" ? indexable.status : 500,
|
|
384
|
+
title: title,
|
|
385
|
+
};
|
|
215
386
|
try {
|
|
216
|
-
for (var _b = __values([
|
|
217
|
-
"id",
|
|
218
|
-
"links",
|
|
219
|
-
"status",
|
|
220
|
-
"code",
|
|
221
|
-
"detail",
|
|
222
|
-
"source",
|
|
223
|
-
"meta",
|
|
224
|
-
"disableExternalErrorTracking",
|
|
225
|
-
]), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
387
|
+
for (var _b = __values(["id", "links", "code", "detail", "source", "meta"]), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
226
388
|
var key = _c.value;
|
|
227
|
-
|
|
228
|
-
|
|
389
|
+
var value = indexable[key];
|
|
390
|
+
if (key === "meta" &&
|
|
391
|
+
value &&
|
|
392
|
+
typeof value === "object" &&
|
|
393
|
+
Object.keys(value).length === 0) {
|
|
394
|
+
continue;
|
|
395
|
+
}
|
|
396
|
+
if (value) {
|
|
397
|
+
body[key] = value;
|
|
229
398
|
}
|
|
230
399
|
}
|
|
231
400
|
}
|
|
@@ -236,7 +405,17 @@ var getAPIErrorBody = function (error) {
|
|
|
236
405
|
}
|
|
237
406
|
finally { if (e_1) throw e_1.error; }
|
|
238
407
|
}
|
|
239
|
-
|
|
408
|
+
if (indexable.disableExternalErrorTracking === true) {
|
|
409
|
+
body.disableExternalErrorTracking = true;
|
|
410
|
+
}
|
|
411
|
+
return body;
|
|
412
|
+
};
|
|
413
|
+
/**
|
|
414
|
+
* Creates an APIError body to send to clients as JSON.
|
|
415
|
+
* @deprecated Use `error.toJSON()` instead.
|
|
416
|
+
*/
|
|
417
|
+
var getAPIErrorBody = function (error) {
|
|
418
|
+
return serializeAPIError(error);
|
|
240
419
|
};
|
|
241
420
|
exports.getAPIErrorBody = getAPIErrorBody;
|
|
242
421
|
var apiUnauthorizedMiddleware = function (err, _req, res, next) {
|
|
@@ -270,43 +449,62 @@ var mongooseErrorToAPIError = function (err) {
|
|
|
270
449
|
}
|
|
271
450
|
finally { if (e_2) throw e_2.error; }
|
|
272
451
|
}
|
|
273
|
-
return new
|
|
452
|
+
return new ValidationError({
|
|
453
|
+
cause: err,
|
|
274
454
|
detail: err.message,
|
|
275
455
|
disableExternalErrorTracking: true,
|
|
276
456
|
fields: fields,
|
|
277
|
-
status: 400,
|
|
278
457
|
title: "Validation failed",
|
|
279
458
|
});
|
|
280
459
|
}
|
|
281
460
|
if (err instanceof mongoose_1.default.Error.CastError) {
|
|
282
461
|
var path = (_c = err.path) !== null && _c !== void 0 ? _c : "field";
|
|
283
|
-
return new
|
|
462
|
+
return new ValidationError({
|
|
463
|
+
cause: err,
|
|
284
464
|
detail: "Invalid value for ".concat(path),
|
|
285
465
|
disableExternalErrorTracking: true,
|
|
286
466
|
fields: (_b = {},
|
|
287
467
|
_b[path] = "Expected ".concat((_d = err.kind) !== null && _d !== void 0 ? _d : "a valid value", ", got ").concat(JSON.stringify(err.value)),
|
|
288
468
|
_b),
|
|
289
|
-
status: 400,
|
|
290
469
|
title: "Validation failed",
|
|
291
470
|
});
|
|
292
471
|
}
|
|
293
472
|
return null;
|
|
294
473
|
};
|
|
295
474
|
exports.mongooseErrorToAPIError = mongooseErrorToAPIError;
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
475
|
+
// Logs the error (warn for 4xx client errors, error for 5xx server errors) and captures it with
|
|
476
|
+
// Sentry unless external tracking is disabled. Fingerprints on the logical error type
|
|
477
|
+
// (name + code/title + status) so unrelated errors constructed at the same framework lines don't
|
|
478
|
+
// group together, while the `cause` chain preserves each occurrence's real origin stack.
|
|
479
|
+
var logAndCaptureAPIError = function (err) {
|
|
480
|
+
var cause = err.cause;
|
|
481
|
+
var causeStack = cause instanceof Error && cause.stack ? "\n".concat(cause.stack) : "";
|
|
482
|
+
var logMessage = "".concat(err.name, "(").concat(err.status, "): ").concat(err.message).concat(err.detail ? " \u2014 ".concat(err.detail) : "").concat(causeStack);
|
|
483
|
+
var logFn = err.status >= 500 ? logger_1.logger.error.bind(logger_1.logger) : logger_1.logger.warn.bind(logger_1.logger);
|
|
484
|
+
if (err.disableExternalErrorTracking) {
|
|
485
|
+
logFn(logMessage);
|
|
302
486
|
return;
|
|
303
487
|
}
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
488
|
+
logFn(logMessage);
|
|
489
|
+
Sentry.withScope(function (scope) {
|
|
490
|
+
var _a;
|
|
491
|
+
scope.setFingerprint([err.name, (_a = err.code) !== null && _a !== void 0 ? _a : err.name, String(err.status)]);
|
|
492
|
+
scope.setTag("http.status_code", String(err.status));
|
|
493
|
+
if (err.code) {
|
|
494
|
+
scope.setTag("api_error.code", err.code);
|
|
495
|
+
}
|
|
496
|
+
scope.setContext("apiError", serializeAPIError(err));
|
|
497
|
+
Sentry.captureException(err);
|
|
498
|
+
});
|
|
499
|
+
};
|
|
500
|
+
var apiErrorMiddleware = function (err, _req, res, next) {
|
|
501
|
+
var apiError = (0, exports.isAPIError)(err) ? err : (0, exports.mongooseErrorToAPIError)(err);
|
|
502
|
+
if (!apiError) {
|
|
503
|
+
next(err);
|
|
307
504
|
return;
|
|
308
505
|
}
|
|
309
|
-
|
|
506
|
+
logAndCaptureAPIError(apiError);
|
|
507
|
+
res.status(apiError.status).json(serializeAPIError(apiError)).send();
|
|
310
508
|
};
|
|
311
509
|
exports.apiErrorMiddleware = apiErrorMiddleware;
|
|
312
510
|
/**
|