error-handling - apache camel 中 doFinally 块的错误处理
问题描述
我有一个骆驼休息 api,它有一个 doTry、doCatch 和 doFinally 块。我还必须处理 doFinally 块中的错误情况。当使用 doTry .. doCatch .. doFinally 时,常规的 Camel 错误处理程序不适用,因此如何处理 doFinally 块中的错误情况。我to
在 doFinally 块中有一个需要错误处理的语句,并且在 doFinally 块的处理器中有一个 if 条件需要错误处理。代码是这样的->
.post("send-req")
.route()
.doTry()
// Some Code
.doCatch()
//Some Code
.doFinally()
// Need error handling for the to statement below
.to()
.process(new Processor(){
@Override
public void process(Exchange exchange){
//Need error handling for the if statement
if(condition)
throw new BadRequestException();
}
})
.endRest();
我尝试像这样进行特定于路由的错误处理->
.post("send-req")
.route()
.doTry()
// Some Code
.doCatch()
//Some Code
.doFinally()
// Need error handling for the to statement below
.to()
.onException(SalesforceException.class)
.handled(true)
.setHeader(Exchange.HTTP_RESPONSE_CODE, new ValueBuilder(new SimpleExpression("${exception.statusCode}")))
.transform(exceptionMessage())
.end()
.process(new Processor(){
@Override
public void process(Exchange exchange){
//Need error handling for the if statement
if(condition)
throw new BadRequestException();
}
})
.onException(BadRequestException.class)
.handled(true)
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
.transform(exceptionMessage())
.end()
.endRest();
但这给出了一个错误The output must be added as top-level on the route. Try moving OnException[[org.apache.camel.component.salesforce.api.SalesforceException] -> []] to the top of route
。像这样的案件应该如何处理?
解决方案
将 onException 块移到路由之外。
它应该是这样的:
onException(SalesforceException.class)
.handled(true)
.setHeader(Exchange.HTTP_RESPONSE_CODE, new ValueBuilder(new SimpleExpression("${exception.statusCode}")))
.transform(exceptionMessage());
onException(BadRequestException.class)
.handled(true)
.setHeader(Exchange.HTTP_RESPONSE_CODE, constant(400))
.transform(exceptionMessage());
rest("/")
.post("send-req")
.route()
.someCodeThatThrowsSalesForceException
.someCodeThatThrowsBadRequestException
.endRest();
推荐阅读
- php - simplexml_load_file 未加载 XML 文件
- java - Eclipse photon错误安装过程
- python - 如何使用python的元组替换文件中的值?
- r - 处理列名中的空格(spotfire,R)
- python - 导入 matplotlib.pyplot 缩进问题
- coldfusion - 为什么我会收到我的组件未定义的 ColdFusion 错误?
- assembly - 汇编语言:字节操作和字操作的主要区别是什么?
- gcloud - 未找到 gcloud 或权限被拒绝
- java - 使用 org.json 将 XML 转换为 JSON 时出现 NoSuchMethod 错误
- android - 如何使用 ViewModel 和 Databinding 实现验证?