Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion docs/middleware/included/raising-errors.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ by the client. They raise error classes inheriting from `Faraday::ClientError`.
| [407](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/407) | `Faraday::ProxyAuthError` |
| [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) | `Faraday::RequestTimeoutError` |
| [409](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409) | `Faraday::ConflictError` |
| [422](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422) | `Faraday::UnprocessableEntityError` |
| [422](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422) | `Faraday::UnprocessableContentError` |
| [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) | `Faraday::TooManyRequestsError` |
| 4xx (any other) | `Faraday::ClientError` |

Expand Down
7 changes: 6 additions & 1 deletion lib/faraday/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,13 @@ class RequestTimeoutError < ClientError
class ConflictError < ClientError
end

# Used to provide compatibility with legacy error name.
module UnprocessableEntityError
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather use a constant assignment to keep this as a class

end

# Raised by Faraday::Response::RaiseError in case of a 422 response.
class UnprocessableEntityError < ClientError
class UnprocessableContentError < ClientError
include UnprocessableEntityError
end

# Raised by Faraday::Response::RaiseError in case of a 429 response.
Expand Down
2 changes: 1 addition & 1 deletion lib/faraday/response/raise_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class RaiseError < Middleware
404 => Faraday::ResourceNotFound,
408 => Faraday::RequestTimeoutError,
409 => Faraday::ConflictError,
422 => Faraday::UnprocessableEntityError,
422 => Faraday::UnprocessableContentError,
429 => Faraday::TooManyRequestsError
}.freeze
# rubocop:enable Naming/ConstantName
Expand Down
19 changes: 15 additions & 4 deletions spec/faraday/response/raise_error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
stub.get('proxy-error') { [407, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('request-timeout') { [408, { '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('unprocessable-content') { [422, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('too-many-requests') { [429, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('4xx') { [499, { 'X-Reason' => 'because' }, 'keep looking'] }
stub.get('nil-status') { [nil, { 'X-Reason' => 'nil' }, 'fail'] }
Expand Down Expand Up @@ -103,9 +103,20 @@
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 for GET http:/unprocessable-entity')
it 'raises legacy Faraday::UnprocessableEntityError for 422 responses' do
expect { conn.get('unprocessable-content') }.to raise_error(Faraday::UnprocessableEntityError) do |ex|
expect(ex.message).to eq('the server responded with status 422 for GET http:/unprocessable-content')
expect(ex.response[:headers]['X-Reason']).to eq('because')
expect(ex.response[:status]).to eq(422)
expect(ex.response_status).to eq(422)
expect(ex.response_body).to eq('keep looking')
expect(ex.response_headers['X-Reason']).to eq('because')
end
end

it 'raises Faraday::UnprocessableContentError for 422 responses' do
expect { conn.get('unprocessable-content') }.to raise_error(Faraday::UnprocessableContentError) do |ex|
expect(ex.message).to eq('the server responded with status 422 for GET http:/unprocessable-content')
expect(ex.response[:headers]['X-Reason']).to eq('because')
expect(ex.response[:status]).to eq(422)
expect(ex.response_status).to eq(422)
Expand Down