Instrumented Java 11 HttpClient does not re-throw exceptions in sendAsync call #5136
Labels
bug
A general bug
instrumentation
An issue that is related to instrumenting a component
module: micrometer-java11
Milestone
Describe the bug
After instrumenting our
java.net.HttpClient
with newly releasedMicrometerHttpClient
decorator, we've noticed our tests started failing. It seems that if any exception (e.g. network error) occurs, it will be caught inMicrometerHttpClient
and not propagated further to the nextCompletionStage
available to the user, contrary to what happens in the originalHttpClient
implementation.Implementation of
sendAsync
decorator inMicrometerHttpClient
does not re-throw exception in theCompletableFuture.handle()
method, but returns onlyresponse
. In casehandle
got an exception as its input,null
result will be returned instead:Environment
To Reproduce
The easiest way to reproduce the bug is to write a test simulating a faulty HTTP server, for example using
WireMock
library. I've attached a minimal demo application (built with JDK 17 and Gradle) with a failing test.Here I show two tests, first passing and second failing, highlighting the reported issue:
Expected behavior
Underlying error should be re-thrown and
CompletionStage
returned to the user should be completed exceptionally too, as per original API behavior.While it's not possible to re-throw a generic
Throwable
inCompletableFuture.handle
method due to limitations of checked exceptions handling, it's possible to wrap the originalThrowable
in aCompletionException
, which would not break the CompletableFuture flow of executions.The text was updated successfully, but these errors were encountered: