Skip to content

Commit

Permalink
Introduces Faraday::ConflictError for 409 response code (#979)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmoreno authored and iMacTia committed May 23, 2019
1 parent 7af105d commit 91ba32e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/faraday/error.rb
Expand Up @@ -61,6 +61,10 @@ class ResourceNotFound < ClientError
class ProxyAuthError < ClientError
end

# Raised by Faraday::Response::RaiseError in case of a 409 response.
class ConflictError < ClientError
end

# Raised by Faraday::Response::RaiseError in case of a 422 response.
class UnprocessableEntityError < ClientError
end
Expand Down
2 changes: 2 additions & 0 deletions lib/faraday/response/raise_error.rb
Expand Up @@ -24,6 +24,8 @@ def on_complete(env)
# mimic the behavior that we get with proxy requests with HTTPS
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
Expand Down
8 changes: 8 additions & 0 deletions spec/faraday/response/raise_error_spec.rb
Expand Up @@ -11,6 +11,7 @@
stub.get('forbidden') { [403, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('not-found') { [404, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('proxy-error') { [407, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('conflict') { [409, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('unprocessable-entity') { [422, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('4xx') { [499, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('server-error') { [500, { 'X-Error' => 'bailout' }, 'fail'] }
Expand Down Expand Up @@ -57,6 +58,13 @@
end
end

it 'raises Faraday::ConflictError for 409 responses' do
expect { conn.get('conflict') }.to raise_error(Faraday::ConflictError) do |ex|
expect(ex.message).to eq('the server responded with status 409')
expect(ex.response[:headers]['X-Reason']).to eq('because')
end
end

it 'raises Faraday::UnprocessableEntityError for 422 responses' do
expect { conn.get('unprocessable-entity') }.to raise_error(Faraday::UnprocessableEntityError) do |ex|
expect(ex.message).to eq('the server responded with status 422')
Expand Down

0 comments on commit 91ba32e

Please sign in to comment.