How to use custom error channel to produce custom error response?

﹥>﹥吖頭↗ 提交于 2021-01-29 16:22:51

问题


I am trying to build a simple API using Spring Integration. For any exceptions or errors thrown in the API, I want to provide custom error responses to client. I am trying with the flow structure below:

@Bean
public IntegrationFlow mainFlow() {
    return IntegrationFlows.from(WebFlux.inboundGateway(path)
            .errorChannel(customErrorChannel())
        )
        .enrichHeaders(h -> h.header(MessageHeaders.ERROR_CHANNEL, "customErrorChannel"))
        .transform(p -> {
            if(<some validation fails>)
                throw new RuntimeException("Error!");
            return "Ok Response";
        })
        .get();
}

@Bean
public PublishSubscribeChannel customErrorChannel() {
    return MessageChannels.publishSubscribe().get();
}

@Bean
public IntegrationFlow errorFlow() {
    return IntegrationFlows.from("customErrorChannel")
        .transform(p -> {
            return "Error Response";
        })
        .get();
}

For success cases, "Ok Response" is provided to the client. But when exception is thrown in transformer, provided response is default error response from the framework with stack trace. How can I use errorFlow to produce final response for exceptions?

I can use CustomErrorWebExceptionHandler as global exception handler, but that is not my intention.


回答1:


It turns out that the .errorChannel(customErrorChannel()) is out of use in case of reactive channel adapter like that WebFlux.inboundGateway(). Please, raise a GH issue and we'll think what and how we can do.

As a workaround I suggest you to take a look into an ExpressionEvaluatingRequestHandlerAdvice for that transform(): https://docs.spring.io/spring-integration/docs/current/reference/html/messaging-endpoints.html#message-handler-advice-chain.

This way you'll catch all the errors in the transformer and can send them to your customErrorChannel for handling.

There is no reason in the .enrichHeaders(h -> h.header(MessageHeaders.ERROR_CHANNEL, "customErrorChannel")) though. The .errorChannel(customErrorChannel()) should be enough. But when we have a fix already.

UPDATE

Turns out as a workaround you can add .channel(MessageChannels.flux()) before your transform() and the error should be process through your errorChannel.



来源:https://stackoverflow.com/questions/61718691/how-to-use-custom-error-channel-to-produce-custom-error-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!