UNPKG

@5minds/processcube_engine

Version:

The ProcessCube Engine. Stores and executes BPMNs.

134 lines 6.46 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ErrorBoundaryEventHandler = void 0; const inversify_1 = require("inversify"); const processcube_engine_sdk_1 = require("@5minds/processcube_engine_sdk"); const index_1 = require("../../../Tools/DatabaseAdaptersSequelize/index"); const EventMiddlewareHandler_1 = require("../../../Tools/EventMiddlewareHandler"); const ProcessInstance_1 = require("../../ProcessInstance"); const FlowNodeHandlerFactory_1 = require("../FlowNodeHandlerFactory"); const BoundaryEventHandler_1 = require("./BoundaryEventHandler"); let ErrorBoundaryEventHandler = class ErrorBoundaryEventHandler extends BoundaryEventHandler_1.BoundaryEventHandler { triggerCallback; constructor(eventMiddlewareHandler, flowNodeInstanceDatabaseAdapter, flowNodeHandlerFactory, boundaryEventModel, processInstance, processTokenOfAttachedFlowNode) { super(eventMiddlewareHandler, flowNodeInstanceDatabaseAdapter, flowNodeHandlerFactory, boundaryEventModel, processInstance, processTokenOfAttachedFlowNode, 'error_boundary_event_handler'); } /** * Checks if the code of the given error is equal to the one attached to the BoundaryEvent model. * * @param error The error to compare against the errorEventDefinition of the model. * @returns True, if the BoundaryEvent can handle the given error. * Otherwise false. */ canHandleError(error) { const boundaryEventCanHandleError = !this.boundaryEventModel.errorEventDefinition || this.checkIfErrorMatches(error); return boundaryEventCanHandleError; } async trigger(error) { if (!this.canHandleError(error) || this.eventWasTriggered) { return; } if (this.triggerCallback) { this.triggerCallback(); } else { this.eventWasTriggered = true; } this.processToken = { name: error.name, code: error.code, message: error.message, stack: error.stack, ...error, }; await this.runPostScript(); await this.saveTokenInDataObjects(this.processToken, this.boundaryEventModel.id); return super.finish(); } async execute(onTriggeredCallback, attachedFlowNodeInstanceId, decoratedFlowNodeRejectFunc) { try { this.logger.debug(`Initializing Error BoundaryEvent on FlowNodeInstance ${attachedFlowNodeInstanceId}`); await this.initialize(attachedFlowNodeInstanceId); await this.waitForError(); } catch (error) { if (this.executionPromise.controller.signal.aborted) { return; } await this.persistOnError(error); decoratedFlowNodeRejectFunc(error); return; } return this.handleNextFlowNodes(); } async resume(boundaryEventInstance, onTriggeredCallback, attachedFlowNodeInstanceId, decoratedFlowNodeRejectFunc, flowNodeInstances) { this.boundaryEventInstance = boundaryEventInstance; if (this.boundaryEventInstance?.state === processcube_engine_sdk_1.FlowNodeInstanceState.canceled) { return; } this.eventWasTriggered = this.boundaryEventWasTriggered(flowNodeInstances); if (this.eventWasTriggered) { return this.handleNextFlowNodes(flowNodeInstances); } try { await this.initialize(attachedFlowNodeInstanceId, boundaryEventInstance); await this.waitForError(); } catch (error) { if (this.executionPromise.controller.signal.aborted) { return; } await this.persistOnError(error); decoratedFlowNodeRejectFunc(error); return; } return this.handleNextFlowNodes(flowNodeInstances); } waitForError() { if (this.eventWasTriggered) { return; } this.createExecutionPromise((resolve) => { this.triggerCallback = () => { if (!this.executionPromise.isPending) { return; } this.eventWasTriggered = true; resolve(); }; }); return this.executionPromise.promise; } checkIfErrorMatches(error) { const errorDefinition = this.boundaryEventModel.errorEventDefinition; const errorDefinitionHasNoCode = !errorDefinition.code || errorDefinition.code === ''; const codeMatches = errorDefinitionHasNoCode || `${errorDefinition.code}` === `${error.code}`; const errorDefinitionHasNoMessage = !errorDefinition.message || errorDefinition.message === ''; if (errorDefinitionHasNoMessage) { return codeMatches; } const errorHasPrefix = error.message.startsWith(`Failure in `); if (errorHasPrefix) { const message = error.message.substring(error.message.indexOf(':') + 1).trim(); return codeMatches && message === errorDefinition.message; } return codeMatches && errorDefinition.message === error.message; } }; exports.ErrorBoundaryEventHandler = ErrorBoundaryEventHandler; exports.ErrorBoundaryEventHandler = ErrorBoundaryEventHandler = __decorate([ (0, inversify_1.injectable)(), __metadata("design:paramtypes", [EventMiddlewareHandler_1.EventMiddlewareHandler, index_1.FlowNodeInstanceDatabaseAdapter, FlowNodeHandlerFactory_1.FlowNodeHandlerFactory, Object, ProcessInstance_1.ProcessInstance, Object]) ], ErrorBoundaryEventHandler); //# sourceMappingURL=ErrorBoundaryEventHandler.js.map