UNPKG

@cognigy/rest-api-client

Version:

Cognigy REST-Client

64 lines 2.79 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.handleServiceError = exports.isInternalServerError = void 0; const errors_1 = require("../../../../../errors"); const logic_1 = require("../../../logic"); const isInternalServerError = (error) => { var _a; return !!((_a = error === null || error === void 0 ? void 0 : error.originalErrorDetails) === null || _a === void 0 ? void 0 : _a.code); }; exports.isInternalServerError = isInternalServerError; const handleServiceError = ({ api, cognigy, nodeId, traceId, searchStoreLocation, searchStoreLocationContextKey, searchStoreLocationInputKey, errorHandling, errorMessage, errorHandlingGotoTarget, }) => async (error, knowledgeSearchResponseData) => { var _a, _b; const compactError = { message: (error === null || error === void 0 ? void 0 : error.message) || error, }; if ((0, exports.isInternalServerError)(error)) { compactError["code"] = error.originalErrorDetails.code; } let searchStoreDataWithError = { error: compactError, }; if (knowledgeSearchResponseData) { searchStoreDataWithError = Object.assign({ error: compactError }, knowledgeSearchResponseData); } // add error to context or input switch (searchStoreLocation) { case "context": // @ts-ignore api.addToContext(searchStoreLocationContextKey, searchStoreDataWithError, "simple"); break; default: api.addToInput(searchStoreLocationInputKey, searchStoreDataWithError); } if (errorHandling === "continue") { // output the timeout message if (errorMessage) { await ((_a = api.output) === null || _a === void 0 ? void 0 : _a.call(api, errorMessage, null)); } } else if (errorHandling === "goto") { if (!errorHandlingGotoTarget) { throw new Error("GoTo Target is required"); } const gotoParams = { cognigy, childConfigs: [], nodeId, config: { flowNode: { flow: errorHandlingGotoTarget.flow, node: errorHandlingGotoTarget.node, }, injectedText: undefined, injectedData: undefined, executionMode: "continue", absorbContext: false, }, }; await ((_b = logic_1.GO_TO.function) === null || _b === void 0 ? void 0 : _b.call(logic_1.GO_TO, gotoParams)); } else { throw new errors_1.InternalServerError(error === null || error === void 0 ? void 0 : error.message, { traceId }); } }; exports.handleServiceError = handleServiceError; //# sourceMappingURL=errorHandler.helper.js.map