gpt4 book ai didi

Apache Camel : How to get the exception message inside the doCatch() block?

转载 作者:行者123 更新时间:2023-12-04 11:26:05 25 4
gpt4 key购买 nike

我需要从抛出的异常中返回消息,或者把它放在 outmessage 中。但它不会在前端打印正确的消息。

Camel 文档建议使用 .transform(simple?...) .handled(true)但其中大部分已被弃用。

这样做的正确方法是什么?

回复:
<418 I'm a teapot,simple{${exception.message}},{}>
路线

from("direct:csv")
.doTry()
.process(doSomeThingWithTheFileProcessor)
.doCatch(Exception.class)
.process(e -> {
e.getOut().setBody(new ResponseEntity<String>(exceptionMessage().toString(), HttpStatus.I_AM_A_TEAPOT));
}).stop()
.end()
.process(finalizeTheRouteProcessor);

doSomethingWithFileProcessor
public void process(Exchange exchange) throws Exception {
String filename = exchange.getIn().getHeader("CamelFileName", String.class);

MyFile mf = repo.getFile(filename); //throws exception

exchange.getOut().setBody(exchange.getIn().getBody());
exchange.getOut().setHeader("CamelFileName", exchange.getIn().getHeader("CamelFileName"));
}

最佳答案

有很多方法可以做到这一点。所有这些都是正确的,根据错误处理的复杂程度选择您最喜欢的。我已发表 examples in this gist .在 Camel 版本中没有一个被弃用 2.22.0 .

带处理器

from("direct:withProcessor")
.doTry()
.process(new ThrowExceptionProcessor())
.doCatch(Exception.class)
.process(new Processor() {
@Override
public void process(Exchange exchange) throws Exception {
final Throwable ex = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Throwable.class);
exchange.getIn().setBody(ex.getMessage());
}
})
.end();

用简单的语言
from("direct:withSimple")
.doTry()
.process(new ThrowExceptionProcessor())
.doCatch(Exception.class)
.transform().simple("${exception.message}")
.end();

与 setBody
from("direct:withValueBuilder")
.doTry()
.process(new ThrowExceptionProcessor())
.doCatch(Exception.class)
.setBody(exceptionMessage())
.end();

关于 Apache Camel : How to get the exception message inside the doCatch() block?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53483879/

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