Skip to content

Commit 0e008c5

Browse files
authored
Use newer Unprocessable Content naming for 422 (#1638)
1 parent c399e43 commit 0e008c5

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

docs/middleware/included/raising-errors.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ by the client. They raise error classes inheriting from `Faraday::ClientError`.
3838
| [407](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/407) | `Faraday::ProxyAuthError` |
3939
| [408](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/408) | `Faraday::RequestTimeoutError` |
4040
| [409](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/409) | `Faraday::ConflictError` |
41-
| [422](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422) | `Faraday::UnprocessableEntityError` |
41+
| [422](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422) | `Faraday::UnprocessableContentError` |
4242
| [429](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/429) | `Faraday::TooManyRequestsError` |
4343
| 4xx (any other) | `Faraday::ClientError` |
4444

lib/faraday/error.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,12 @@ class ConflictError < ClientError
155155
end
156156

157157
# Raised by Faraday::Response::RaiseError in case of a 422 response.
158-
class UnprocessableEntityError < ClientError
158+
class UnprocessableContentError < ClientError
159159
end
160160

161+
# Used to provide compatibility with legacy error name.
162+
UnprocessableEntityError = UnprocessableContentError
163+
161164
# Raised by Faraday::Response::RaiseError in case of a 429 response.
162165
class TooManyRequestsError < ClientError
163166
end

lib/faraday/response/raise_error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RaiseError < Middleware
1515
404 => Faraday::ResourceNotFound,
1616
408 => Faraday::RequestTimeoutError,
1717
409 => Faraday::ConflictError,
18-
422 => Faraday::UnprocessableEntityError,
18+
422 => Faraday::UnprocessableContentError,
1919
429 => Faraday::TooManyRequestsError
2020
}.freeze
2121
# rubocop:enable Naming/ConstantName

spec/faraday/response/raise_error_spec.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
stub.get('proxy-error') { [407, { 'X-Reason' => 'because' }, 'keep looking'] }
1414
stub.get('request-timeout') { [408, { 'X-Reason' => 'because' }, 'keep looking'] }
1515
stub.get('conflict') { [409, { 'X-Reason' => 'because' }, 'keep looking'] }
16-
stub.get('unprocessable-entity') { [422, { 'X-Reason' => 'because' }, 'keep looking'] }
16+
stub.get('unprocessable-content') { [422, { 'X-Reason' => 'because' }, 'keep looking'] }
1717
stub.get('too-many-requests') { [429, { 'X-Reason' => 'because' }, 'keep looking'] }
1818
stub.get('4xx') { [499, { 'X-Reason' => 'because' }, 'keep looking'] }
1919
stub.get('nil-status') { [nil, { 'X-Reason' => 'nil' }, 'fail'] }
@@ -103,9 +103,20 @@
103103
end
104104
end
105105

106-
it 'raises Faraday::UnprocessableEntityError for 422 responses' do
107-
expect { conn.get('unprocessable-entity') }.to raise_error(Faraday::UnprocessableEntityError) do |ex|
108-
expect(ex.message).to eq('the server responded with status 422 for GET http:/unprocessable-entity')
106+
it 'raises legacy Faraday::UnprocessableEntityError for 422 responses' do
107+
expect { conn.get('unprocessable-content') }.to raise_error(Faraday::UnprocessableEntityError) do |ex|
108+
expect(ex.message).to eq('the server responded with status 422 for GET http:/unprocessable-content')
109+
expect(ex.response[:headers]['X-Reason']).to eq('because')
110+
expect(ex.response[:status]).to eq(422)
111+
expect(ex.response_status).to eq(422)
112+
expect(ex.response_body).to eq('keep looking')
113+
expect(ex.response_headers['X-Reason']).to eq('because')
114+
end
115+
end
116+
117+
it 'raises Faraday::UnprocessableContentError for 422 responses' do
118+
expect { conn.get('unprocessable-content') }.to raise_error(Faraday::UnprocessableContentError) do |ex|
119+
expect(ex.message).to eq('the server responded with status 422 for GET http:/unprocessable-content')
109120
expect(ex.response[:headers]['X-Reason']).to eq('because')
110121
expect(ex.response[:status]).to eq(422)
111122
expect(ex.response_status).to eq(422)

0 commit comments

Comments
 (0)