我如何抛出 Camunda 业务错误?

How do I throw Camunda business Error to out?

我关于抛出业务错误的问题。例如,我有一些图表,我从 Spring REST 控制器的方法开始流程。 我如何在 test() 方法中捕获 "Error-CheckNoneAZNOperationIsExist" 并将其抛出?

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

最后,当发生“错误结束事件”时,我想向消费者抛出一个异常,例如 JSON

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

终于找到解决方法了

1) 我向所有边界事件添加了错误代码变量(例如 globalError

2) 执行流程后我检查历史变量实例 (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) 发生错误时 globalError 由 Camunda Engine 填充 "Error Name"

Camunda diagram example

上面代码的结果

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