Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix compatibility with Faraday 1.0 #194

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ rvm:
gemfile:
- Gemfile
- Gemfile-0.9.rb
- Gemfile-1.0.rb
matrix:
exclude:
- rvm: 1.9.3
gemfile: Gemfile-1.0.rb
- rvm: 2.0
gemfile: Gemfile-1.0.rb
- rvm: 2.1
gemfile: Gemfile-1.0.rb
- rvm: 2.2
gemfile: Gemfile-1.0.rb
- rvm: 2.4
gemfile: Gemfile-0.9.rb
- rvm: 2.5.0
Expand Down
3 changes: 3 additions & 0 deletions Gemfile-1.0.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
eval File.read(File.expand_path('../Gemfile', __FILE__))

gem 'faraday', '~> 1.0.0.pre.rc1'
2 changes: 1 addition & 1 deletion faraday_middleware.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Gem::Specification.new do |spec|
spec.homepage = 'https://github.com/lostisland/faraday_middleware'
spec.licenses = ['MIT']

spec.add_dependency 'faraday', ['>= 0.7.4', '< 1.0']
spec.add_dependency 'faraday', '>= 0.7.4'

spec.files = `git ls-files -z lib LICENSE.md README.md`.split("\0")
end
9 changes: 8 additions & 1 deletion lib/faraday_middleware/response/follow_redirects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@
require 'set'

module FaradayMiddleware
CLIENT_ERROR =
begin
Faraday::Error::ClientError
rescue NameError
Faraday::ClientError
end

# Public: Exception thrown when the maximum amount of requests is exceeded.
class RedirectLimitReached < Faraday::Error::ClientError
class RedirectLimitReached < CLIENT_ERROR
attr_reader :response

def initialize(response)
Expand Down
13 changes: 10 additions & 3 deletions lib/faraday_middleware/response_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ module FaradayMiddleware
class ResponseMiddleware < Faraday::Middleware
CONTENT_TYPE = 'Content-Type'.freeze

PARSING_ERROR =
begin
Faraday::ParsingError
rescue NameError
Faraday::Error::ParsingError
end

class << self
attr_accessor :parser
end
Expand Down Expand Up @@ -38,8 +45,8 @@ def call(environment)
def process_response(env)
env[:raw_body] = env[:body] if preserve_raw?(env)
env[:body] = parse(env[:body])
rescue Faraday::Error::ParsingError => err
raise Faraday::Error::ParsingError.new(err, env[:response])
rescue PARSING_ERROR => err
raise PARSING_ERROR.new(err, env[:response])
end

# Parse the response body.
Expand All @@ -51,7 +58,7 @@ def parse(body)
self.class.parser.call(body, @parser_options)
rescue StandardError, SyntaxError => err
raise err if err.is_a? SyntaxError and err.class.name != 'Psych::SyntaxError'
raise Faraday::Error::ParsingError, err
raise PARSING_ERROR, err
end
else
body
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/parse_json_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@
end

it "chokes on invalid json" do
expect{ process('{!') }.to raise_error(Faraday::Error::ParsingError)
expect{ process('{!') }.to raise_error(FaradayMiddleware::ResponseMiddleware::PARSING_ERROR)
end

it "includes the response on the ParsingError instance" do
begin
process('{') { |env| env[:response] = Faraday::Response.new }
fail 'Parsing should have failed.'
rescue Faraday::Error::ParsingError => err
rescue FaradayMiddleware::ResponseMiddleware::PARSING_ERROR => err
expect(err.response).to be_a(Faraday::Response)
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/unit/parse_marshal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
end

it "chokes on invalid content" do
expect{ process('abc') }.to raise_error(Faraday::Error::ParsingError)
expect{ process('abc') }.to raise_error(FaradayMiddleware::ResponseMiddleware::PARSING_ERROR)
end
end
2 changes: 1 addition & 1 deletion spec/unit/parse_xml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@

it "chokes on invalid xml" do
['{!', '"a"', 'true', 'null', '1'].each do |data|
expect{ process(data) }.to raise_error(Faraday::Error::ParsingError)
expect{ process(data) }.to raise_error(FaradayMiddleware::ResponseMiddleware::PARSING_ERROR)
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/unit/parse_yaml_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
end

it "chokes on invalid yaml" do
expect{ process('{!') }.to raise_error(Faraday::Error::ParsingError)
expect{ process('{!') }.to raise_error(FaradayMiddleware::ResponseMiddleware::PARSING_ERROR)
end

context "SafeYAML options" do
Expand Down