npm package diff
Package: @forge/cache
Versions: 1.0.3-next.0 - 1.0.3-next.0-experimental-ab129b0
File: package/out/kvs/utils/error-handling.js
Index: package/out/kvs/utils/error-handling.js
===================================================================
--- package/out/kvs/utils/error-handling.js
+++ package/out/kvs/utils/error-handling.js
@@ -0,0 +1,42 @@
+"use strict";
+Object.defineProperty(exports, "__esModule", { value: true });
+exports.checkResponseError = exports.extractTraceId = exports.safeGetParsedBody = exports.isForgeError = void 0;
+const errors_1 = require("../errors");
+function isForgeError(body) {
+ return typeof body === 'object' && body !== null && 'code' in body && 'message' in body;
+}
+exports.isForgeError = isForgeError;
+function safeGetParsedBody(text) {
+ try {
+ return JSON.parse(text);
+ }
+ catch (error) {
+ return undefined;
+ }
+}
+exports.safeGetParsedBody = safeGetParsedBody;
+function extractTraceId(response) {
+ return response.headers.get('x-b3-traceid') || response.headers.get('x-trace-id');
+}
+exports.extractTraceId = extractTraceId;
+async function checkResponseError(response) {
+ if (response.ok) {
+ return;
+ }
+ const responseText = await response.text();
+ const details = {
+ status: response.status,
+ statusText: response.statusText,
+ traceId: extractTraceId(response)
+ };
+ const parsedBody = safeGetParsedBody(responseText);
+ if (parsedBody && isForgeError(parsedBody)) {
+ throw new errors_1.ForgeKvsAPIError(details, parsedBody);
+ }
+ throw new errors_1.ForgeKvsAPIError(details, {
+ code: 'UNKNOWN_ERROR',
+ message: 'Unexpected error in Forge KVS API',
+ context: { responseText }
+ });
+}
+exports.checkResponseError = checkResponseError;