Skip to content

Commit 37f3427

Browse files
committed
Add extra error classes to handle server errors.
In order to provide more granularity on the exceptions, we are adding some additional error classes for the common Server errors.
1 parent 9b9835b commit 37f3427

File tree

3 files changed

+68
-12
lines changed

3 files changed

+68
-12
lines changed

lib/json_api_client/errors.rb

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ class AccessDenied < ClientError
4444
class NotAuthorized < ClientError
4545
end
4646

47+
class NotFound < ClientError
48+
attr_reader :uri
49+
def initialize(uri)
50+
@uri = uri
51+
52+
msg = "Couldn't find resource at: #{uri.to_s}"
53+
super nil, msg
54+
end
55+
end
56+
57+
class Conflict < ClientError
58+
def initialize(env, msg = 'Resource already exists')
59+
super env, msg
60+
end
61+
end
62+
63+
class TooManyRequests < ClientError
64+
end
65+
4766
class ConnectionError < ApiError
4867
end
4968

@@ -59,23 +78,16 @@ def initialize(env, msg = nil)
5978
end
6079
end
6180

62-
class Conflict < ServerError
63-
def initialize(env, msg = 'Resource already exists')
64-
super env, msg
65-
end
81+
class InternalServerError < ServerError
6682
end
6783

68-
class NotFound < ServerError
69-
attr_reader :uri
70-
def initialize(uri)
71-
@uri = uri
84+
class BadGateway < ServerError
85+
end
7286

73-
msg = "Couldn't find resource at: #{uri.to_s}"
74-
super nil, msg
75-
end
87+
class ServiceUnavailable < ServerError
7688
end
7789

78-
class InternalServerError < ServerError
90+
class GatewayTimeout < ServerError
7991
end
8092

8193
class UnexpectedStatus < ServerError

lib/json_api_client/middleware/status.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,18 @@ def handle_status(code, env)
4242
raise Errors::Conflict, env
4343
when 422
4444
# Allow to proceed as resource errors will be populated
45+
when 429
46+
raise Errors::TooManyRequests, env
4547
when 400..499
4648
raise Errors::ClientError, env
4749
when 500
4850
raise Errors::InternalServerError, env
51+
when 502
52+
raise Errors::BadGateway, env
53+
when 503
54+
raise Errors::ServiceUnavailable, env
55+
when 504
56+
raise Errors::GatewayTimeout, env
4957
when 501..599
5058
raise Errors::ServerError, env
5159
else

test/unit/errors_test.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,42 @@ def test_not_authorized
9696
end
9797
end
9898

99+
def test_too_many_requests
100+
stub_request(:get, "http://example.com/users")
101+
.to_return(headers: {content_type: "text/plain"}, status: 429, body: "too many requests")
102+
103+
assert_raises JsonApiClient::Errors::TooManyRequests do
104+
User.all
105+
end
106+
end
107+
108+
def test_bad_gateway
109+
stub_request(:get, "http://example.com/users")
110+
.to_return(headers: {content_type: "text/plain"}, status: 502, body: "bad gateway")
111+
112+
assert_raises JsonApiClient::Errors::BadGateway do
113+
User.all
114+
end
115+
end
116+
117+
def test_service_unavailable
118+
stub_request(:get, "http://example.com/users")
119+
.to_return(headers: {content_type: "text/plain"}, status: 503, body: "service unavailable")
120+
121+
assert_raises JsonApiClient::Errors::ServiceUnavailable do
122+
User.all
123+
end
124+
end
125+
126+
def test_gateway_timeout
127+
stub_request(:get, "http://example.com/users")
128+
.to_return(headers: {content_type: "text/plain"}, status: 504, body: "gateway timeout")
129+
130+
assert_raises JsonApiClient::Errors::GatewayTimeout do
131+
User.all
132+
end
133+
end
134+
99135
def test_errors_are_rescuable_by_default_rescue
100136
begin
101137
raise JsonApiClient::Errors::ApiError, "Something bad happened"

0 commit comments

Comments
 (0)