Skip to content

Commit c8a662c

Browse files
committed
Add specific error class for 503s
I've also cleaned up the use of `last_response` versus `@last_response`.
1 parent d9b0ccc commit c8a662c

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed

lib/help_scout.rb

+10-6
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class NotFoundError < StandardError; end
88
class TooManyRequestsError < StandardError; end
99
class InternalServerError < StandardError; end
1010
class ForbiddenError < StandardError; end
11+
class ServiceUnavailable < StandardError; end
1112

1213
# Status codes used by Help Scout, not all are implemented in this gem yet.
1314
# http://developer.helpscout.net/help-desk-api/status-codes/
@@ -19,6 +20,7 @@ class ForbiddenError < StandardError; end
1920
HTTP_NOT_FOUND = 404
2021
HTTP_TOO_MANY_REQUESTS = 429
2122
HTTP_INTERNAL_SERVER_ERROR = 500
23+
HTTP_SERVICE_UNAVAILABLE = 503
2224

2325
attr_accessor :last_response
2426

@@ -229,10 +231,9 @@ def request(method, path, options)
229231
}.merge(options)
230232

231233
@last_response = HTTParty.send(method, uri, options)
232-
233-
case @last_response.code
234+
case last_response.code
234235
when HTTP_OK, HTTP_CREATED, HTTP_NO_CONTENT
235-
@last_response.parsed_response
236+
last_response.parsed_response
236237
when HTTP_BAD_REQUEST
237238
raise ValidationError, last_response.parsed_response["validationErrors"]
238239
when HTTP_FORBIDDEN
@@ -241,12 +242,15 @@ def request(method, path, options)
241242
raise NotFoundError
242243
when HTTP_INTERNAL_SERVER_ERROR
243244
raise InternalServerError
245+
when HTTP_SERVICE_UNAVAILABLE
246+
raise ServiceUnavailable
244247
when HTTP_TOO_MANY_REQUESTS
245248
retry_after = last_response.headers["Retry-After"]
246-
error_message = "Rate limit of 200 RPM or 12 POST/PUT/DELETE requests per 5 seconds reached. Next request possible in #{retry_after} seconds."
247-
raise TooManyRequestsError, error_message
249+
message = "Rate limit of 200 RPM or 12 POST/PUT/DELETE requests per 5 " +
250+
"seconds reached. Next request possible in #{retry_after} seconds."
251+
raise TooManyRequestsError, message
248252
else
249-
raise NotImplementedError, "Help Scout returned something that is not implemented by the help_scout gem yet: #{@last_response.code}: #{@last_response.parsed_response["message"] if @last_response.parsed_response}"
253+
raise NotImplementedError, "Help Scout returned something that is not implemented by the help_scout gem yet: #{last_response.code}: #{last_response.parsed_response["message"] if last_response.parsed_response}"
250254
end
251255
end
252256
end

spec/helpscout_spec.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,23 @@
7373
end
7474
end
7575

76+
context 'with a 503 status code' do
77+
it 'returns ServiceUnavailable' do
78+
url = 'https://api.helpscout.net/v1/conversations/1337.json'
79+
stub_request(:get, url).to_return(status: 503)
80+
81+
expect { client.get_conversation(1337) }.to raise_error(HelpScout::ServiceUnavailable)
82+
end
83+
end
84+
7685
context 'with a not implemented status code' do
7786
it 'returns a not implemented error' do
7887
data = { subject: "Help me!" }
7988

8089
url = 'https://api.helpscout.net/v1/conversations.json'
8190
stub_request(:post, url).
8291
to_return(
83-
status: 503,
92+
status: 402,
8493
)
8594

8695
expect { client.create_conversation(data) }.to raise_error(HelpScout::NotImplementedError)

0 commit comments

Comments
 (0)