Search code examples
camunda

Camunda: How to read incident stack trace with Java API


Let's say I have incident (org.camunda.bpm.engine.runtime.Incident) found using RuntimeService.createIncidentQuery()....

Is there a way to read actual incident stack trace using Java API? Same stack trace accessible in Cockpit.


Solution

  • If it is failed job then the configuration / payload of the incident will be the job id. If the incident is caused by a failed external task then it will be the external task id.

    See https://docs.camunda.org/javadoc/camunda-bpm-platform/7.15/org/camunda/bpm/engine/runtime/Incident.html

    and https://docs.camunda.org/javadoc/camunda-bpm-platform/7.15/org/camunda/bpm/engine/ManagementService.html#getJobExceptionStacktrace-java.lang.String-

    and https://docs.camunda.org/javadoc/camunda-bpm-platform/7.15/org/camunda/bpm/client/task/ExternalTask.html#getErrorDetails--

    Hence:

    Incident incident = runtimeService.createIncidentQuery().singleResult();
    String configuration = incident.getConfiguration();
    
    log.info("Incident type: {}", incident.getIncidentType());
    if (incident.getIncidentType().equals(Incident.FAILED_JOB_HANDLER_TYPE)) {
        log.info("Here comes the stacktrace: {}", managementService.getJobExceptionStacktrace(configuration));
    } else {
        log.info("Here come the error details: {}", externalTaskService.getExternalTaskErrorDetails(configuration));
    }