Skip to content

Instantly share code, notes, and snippets.

@adoseofjess
Created October 29, 2014 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adoseofjess/c7e928eb38c5f9972f96 to your computer and use it in GitHub Desktop.
Save adoseofjess/c7e928eb38c5f9972f96 to your computer and use it in GitHub Desktop.
Faraday Response::RaiseError
module Faraday
class Response::RaiseError < Response::Middleware
ClientErrorStatuses = 400...600
def on_complete(env)
case env[:status]
when 404
raise Faraday::Error::ResourceNotFound, response_values(env)
when 407
# mimic the behavior that we get with proxy requests with HTTPS
raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
when ClientErrorStatuses
raise Faraday::Error::ClientError, response_values(env)
end
end
def response_values(env)
{:status => env.status, :headers => env.response_headers, :body => env.body}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment