ololo
ololo

Reputation: 2076

How to Get exact Rest API Error from Feign Client

I am new to Feign Client on Spring Boot, so please permit me.

I am trying to make a network call with Feign Client using the code below

fun validateAccessToken(accessToken: String?): ResponseEntity<ObjectNode> {
        val tokenConfirmationEndpoint = "https://graph.facebook.com"
        val facebookAPIClient = Feign
            .builder()
            .target(FacebookAPIClient::class.java, tokenConfirmationEndpoint)
        val facebookAccessTokenResponse = facebookAPIClient.validateAccessToken(accessToken)
        val body = JSONObject(facebookAccessTokenResponse?.body)
        logger.info("Feign Error $body")
        return responseHandler.errorResponse(facebookAccessTokenResponse?.statusCode?.value() ?: 0, body.toString())
    }

Now, when I deliberately supplied a wrong access Token to test the client using Post Man as below

Request is http://localhost:8080/validateToken?token=adadadadad

The Facebook API is returning a valid error that states Invalid OAUTH access token but instead of Feign client to give me the actual error message from Facebook, Feign client is adding some text before the error message.

Here is what Feign Client is returning as the error message

feign.FeignException$BadRequest: [400 Bad Request] during [GET] to [https://graph.facebook.com/me?access_token=adadadadad] [FacebookAPIClient#validateAccessToken(String)]: [{\"error\":{\"message\":\"Invalid OAuth access token.\",\"type\":\"OAuthException\",\"code\":190,\"fbtrace_id\":\"ApihqZcs6bOyBjCxDgTYklS\"}}]\n\tat feign.FeignException.clientErrorStatus(FeignException.java:213)\n\tat feign.FeignException.errorStatus(FeignException.java:194)\n\tat feign.FeignException.errorStatus(FeignException.java:185)\n\tat feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:92)\n\tat feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:96)\n\tat feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138)

But I want it to instead return exactly the error from facebook as this

{\"error\":{\"message\":\"Invalid OAuth access token.\",\"type\":\"OAuthException\",\"code\":190,\"fbtrace_id\":\"ApihqZcs6bOyBjCxDgTYklS\"}}

And nothing more.

So, how can I make the Feign Client return the exact message from the API and stop adding all that FeignException text before and after the error?

Thank you

Upvotes: 0

Views: 1247

Answers (1)

new String(((FeignException)ex).responseBody().get().array(), StandardCharsets.UTF_8)

Upvotes: -1

Related Questions