OpenFeign / OpenFeign/feign

Cannot get response headers from a FeignException

Open
#1,268 2 comments 8 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feedback provided help wanted
Dominant language
Java
Stars
9.8k
Forks
1.9k
Avg merge
1d 2h
Merged PRs (30d)
41

Description

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?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FeignException API and the response handling path that creates it. Determine whether response headers are retained, then assess the requested access pattern or an alternative for relaying the response. Done means the supported behavior is implemented or clearly documented, with coverage for response headers and content types.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, kotlin
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.