paed01 / bpmn-engine

BPMN 2.0 execution engine. Open source javascript workflow engine.
MIT License
860 stars 166 forks source link

Correct way for error handling #175

Closed mrpotato3 closed 9 months ago

mrpotato3 commented 1 year ago

What is the correct way to handle errors? I have a UserTask that in the "wait" event performs asynchronous operations that can fail. If I handle the error via "new Error("Error message") the process aborts with an unhandled error exception. Good job and thank you very much!

paed01 commented 1 year ago

You should use bound error event to catch an error and take appropriate action.

mrpotato3 commented 1 year ago

Hi @paed01, thanks for responding so quickly.

This is a technical error, not business error. For example, it occurs before doing a wait on the activity when going to execute a REST service. Also, what happens (even if you put a "boundary error event") is that it breaks the process and ends execution on the server.

I perform the actions in an extension during the activity.wait event and when an error occurs I do a throw new Error('Custom error')

Example code:

activity.on('wait', async(element: any) => {

  // REST request...

  if (!restSuccess) {
    throw new Error(`No signature flow template found.`)
  }

  // DO SOMETHING
})

Error in the process:

Error: Custom error thrown.
     at handleExecuteREST
     at handleProcessActivityWait
     at Consumer.eventCallback [as onMessage]
     at Consumer.consume [as _consume]
     at Consumer.push [as _push]
     at Queue.consumeNext [as _consumeNext]
     at Queue.queueMessage
     at ExchangeBase.publishToQueue [as _publishToQueue]
     at ExchangeBase.topic [as _onTopicMessage]

Thanks!

paed01 commented 1 year ago

You could use the, unfortunately undocumented, element.fail(new Error('Custom error')) function to make the user task fail.

rolu01 commented 10 months ago

Sounds like a nice undocumented feature :) How and where can you use element.fail(new Error('Custom error')) @paed01 ?

mrpotato3 commented 9 months ago

@paed01 perfect! thanks.