【问题标题】:Apache Camel: How to get the exception message inside the doCatch() block?Apache Camel:如何在 doCatch() 块中获取异常消息?
【发布时间】:2018-11-26 15:01:32
【问题描述】:

我需要从抛出的异常中返回消息,或者将其放入 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"));
    }

【问题讨论】:

    标签: apache spring-boot apache-camel dsl


    【解决方案1】:

    有很多方法可以做到这一点。所有这些都是正确的,根据错误处理的复杂性选择你喜欢的。我已经发布了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();
    

    【讨论】:

    • ThrowExceptionProcessor 在构造函数中需要异常...
    • @ArtemPtushkin 不,它没有。 ThrowExceptionProcessor 是自定义处理器,而不是 Apache Camel 中提供的处理器。如果您再次阅读答案,您将看到指向要点的链接...gist.github.com/bedlaj/…
    【解决方案2】:

    在 doCatch() 中,Camel 将异常移动到带有键 Exchange.EXCEPTION_CAUGHT (http://camel.apache.org/why-is-the-exception-null-when-i-use-onexception.html) 的交换属性中。

    所以你可以使用

    e.getOut().setBody(new ResponseEntity<String>(e.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class).getMessage(), HttpStatus.OK));
    

    【讨论】:

    • +1 的答案,但我选择了@Bedla 的答案,因为它更完整,并且提供了多种解决问题的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2023-01-20
    • 1970-01-01
    • 2011-06-08
    • 1970-01-01
    • 2018-12-16
    相关资源
    最近更新 更多