Skip to content

Instantly share code, notes, and snippets.

@prcongithub
Created June 24, 2013 11:46
Show Gist options
  • Select an option

  • Save prcongithub/5849505 to your computer and use it in GitHub Desktop.

Select an option

Save prcongithub/5849505 to your computer and use it in GitHub Desktop.
Faraday Exceptions
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 422
raise Faraday::Error::UnprocessableEntity, response_values(env)
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