Throw Error to out

My questions about throwing business error to out. For example, I have the diagram depicted below

This process used in SpringBoot Rest-application. Finally, I want to throw exception to the consumer, when “Error End Event” is occured.

Example:
Response json is {“error”: “CheckNoneAZNOperationIsExist”, “errorCode”:123 }

Thanks in advance

Finally, I have found solution.

  1. I added “Error Code Variable” (ex. globalError ) to all my boundary events
  2. After execution of process I check historic variable instance (Camunda Java API)
  3. When error is occurred globalError is filled by Camunda Engine with “Error Name”

Example:
BPMN


Java / SpringBoot

@RestController
public class TestEndpoint{
    @Autowired
    ProcessEngine processEngine;

    @GetMapping(path = "x")
    public String test(){
        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("account_close_flow");
        HistoricVariableInstanceEntity variable = (HistoricVariableInstanceEntity) processEngine.getHistoryService()
                .createHistoricVariableInstanceQuery()
                .processInstanceId(processInstance.getId())
                .variableName("globalError").singleResult();
        if(variable != null)
           throw new ResponseStatusException(HttpStatus.BAD_REQUEST, processInstance.getId() +" "+variable.getTextValue());

        return "hi";
    }
}

Result of code above

    {
    "timestamp": "2019-08-18T10:34:49.928+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "ce72ca30-c1a3-11e9-bb0b-0a0027000005 ErrorUserIsFrozen",
    "path": "/x"
}

Addition
Thanks to jan-galinski
Instead of querying to db every time, we can use executeWithVariablesInReturn for getting variables right after process will be ended

ProcessInstanceWithVariables processInstance = runtimeService.createProcessInstanceByKey("account_close_flow").executeWithVariablesInReturn();
String errorNameValue = processInstance.getVariables().getValue("globalError", String.class);
1 Like