Skip to content

Commit

Permalink
Merge pull request #1 from sshao/faraday_1_0_upgrade
Browse files Browse the repository at this point in the history
Update to use Faraday ~> 1.0
  • Loading branch information
packrat386 authored May 26, 2020
2 parents 4eee42b + 500ac34 commit 2d6bbb4
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 48 deletions.
8 changes: 3 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
language: ruby
rvm:
- 1.9.3
- 2.0.0
- 2.1.1
- 2.3.0
- 2.7.0
gemfile:
- gemfiles/faraday_0.8.gemfile
- gemfiles/faraday_0.9.gemfile
- gemfiles/faraday_1.0.0.gemfile
8 changes: 2 additions & 6 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
appraise "faraday-0.8" do
gem "faraday", "~> 0.8.0"
end

appraise "faraday-0.9" do
gem "faraday", "~> 0.9.0"
appraise "faraday-1.0.0" do
gem "faraday", "~> 1.0.0"
end
7 changes: 2 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,7 @@ connection = Faraday.new(url: "http://widgets.yourapp.com") do |faraday|
end
```

The errors raised will be the same as Faraday, namely
`Faraday::Error::ResourceNotFound` for 404 errors,
`Faraday::Error::ConnectionFailed` for 407 and `Faraday::Error::ClientError` for
the rest.
The errors raised will be the same [as Faraday](https://github.com/lostisland/faraday/blob/019e1a841707718adad2fd05c602eb1a869b42bc/lib/faraday/response/raise_error.rb).

If you don't specify the `:on` or `:except` options, it will behave exactly like
`:raise_error`. The errors are however "enhanced" with extra information about
Expand All @@ -176,7 +173,7 @@ the request that normally are lost:
``` ruby
begin
do_failing_request_here
rescue Faraday::Error::ClientError => error
rescue Faraday::ClientError => error
puts error.request[:url]
puts error.request[:method]
puts error.request[:body]
Expand Down
2 changes: 1 addition & 1 deletion faraday-conductivity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
gem.require_paths = ["lib"]

gem.add_dependency "faraday", "~> 0.8"
gem.add_dependency "faraday", "~> 1.0"
gem.add_development_dependency "rake"
gem.add_development_dependency "rspec", "~> 3"
gem.add_development_dependency "pry"
Expand Down
7 changes: 0 additions & 7 deletions gemfiles/faraday_0.9.gemfile

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

source "https://rubygems.org"

gem "faraday", "~> 0.8.0"
gem "faraday", "~> 1.0.0"

gemspec :path=>"../"
gemspec path: "../"
49 changes: 32 additions & 17 deletions lib/faraday/conductivity/selective_errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ def inspect
# end
class SelectiveErrors < Faraday::Middleware

ClientErrorStatuses = 400...600
ClientErrorStatuses = (400...500).freeze
ServerErrorStatuses = (500...600).freeze

def initialize(app, options = {})
@app = app
Expand All @@ -74,35 +75,49 @@ def call(env)
start_time = Time.now

@app.call(env).on_complete do

status = env[:status]

if should_raise_error?(status)
response = {
:status => env[:status],
:body => env[:body],
:headers => env[:response_headers],
}
error = case status
when 404
Faraday::Error::ResourceNotFound.new(response)
when 407
# mimic the behavior that we get with proxy requests with HTTPS
Faraday::Error::ConnectionFailed.new(%{407 "Proxy Authentication Required "})
else
Faraday::Error::ClientError.new(response)
end
when 400
Faraday::BadRequestError.new(response_values(env))
when 401
Faraday::UnauthorizedError.new(response_values(env))
when 403
Faraday::ForbiddenError.new(response_values(env))
when 404
Faraday::ResourceNotFound.new(response_values(env))
when 407
# mimic the behavior that we get with proxy requests with HTTPS
msg = %(407 "Proxy Authentication Required")
Faraday::ProxyAuthError.new(msg, response_values(env))
when 409
Faraday::ConflictError.new(response_values(env))
when 422
Faraday::UnprocessableEntityError.new(response_values(env))
when ClientErrorStatuses
Faraday::ClientError.new(response_values(env))
when ServerErrorStatuses
Faraday::ServerError.new(response_values(env))
when nil
Faraday::NilStatusError.new(response_values(env))
end

error.extend Error
error.response = response
error.response = response_values(env)
error.request = request
error.response_time = Time.now - start_time
raise error

raise error
end

end
end

def response_values(env)
{ status: env.status, headers: env.response_headers, body: env.body }
end

def should_raise_error?(status)
@on.include?(status) && !@except.include?(status)
end
Expand Down
10 changes: 5 additions & 5 deletions spec/middleware/selective_errors_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

it "raises an exception if the error is inside the :on argument" do
apply_selective_errors on: 407..409
expect { response_with_status(408) }.to raise_error Faraday::Error::ClientError
expect { response_with_status(408) }.to raise_error Faraday::ClientError
end

it "won't raise an exception when outside the range" do
Expand All @@ -19,17 +19,17 @@

it "raises an exception if included in :on and not included in :except" do
apply_selective_errors on: 403..422, except: [408]
expect { response_with_status(409) }.to raise_error Faraday::Error::ClientError
expect { response_with_status(409) }.to raise_error Faraday::ClientError
end

it "raises a resource not found error when the actual status is 404" do
apply_selective_errors on: 403..422, except: [408]
expect { response_with_status(404) }.to raise_error Faraday::Error::ResourceNotFound
expect { response_with_status(404) }.to raise_error Faraday::ResourceNotFound
end

it "raises a connection failed on 407" do
it "raises proxy auth required on 407" do
apply_selective_errors on: 403..422, except: [408]
expect { response_with_status(407) }.to raise_error Faraday::Error::ConnectionFailed
expect { response_with_status(407) }.to raise_error Faraday::ProxyAuthError
end

it "stores more information about the request and response" do
Expand Down

0 comments on commit 2d6bbb4

Please sign in to comment.