Search code examples
javaspring-bootbpmncamunda

How do I throw Camunda business Error to out?


My question about throwing a business error to out. For example, I have the some diagram and I start the process from method of Spring REST Controller. How I can catch "Error-CheckNoneAZNOperationIsExist" in test() method and throw it to out?

Camunda process diagram

@RestController
public class TestEndpoint{
    @Autowired
    ProcessEngine processEngine;

    @GetMapping(path = "account-close")
    public String test(){
        ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("account_close_flow");
        return "hi";
    }
}

Finally, I want to throw an exception to the consumer, when “Error End Event” is occurred, for example as JSON

{
    “errorMessage”: “CheckNoneAZNOperationIsExist”, 
    “errorCode”: 123 
}

Solution

  • 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)

    @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";
        }
    }
    

    3) When error is occurred globalError is filled by Camunda Engine with "Error Name"

    Camunda diagram example

    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"
    }