Skip to content

Commit

Permalink
replace legacy Faraday::Error::* references with Faraday::*
Browse files Browse the repository at this point in the history
  • Loading branch information
technoweenie committed Oct 17, 2019
1 parent fc816cc commit f1a9520
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 46 deletions.
14 changes: 7 additions & 7 deletions lib/faraday/adapter/em_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ def perform_request(env)
end
rescue EventMachine::Connectify::CONNECTError => err
if err.message.include?("Proxy Authentication Required")
raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
raise Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
else
raise Error::ConnectionFailed, err
raise Faraday::ConnectionFailed, err
end
rescue => err
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
Expand Down Expand Up @@ -159,15 +159,15 @@ def error_message(client)
end

def raise_error(msg)
errklass = Faraday::Error::ClientError
errklass = Faraday::ClientError
if msg == Errno::ETIMEDOUT
errklass = Faraday::Error::TimeoutError
errklass = Faraday::TimeoutError
msg = "request timed out"
elsif msg == Errno::ECONNREFUSED
errklass = Faraday::Error::ConnectionFailed
errklass = Faraday::ConnectionFailed
msg = "connection refused"
elsif msg == "connection closed by server"
errklass = Faraday::Error::ConnectionFailed
errklass = Faraday::ConnectionFailed
end
raise errklass, msg
end
Expand Down Expand Up @@ -211,7 +211,7 @@ def run
end
end
if @errors.size > 0
raise Faraday::Error::ClientError, @errors.first || "connection failed"
raise Faraday::ClientError, @errors.first || "connection failed"
end
end
ensure
Expand Down
10 changes: 5 additions & 5 deletions lib/faraday/adapter/em_synchrony.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,18 @@ def call(env)

@app.call env
rescue Errno::ECONNREFUSED
raise Error::ConnectionFailed, $!
raise Faraday::ConnectionFailed, $!
rescue EventMachine::Connectify::CONNECTError => err
if err.message.include?("Proxy Authentication Required")
raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
raise Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
else
raise Error::ConnectionFailed, err
raise Faraday::ConnectionFailed, err
end
rescue Errno::ETIMEDOUT => err
raise Error::TimeoutError, err
raise Faraday::TimeoutError, err
rescue RuntimeError => err
if err.message == "connection closed by server"
raise Error::ConnectionFailed, err
raise Faraday::ConnectionFailed, err
else
raise
end
Expand Down
6 changes: 3 additions & 3 deletions lib/faraday/adapter/excon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ def call(env)
@app.call env
rescue ::Excon::Errors::SocketError => err
if err.message =~ /\btimeout\b/
raise Error::TimeoutError, err
raise Faraday::TimeoutError, err
elsif err.message =~ /\bcertificate\b/
raise Faraday::SSLError, err
else
raise Error::ConnectionFailed, err
raise Faraday::ConnectionFailed, err
end
rescue ::Excon::Errors::Timeout => err
raise Error::TimeoutError, err
raise Faraday::TimeoutError, err
end

def create_connection(env, opts)
Expand Down
8 changes: 4 additions & 4 deletions lib/faraday/adapter/httpclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ def call(env)

@app.call env
rescue ::HTTPClient::TimeoutError, Errno::ETIMEDOUT
raise Faraday::Error::TimeoutError, $!
raise Faraday::TimeoutError, $!
rescue ::HTTPClient::BadResponseError => err
if err.message.include?('status 407')
raise Faraday::Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
raise Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
else
raise Faraday::Error::ClientError, $!
raise Faraday::ClientError, $!
end
rescue Errno::ECONNREFUSED, IOError, SocketError
raise Faraday::Error::ConnectionFailed, $!
raise Faraday::ConnectionFailed, $!
rescue => err
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
raise Faraday::SSLError, err
Expand Down
4 changes: 2 additions & 2 deletions lib/faraday/adapter/net_http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def call(env)
if defined?(OpenSSL) && OpenSSL::SSL::SSLError === err
raise Faraday::SSLError, err
else
raise Error::ConnectionFailed, err
raise Faraday::ConnectionFailed, err
end
end

Expand All @@ -58,7 +58,7 @@ def call(env)

@app.call env
rescue Timeout::Error, Errno::ETIMEDOUT => err
raise Faraday::Error::TimeoutError, err
raise Faraday::TimeoutError, err
end

private
Expand Down
6 changes: 3 additions & 3 deletions lib/faraday/adapter/net_http_persistent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ def proxy_uri(env)
def perform_request(http, env)
http.request env[:url], create_request(env)
rescue Errno::ETIMEDOUT => error
raise Faraday::Error::TimeoutError, error
raise Faraday::TimeoutError, error
rescue Net::HTTP::Persistent::Error => error
if error.message.include? 'Timeout'
raise Faraday::Error::TimeoutError, error
raise Faraday::TimeoutError, error
elsif error.message.include? 'connection refused'
raise Faraday::Error::ConnectionFailed, error
raise Faraday::ConnectionFailed, error
else
raise
end
Expand Down
10 changes: 5 additions & 5 deletions lib/faraday/adapter/patron.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def call(env)
data = env[:body] ? env[:body].to_s : nil
session.request(env[:method], env[:url].to_s, env[:request_headers], :data => data)
rescue Errno::ECONNREFUSED, ::Patron::ConnectionFailed
raise Error::ConnectionFailed, $!
raise Faraday::ConnectionFailed, $!
end

# Remove the "HTTP/1.1 200", leaving just the reason phrase
Expand All @@ -39,15 +39,15 @@ def call(env)
@app.call env
rescue ::Patron::TimeoutError => err
if connection_timed_out_message?(err.message)
raise Faraday::Error::ConnectionFailed, err
raise Faraday::ConnectionFailed, err
else
raise Faraday::Error::TimeoutError, err
raise Faraday::TimeoutError, err
end
rescue ::Patron::Error => err
if err.message.include?("code 407")
raise Error::ConnectionFailed, %{407 "Proxy Authentication Required "}
raise Faraday::ConnectionFailed, %{407 "Proxy Authentication Required "}
else
raise Error::ConnectionFailed, err
raise Faraday::ConnectionFailed, err
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/faraday/adapter/rack.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def call(env)

timeout = env[:request][:timeout] || env[:request][:open_timeout]
response = if timeout
Timer.timeout(timeout, Faraday::Error::TimeoutError) { execute_request(env, rack_env) }
Timer.timeout(timeout, Faraday::TimeoutError) { execute_request(env, rack_env) }
else
execute_request(env, rack_env)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/faraday/request/retry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Faraday
#
class Request::Retry < Faraday::Middleware

DEFAULT_EXCEPTIONS = [Errno::ETIMEDOUT, 'Timeout::Error', Error::TimeoutError, Faraday::Error::RetriableResponse].freeze
DEFAULT_EXCEPTIONS = [Errno::ETIMEDOUT, 'Timeout::Error', Faraday::TimeoutError, Faraday::RetriableResponse].freeze
IDEMPOTENT_METHODS = [:delete, :get, :head, :options, :put]

class Options < Faraday::Options.new(:max, :interval, :max_interval, :interval_randomness,
Expand Down Expand Up @@ -95,7 +95,7 @@ def retry_statuses
# exceptions - The list of exceptions to handle. Exceptions can be
# given as Class, Module, or String. (default:
# [Errno::ETIMEDOUT, 'Timeout::Error',
# Error::TimeoutError, Faraday::Error::RetriableResponse])
# Faraday::TimeoutError, Faraday::RetriableResponse])
# methods - A list of HTTP methods to retry without calling retry_if. Pass
# an empty Array to call retry_if for all exceptions.
# (defaults to the idempotent HTTP methods in IDEMPOTENT_METHODS)
Expand Down Expand Up @@ -128,7 +128,7 @@ def call(env)
begin
env[:body] = request_body # after failure env[:body] is set to the response body
@app.call(env).tap do |resp|
raise Faraday::Error::RetriableResponse.new(nil, resp) if @options.retry_statuses.include?(resp.status)
raise Faraday::RetriableResponse.new(nil, resp) if @options.retry_statuses.include?(resp.status)
end
rescue @errmatch => exception
if retries > 0 && retry_request?(env, exception)
Expand All @@ -141,7 +141,7 @@ def call(env)
end
end

if exception.is_a?(Faraday::Error::RetriableResponse)
if exception.is_a?(Faraday::RetriableResponse)
exception.response
else
raise
Expand Down
22 changes: 18 additions & 4 deletions lib/faraday/response/raise_error.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
module Faraday
class Response::RaiseError < Response::Middleware
ClientErrorStatuses = 400...600
ClientErrorStatuses = (400...600).freeze
ServerErrorStatuses = (500...600).freeze

def on_complete(env)
case env[:status]
when 400
raise Faraday::BadRequestError, response_values(env)
when 401
raise Faraday::UnauthorizedError, response_values(env)
when 403
raise Faraday::ForbiddenError, response_values(env)
when 404
raise Faraday::Error::ResourceNotFound, response_values(env)
raise Faraday::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 "}
msg = %(407 "Proxy Authentication Required")
raise Faraday::ProxyAuthError.new(msg, response_values(env))
when 409
raise Faraday::ConflictError, response_values(env)
when 422
raise Faraday::UnprocessableEntityError, response_values(env)
when ClientErrorStatuses
raise Faraday::Error::ClientError, response_values(env)
raise Faraday::ClientError, response_values(env)
when ServerErrorStatuses
raise Faraday::ServerError, response_values(env)
end
end

Expand Down
8 changes: 4 additions & 4 deletions test/adapters/integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ def test_DELETE_retrieves_the_body

def test_timeout
conn = create_connection(:request => {:timeout => 1, :open_timeout => 1})
assert_raises Faraday::Error::TimeoutError do
assert_raises Faraday::TimeoutError do
conn.get '/slow'
end
end

def test_connection_error
assert_raises Faraday::Error::ConnectionFailed do
assert_raises Faraday::ConnectionFailed do
get 'http://localhost:4'
end
end
Expand All @@ -209,15 +209,15 @@ def test_proxy_auth_fail
proxy_uri.password = 'WRONG'
conn = create_connection(:proxy => proxy_uri)

err = assert_raises Faraday::Error::ConnectionFailed do
err = assert_raises Faraday::ProxyAuthError do
conn.get '/echo'
end

unless self.class.ssl_mode? && (self.class.jruby? ||
adapter == :em_http || adapter == :em_synchrony)
# JRuby raises "End of file reached" which cannot be distinguished from a 407
# EM raises "connection closed by server" due to https://github.com/igrigorik/em-socksify/pull/19
assert_equal %{407 "Proxy Authentication Required "}, err.message
assert_equal %{407 "Proxy Authentication Required"}, err.message
end
end

Expand Down
2 changes: 1 addition & 1 deletion test/adapters/patron_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def test_custom_adapter_config

def test_connection_timeout
conn = create_connection(:request => {:timeout => 10, :open_timeout => 1})
assert_raises Faraday::Error::ConnectionFailed do
assert_raises Faraday::ConnectionFailed do
conn.get 'http://8.8.8.8:88'
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/adapters/rack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_timeout
conn = create_connection(:request => {:timeout => 1, :open_timeout => 1})
begin
conn.get '/slow'
rescue Faraday::Error::TimeoutError
rescue Faraday::TimeoutError
end
end

Expand Down
4 changes: 2 additions & 2 deletions test/response_middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ def test_success
end

def test_raises_not_found
error = assert_raises Faraday::Error::ResourceNotFound do
error = assert_raises Faraday::ResourceNotFound do
@conn.get('not-found')
end
assert_equal 'the server responded with status 404', error.message
assert_equal 'because', error.response[:headers]['X-Reason']
end

def test_raises_error
error = assert_raises Faraday::Error::ClientError do
error = assert_raises Faraday::ClientError do
@conn.get('error')
end
assert_equal 'the server responded with status 500', error.message
Expand Down

0 comments on commit f1a9520

Please sign in to comment.