How to handle orphand "bpmn:Error" objects?

Hello,

currently I’am working on a new extension of our BPMN Modeler.
We are creating an “bpmn:Error” when the user enters an error message on an error end event.

image

After removing and adding an event definition on one or more end events. The bpmn file collects all “bpmn:Error” objects. But some of them are no longer referenced. So they are orphaned.

I tried to use the eventBus to catch the information which event definition is deleted and which errorRef is effected. But no change event seams to store the event definition while deleting.

e.g.
image
image

This event contains only Ids for the changed property. But the Id of the oldProperties does not exist in my process.

Do you have any idea?

What is the proposed way to handle such orphaned “bpmn:Error” objects?
Such I simple ignore them?

Many thanks in advanced.
Best regards, Anja

Can you maybe share a codesandbox with your custom extensions? Without it, it will be hard to check which events are fired when and where your behavior should hook into.

Hi Niklas,

I setup an example via coding

1 step → switch an end event to an error end event with error definition and bpmn:Error
2 step → switch back to an error end event. In the coding I just remove the event definition, because if you perform it via contextPad it happen in the same way.
Afterwards the bpmn:Error is still available in the rootElements.

Normaly the user removes an error end event or switch them back to an end event (via contextPad) and then the bpmn:Error stays available in the file. How can I avoid this?

My assumption is that I can catch an event (via eventBus) that the event definition is removed, which includes all data of the event definition, espacially errorRef. So that I can delete the bpmn:Error also.

What do you think?

Best Regards,
Anja

1 Like

My assumption is that I can catch an event (via eventBus) that the event definition is removed, which includes all data of the event definition, espacially errorRef. So that I can delete the bpmn:Error also.

Thanks for sharing!

That’s exactly how I would do it, so something like that

import { is } from "bpmn-js/lib/util/ModelUtil";

import {
  remove as collectionRemove
} from 'diagram-js/lib/util/Collections';

bpmnJS.on("commandStack.element.updateProperties.postExecute", function (
  event
) {
  var context = event.context,
    oldProperties = context.oldProperties,
    eventDefinitions = oldProperties.eventDefinitions;

  // (1) get all removed error event definitions
  var errorEventDefinitions = (eventDefinitions || []).filter((p) =>
    is(p, "bpmn:ErrorEventDefinition")
  );

  // (2) get errors from the definitions
  var removedErrors = errorEventDefinitions.map(function (eventDefinition) {
    return eventDefinition.errorRef;
  });

  var definitions = bpmnJS.getDefinitions();

  // (3) remove error from diagram
  removedErrors.forEach(function(error) {
    collectionRemove(definitions.get('rootElements'), error);
  });
});

Please note that this only works under the assumption there is a 1:1 relation between error event definition & error in your diagrams. If not (and the BPMN 2.0 spec allows that) you will have to check whether the error is referenced in any other event beforehand.

Hi Niklas,

thank you very much for your example.

But there is one mismatch … if I catch this event after removing the eventDefinition (via contextPad. The context of oldProperties contains only the event id and not information about the eventDefinition.

image

The eventBus does not detect the deletion of the eventDefiniton. You can try it manually in the sandbox. You will catch the event 2 times … first if you add the eventDefintion (switch to an error end event) and second if you remove the eventDefinition (switch back to an end event).

Best Regards,
Anja