如何处理Micronaut中通过blockingFirst()引发的UndeliverableException错误?

时间:2019-05-02 16:36:09

标签: rx-java2 micronaut

以下是我的micronaut网络服务的摘录:

      try {
         val result = hClient.exchange(GET<String>("$readEndpoint/$token")).blockingFirst()
         logger.error("result")

        } catch (e: Exception) {
            logger.error(e.message)
        }

hClient是作为@Inject val hClient: RxHttpClient注入的响应式HTTP客户端

  

端点抛出“对等连接重置”异常。

我面临的问题

即使我将代码包装在try and catch中,也会抛出但未捕获到异常io.reactivex.exceptions.UndeliverableException

我基本上会抛出两个异常,一个异常被消息Error occurred reading HTTP response: Connection reset by peer的catch块捕获,另一个异常正在消息io.reactivex.exceptions.UndeliverableException: The exception could not be delivered to the consumer because it has already canceled/disposed the flow or the exception has nowhere to go to begin with. Further reading: https://github.com/ReactiveX/RxJava/wiki/What's-different-in-2.0#error-handling | java.nio.channels.ClosedChannelException的情况下服务

可通过以下代码复制

保持超时时间太短,以至于不会收到超时错误。

micronaut:   
    http:
        client:
            read-timeout: 1s

@Controller("/")
class TokenController(
    @Client("https://hello123456789.com/dummy") @Inject val hClient: RxHttpClient
) {

    @Get("/test")
    fun refresh(): String {

        try {
            val result = hClient.exchange(HttpRequest.GET<String>("/token/1234")).blockingFirst()
            println("result")

        } catch (e: Exception) {
            println(e.message)
        }
        return ""
    }
}

Google告诉我,我需要向rxjava添加全局onError,但找不到在Micronaut中执行该操作的方法。

感谢您的帮助。

0 个答案:

没有答案