Skip to content

Commit 99d1de5

Browse files
Do not raise errors on 422 responses
JSON APIs often return validation errors with 422 so the client would be notified that the response was not successful by the existence of the resource errors.
1 parent 118fa6c commit 99d1de5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

lib/json_api_client/middleware/status.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ def handle_status(code, env)
2828
raise Errors::NotFound, env[:url]
2929
when 409
3030
raise Errors::Conflict, env
31+
when 422
32+
# Allow to proceed as resource errors will be populated
3133
when 400..499
3234
raise Errors::ClientError, env
3335
when 500..599

test/unit/status_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,16 @@ def test_server_responding_with_408_status
6969
User.find(1)
7070
end
7171
end
72+
73+
def test_server_responding_with_422_status
74+
stub_request(:get, "http://example.com/users/1")
75+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
76+
meta: {
77+
status: 422
78+
}
79+
}.to_json)
80+
81+
# We want to test that this response does not raise an error
82+
User.find(1)
83+
end
7284
end

0 commit comments

Comments
 (0)