I initialize my Faraday client this way:
@client = Faraday.new(url: BASE_URL) do |faraday|
faraday.request :json
faraday.response :json
faraday.response :raise_error
end
And when I rescue the Faraday::ClientError
, the response's body looks like this:
irb> e.response[:body]
"{\"error\":\"access_denied\",\"error_description\":\"Unauthorized\"}"
I would expect it to be parsed and have the body be a Hash. What am I doing wrong?
In Faraday, the order of registration of middlewares matters because it's a pipeline (similar to Rack).
So if you put the :raise_error
middleware before :json
, the error's body will be parsed.
@client = Faraday.new(url: BASE_URL) do |faraday|
faraday.request :json
faraday.response :raise_error
faraday.response :json
end