Skip to content
This repository was archived by the owner on Apr 6, 2023. It is now read-only.

Commit c0f2ec2

Browse files
authored
Merge pull request #1 from carwow/add_extra_error_classes
Add extra error classes to handle server errors.
2 parents 9b9835b + f407acd commit c0f2ec2

File tree

3 files changed

+82
-12
lines changed

3 files changed

+82
-12
lines changed

lib/json_api_client/errors.rb

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,28 @@ 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 RequestTimeout < ClientError
58+
end
59+
60+
class Conflict < ClientError
61+
def initialize(env, msg = 'Resource already exists')
62+
super env, msg
63+
end
64+
end
65+
66+
class TooManyRequests < ClientError
67+
end
68+
4769
class ConnectionError < ApiError
4870
end
4971

@@ -59,23 +81,16 @@ def initialize(env, msg = nil)
5981
end
6082
end
6183

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

68-
class NotFound < ServerError
69-
attr_reader :uri
70-
def initialize(uri)
71-
@uri = uri
87+
class BadGateway < ServerError
88+
end
7289

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

78-
class InternalServerError < ServerError
93+
class GatewayTimeout < ServerError
7994
end
8095

8196
class UnexpectedStatus < ServerError

lib/json_api_client/middleware/status.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,24 @@ def handle_status(code, env)
3838
raise Errors::AccessDenied, env
3939
when 404
4040
raise Errors::NotFound, env[:url]
41+
when 408
42+
raise Errors::RequestTimeout, env
4143
when 409
4244
raise Errors::Conflict, env
4345
when 422
4446
# Allow to proceed as resource errors will be populated
47+
when 429
48+
raise Errors::TooManyRequests, env
4549
when 400..499
4650
raise Errors::ClientError, env
4751
when 500
4852
raise Errors::InternalServerError, env
53+
when 502
54+
raise Errors::BadGateway, env
55+
when 503
56+
raise Errors::ServiceUnavailable, env
57+
when 504
58+
raise Errors::GatewayTimeout, env
4959
when 501..599
5060
raise Errors::ServerError, env
5161
else

test/unit/errors_test.rb

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

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

0 commit comments

Comments
 (0)