@cognigy/rest-api-client
Version:
Cognigy REST-Client
60 lines • 2.6 kB
JavaScript
import { __awaiter } from "tslib";
import { InternalServerError } from "../../../../../errors";
import { GO_TO } from "../../../logic";
export const isInternalServerError = (error) => { var _a; return !!((_a = error === null || error === void 0 ? void 0 : error.originalErrorDetails) === null || _a === void 0 ? void 0 : _a.code); };
export const handleServiceError = ({ api, cognigy, nodeId, traceId, searchStoreLocation, searchStoreLocationContextKey, searchStoreLocationInputKey, errorHandling, errorMessage, errorHandlingGotoTarget, }) => (error, knowledgeSearchResponseData) => __awaiter(void 0, void 0, void 0, function* () {
var _a, _b;
const compactError = {
message: (error === null || error === void 0 ? void 0 : error.message) || error,
};
if (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) {
yield ((_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,
},
};
yield ((_b = GO_TO.function) === null || _b === void 0 ? void 0 : _b.call(GO_TO, gotoParams));
}
else {
throw new InternalServerError(error === null || error === void 0 ? void 0 : error.message, { traceId });
}
});
//# sourceMappingURL=errorHandler.helper.js.map