gpt4 book ai didi

error-handling - Apache Camel 中doFinally block 的错误处理

转载 作者:行者123 更新时间:2023-12-03 08:39:31 27 4
gpt4 key购买 nike

我有一个 Camel 休息api,其中包含doTry,doCatch和doFinally块。我还必须处理doFinally块中出现的错误情况。使用doTry .. doCatch .. doFinally时,常规的Camel错误处理程序不适用,因此如何在doFinally块中处理错误情况。我在doFinally块中有一个to语句,需要处理错误,在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();

关于error-handling - Apache Camel 中doFinally block 的错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63115740/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com