OpenFeign / feign-annotation-error-decoder

Apache License 2.0
51 stars 9 forks source link

[Question] [support needed] Error Response Body is empty #24

Open hellboy81 opened 5 years ago

hellboy81 commented 5 years ago

I am trying to decode error response body but got always null:

code specific ErrorCodes error Exception classes are generated properly but with empty body:

image

Feign's OkHttp client is used.

Where is my bad?

Used versions:

P.S. Another developers from UA reported feign-error-decoder does not work properly.

See also my question in chat.

saintf commented 5 years ago

@hellboy81 can you try:

There's one part in the exception generator that I'm not terribly proud of:

  private Object resolveBody(Response response) {
    if (bodyType instanceof Class<?> && ((Class<?>) bodyType).isInstance(response)) {
      return response;
    }
    try {
      return bodyDecoder.decode(response, bodyType);
    } catch (IOException e) {
      // How do we log this?
      return null;
    } catch (DecodeException e) {
      // How do we log this?
      return null;
    }
  }

you see that it basically tries its best to decode the body, but if it fails, it actually does so silently (yuck).

That said - Feign itself doesn't decide on a logger for you... which begs the question: how to best log?

I expect that ErrorResponse is likely being returned in a format you don't expect or that your POJO doesn't fit to. You can perhaps debug by having the constructor just take feign.Response and capture the body/decode it yourself just to see what is happening.

The other option is a little more intrusive (Support some form of logging - while still not trying to be opinionated - much like what Feign itself tries to do). If you have a proposal (maybe we include a Logger class/object that logs the error, and if available, the body or so) then I'll be happy to implement it (or even, if you are up for it, have you implement it and I'll review).

Thoughts?