How to extend default incident handler

Hello,

We are trying to extend the default incident behavior in Camunda. For business errors we have modeled out the subprocess but for technical errors (connectivity issues for example) we want to wait and automatically retry x number of times. If the call still fails we want the incident to be created and an email to be sent to a support team informing them of where the issue has taken place.

I have the following class

import org.camunda.bpm.engine.ProcessEngine
import org.camunda.bpm.engine.ProcessEngines
import org.camunda.bpm.engine.impl.incident.DefaultIncidentHandler
import org.camunda.bpm.engine.impl.incident.IncidentContext
import org.camunda.bpm.engine.runtime.Incident

class TestIncidentHandler extends DefaultIncidentHandler {

    /**
     * The type of incident handler this is.
     */
    final static String INCIDENT_HANDLER_TYPE = "failedJob"

    /**
     * Default Constructor to inject beans.
     *
     */
    TestIncidentHandler() {
        super(INCIDENT_HANDLER_TYPE)
    }

    @Override
    Incident handleIncident(IncidentContext context, String message) {

        // Do what the default method does
        Incident incident = super.handleIncident(context, message)

        // Send a message event to the required subprocess
        println("Send email.")

        // Return
        return incident
    }
}

Here, every time an unexpected error occurs in my code I want to send an email with a specific message depending on where the error has occurred. How do I pass my specific error message to this handler?
Also, would I be able to access the ProcessEngine or the RuntimeService in the incident handler? When I try to inject them I get a circular dependency problem.

I want to be able to kick off a sub process from the incident handler so that the same code to send email can be reused. We can also extract that into an external class and call it from both the delegate in the subprocess and the incident handler but I want to see if this is possible.

Also, is there a way to add a wait between the default retries before the incident is created? I tried using the setFailedJobRetryTimeCycle property to R3/P1M but it does not make any difference. The delegate code gets retried without a gap of 1 minute.

I am also looking for general guidance on how to handle technical errors. Is there a way to implement default behavior for uncaught exceptions without using an IncidentHandler?

Thank you

2 Likes