Is there a way to get the error code that was thrown from the service task in Camunda v 7.2?

This Service Task is a REST invoker task and we have modeled to throw many exceptions depending the different cases.

For few errors there won’t be any way to handle them in the workflow, so I don’t want to catch individual bpmn errors. Instead I want to catch them in the start event of event sub-process and log the error code (errorCode) that was thrown. Before throwing the BPMN error, I set a variable called errorCode in execution. but this variable does not retrun anything, is always empty.

Does that mean that I would not be able retrieve any execution variable outside of the task because it threw an error?

Is there a way, I can get the variable that I set in the execution before throwing BPMN error?

Appreciate your help.

Why do you want to manually set the bpmn error code variable? The config of the bpmn error catch event has a config for a “error code variable” and a “error message variable”; theses are the variables that will be created to hold the code and message values when the error is caught.

1 Like

I configured the error code and error name on the error event and not chosen any of the errors (so as to catch all the errors) in the service task’s boundary catch event. It works as expected, i.e. the error thrown from the service task is caught on the boundary, but i want to know the error code that caused this bpmn throw event, because i have not configured a particular error code on the error event(since i wanted to catch few error codes on the same boundary event)

Hope this clears my requirement.

Rather than not assign a error, what i have done in the past is:

  1. Use a generic error code that all errors throw
  2. Use the Error Message to hold the specific error information.

So all errors throw the same error code which is caught by the BPMN error event, and the error message contains the specific information about the error being thrown.

  1. But how do I retrieve the error message in the bpmn that was thrown from code i.e. service task? I see there is no way to get it.
  2. While designing, we give code and meaning full error name, but even then the error name is not retrievable while execution.

If you are throwing a “bpmn error” than when it is caught by the bpmn error event, the event will generate a process variable for the error code and error message as defined in the bpmn error event config.

PFA diagram which shows the boundary error event configuration.

Below is the source snippet. If you see the diagram, two errors were caught with boundary event and other 2 were not caught with boundary event. The uncaught exception will be propagated to parent process, if this diagram is the call-activity. if the uncaught (not with boundary) exception (ex. error.missing.config) is thrown from the service task, i want to get the error coded and log it in the sub-process shown in the diagram and pass the execution variable errorCode to parent workflows.how do i get the error code in the event-sub process ?

<bpmn2:serviceTask id="ServiceTask_1" camunda:async="true" camunda:class="com.workflows.TestAsync" name="Service Task">
  <bpmn2:extensionElements>
    <fox:failedJobRetryTimeCycle>R5/PT5S</fox:failedJobRetryTimeCycle>
  </bpmn2:extensionElements>
  <bpmn2:incoming>SequenceFlow_3</bpmn2:incoming>
  <bpmn2:incoming>SequenceFlow_6</bpmn2:incoming>
  <bpmn2:outgoing>SequenceFlow_2</bpmn2:outgoing>
</bpmn2:serviceTask>
<bpmn2:sequenceFlow id="SequenceFlow_2" name="" sourceRef="ServiceTask_1" targetRef="EndEvent_1"/>
<bpmn2:userTask id="UserTask_1" camunda:formKey="wfapp:forms/throw.xml" name="Throw?">
  <bpmn2:incoming>SequenceFlow_1</bpmn2:incoming>
  <bpmn2:outgoing>SequenceFlow_3</bpmn2:outgoing>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="SequenceFlow_3" name="" sourceRef="UserTask_1" targetRef="ServiceTask_1"/>
<bpmn2:boundaryEvent id="BoundaryEvent_1" name="" attachedToRef="ServiceTask_1">
  <bpmn2:outgoing>SequenceFlow_5</bpmn2:outgoing>
  <bpmn2:errorEventDefinition id="ErrorEventDefinition_1" errorRef="Error_1"/>
</bpmn2:boundaryEvent>
<bpmn2:boundaryEvent id="BoundaryEvent_2" name="" attachedToRef="ServiceTask_1">
  <bpmn2:outgoing>SequenceFlow_4</bpmn2:outgoing>
  <bpmn2:errorEventDefinition id="ErrorEventDefinition_2" errorRef="Error_2"/>
</bpmn2:boundaryEvent>
<bpmn2:sequenceFlow id="SequenceFlow_4" name="" sourceRef="BoundaryEvent_2" targetRef="UserTask_2"/>
<bpmn2:sequenceFlow id="SequenceFlow_5" name="" sourceRef="BoundaryEvent_1" targetRef="UserTask_2"/>
<bpmn2:userTask id="UserTask_2">
  <bpmn2:incoming>SequenceFlow_4</bpmn2:incoming>
  <bpmn2:incoming>SequenceFlow_5</bpmn2:incoming>
  <bpmn2:outgoing>SequenceFlow_6</bpmn2:outgoing>
</bpmn2:userTask>
<bpmn2:sequenceFlow id="SequenceFlow_6" name="" sourceRef="UserTask_2" targetRef="ServiceTask_1"/>
  </bpmn2:process>
  <bpmn2:error id="Error_1" errorCode="error.input.invalid" name="Invalid Input"/>
  <bpmn2:error id="Error_2" errorCode="error.auth.failure" name="Authentication failure"/>
  <bpmn2:error id="Error_3" errorCode="error.timeout" name="Timed out"/>
  <bpmn2:error id="Error_4" errorCode="error.missing.config" name="Config missing"/>

Could you please help to identify this? Since I’m using Camunda 7.2, i dont see error variable in the properties window to configure.

One more diagram that explains my requirement.

Hi @bpmlearner,

the camunda modeler supports modeling of the errorCodeVariable and the errorMessageVariable. The engine must be Camunda 7.6 or later to use this feature.

The easiest way to access the error-variables is an upgrade to Camunda 7.6.

Hope this helps, Ingo

Thanks you.

So, does that mean with Camunda 7.2 the above diagram that shows handling different error code cannot be achieved?

I’m just wondering that there is no way to get the error code in 7.2. :confused:

https://docs.camunda.org/javadoc/camunda-bpm-platform/7.6/org/camunda/bpm/engine/delegate/BpmnError.html#BpmnError(java.lang.String,%20java.lang.String)

public BpmnError(String errorCode, String message)

Use the errorCode as your generic thrown error, and the errorMessage as the specific error content you want to parse on. You can do all of this with a single bpmn error event.

edit:

and if you want to do multiple error events, here is a example using 7.6:

This is the output parameter of a service task with http-connector:

if (statusCode == 401){
   throw new org.camunda.bpm.engine.delegate.BpmnError("MailGun401", response);
} else if (statusCode == 400){
   throw new org.camunda.bpm.engine.delegate.BpmnError("MailGun400", response);
} else {
   S(response);
}

and the diagram snippet:

I get this, but how do I get the error message / error code once this error is thrown from service task and caught on the boundary event of a service task? i.e. say catch “all” errors in boundary event, and log (handle) the particular error code that was thrown.

I can model this having more than one boundary event on a service task, but i want to avoid do this. Instead catch “all” errors and use a event sub-process to handle them.

@bpmlearner when the error event is catch, the config of the event can set the process variables:

1 Like

Thank you for the screen shot. It explains my requirement.
But Is this feature (error code variable and Error message variable) supported in Camunda 7.2?

No, the code variable was introduced in Camunda BPM 7.5, the message variable in Camunda BPM 7.6.

1 Like

Thank you for the confirmation. :slight_smile:

Hello Ingo,

I use 7.9 version, but I don’t see how I can set the error message variable.

Only the errorCode is describe in the request body.
https://docs.camunda.org/manual/7.9/reference/rest/external-task/post-bpmn-error/
Also the error end event doesn’t have the message variable, but the error start event has the variable.

Any suggestions in how to set the error message variable?
I’ve tried to set errorMessage like on post-failure, but it doesn’t seem to work.
https://docs.camunda.org/manual/7.9/reference/rest/external-task/post-failure/

Best regards,
Cosmin

Hi @Cosmin,

Error message from external tasks will be available in the upcoming release: https://docs.camunda.org/manual/latest/reference/rest/external-task/post-bpmn-error/.

In Java delegates, you can set the error message throwing a new BpmnError: https://docs.camunda.org/manual/7.9/user-guide/process-engine/delegation-code/#throw-bpmn-errors-from-delegation-code and the API-Call https://docs.camunda.org/javadoc/camunda-bpm-platform/7.9/org/camunda/bpm/engine/delegate/BpmnError.html#BpmnError(java.lang.String,%20java.lang.String).

Hope this helps, Ingo

Hi Ingo

Thanks for the info.
I’ll see if I can upgrade the version.

Best regards,
Cosmin