Micronaut-core: http-client: support returning http response without exception when status >= 400

Created on 3 Oct 2019  Â·  5Comments  Â·  Source: micronaut-projects/micronaut-core

Task List

  • [x] Steps to reproduce provided
  • [x] Stacktrace (if present) provided
  • [x] Example that reproduces the problem uploaded to Github
  • [x] Full description of the issue provided (see below)

Steps to Reproduce

  1. With HttpClient, query an endpoint that returns a status code >= 400
  2. Notice HttpClientResponseException is thrown
io.micronaut.http.client.exceptions.HttpClientResponseException: Conflict

    at io.micronaut.http.client.DefaultHttpClient$10.channelRead0(DefaultHttpClient.java:1821)
    at io.micronaut.http.client.DefaultHttpClient$10.channelRead0(DefaultHttpClient.java:1761)
    at io.netty.channel.SimpleChannelInboundHandler.channelRead(SimpleChannelInboundHandler.java:105)

Expected Behaviour

Need HttpClient implementation that does _not create nor throw_ an exception when full response has been received and status code >= 400.
This should be a configurable option in HttpClientConfiguration.

Actual Behaviour

DefaultHttpClient always throw HttpClientResponseException when status code is >= 400.
https://github.com/micronaut-projects/micronaut-core/blob/master/http-client/src/main/java/io/micronaut/http/client/DefaultHttpClient.java#L1807

Environment Information

  • Operating System: Mac OS 10.14.6 (mojave)
  • Micronaut Version: 1.2.3
  • JDK Version: Oracle JDK 1.8.0_181

Example Application

Code identical to _Mocking Collaborators_ official test example: https://micronaut-projects.github.io/micronaut-test/latest/guide/index.html#_mocking_collaborators but with the endpoint returning an error.

Micronaut code throwing the exception: https://github.com/micronaut-projects/micronaut-core/blob/master/http-client/src/main/java/io/micronaut/http/client/DefaultHttpClient.java#L1807

http-client improvement

Most helpful comment

Is this something that could be added soon?
Writing tests for error cases (code ≥ 400) using HttpClient is very cumbersome at the momen. I had to define some nasty utility methods to try/catch every call to work around this ... I'd like to make micronaut mainstream on my team soon and not have to tell everyone to rewrite all their tests (to remove the utility methods) in 6 months when this ships...

All 5 comments

I think rather than making it configurable it should be another method on the HttpClient interface

It would be nice to have it configurable for declarative http clients.

Is this something that could be added soon?
Writing tests for error cases (code ≥ 400) using HttpClient is very cumbersome at the momen. I had to define some nasty utility methods to try/catch every call to work around this ... I'd like to make micronaut mainstream on my team soon and not have to tell everyone to rewrite all their tests (to remove the utility methods) in 6 months when this ships...

@graemerocher Would it be acceptable to modify behavior (for 1.3.x) of the existing exchange methods to not throw an exception when bodyType == errorType ? This would preserve most of the existing default behaviors (since errorType is always different than bodyType by default).

From a usage standpoint, there is no reason to throw an exception when the bad response can gracefully be decoded (and returned) without triggering an alternate flow...

This turned out to be a trivial change: https://github.com/micronaut-projects/micronaut-core/pull/2372

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dchenk picture dchenk  Â·  3Comments

dforegger picture dforegger  Â·  3Comments

iBernd81 picture iBernd81  Â·  3Comments

Solido picture Solido  Â·  5Comments

ggservice007 picture ggservice007  Â·  5Comments