OpenFeign/feign

Cannot get response headers from a FeignException

Open

#1268 aperta il 3 set 2020

Vedi su GitHub
 (2 commenti) (8 reazioni) (0 assegnatari)Java (1911 fork)batch import
feedback providedhelp wanted

Metriche repository

Star
 (9070 star)
Metriche merge PR
 (Merge medio 2g 17h) (71 PR mergiate in 30 g)

Descrizione

TL;DR: Is it possible to retrieve response headers from a FeignException?

My use case is this: I'm using Feign from microservice A to call microservice B. If the call fails with a 4xx status code (which results in a FeignClientException being thrown), the response may contain information about what went wrong, which fields were missing in the request, etc, so I'd like to relay the response to the caller of microservice A. I've created a Spring exception handler for this purpose:

@RestControllerAdvice
class FeignExceptionHandler {

    @ExceptionHandler(FeignException.FeignClientException::class)
    fun handleFeignClientException(e: FeignException.FeignClientException): ResponseEntity<String> {
        val headers = HttpHeaders()
        headers.contentType = MediaType.APPLICATION_JSON
        return ResponseEntity(e.contentUTF8(), headers, HttpStatus.valueOf(e.status()))
    }

}

The thing is, that the response content type may not always be application/json, so I'd like to fetch the content type from the response itself (if it's available). However, I found no easy way to retrieve the response headers from a FeignException, only the response body is available. Am I missing something obvious here? Or maybe is there a better way to achieve what I'm trying to do?

Guida contributor