Skip to content

Add TooManyRequests error for 429 #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/help_scout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class HelpScout
class ValidationError < StandardError; end
class NotImplementedError < StandardError; end
class NotFoundError < StandardError; end
class TooManyRequestsError < StandardError; end
class InternalServerError < StandardError; end

# Status codes used by Help Scout, not all are implemented in this gem yet.
Expand All @@ -14,6 +15,7 @@ class InternalServerError < StandardError; end
HTTP_NO_CONTENT = 204
HTTP_BAD_REQUEST = 400
HTTP_NOT_FOUND = 404
HTTP_TOO_MANY_REQUESTS = 429
HTTP_INTERNAL_SERVER_ERROR = 500

attr_accessor :last_response
Expand Down Expand Up @@ -206,6 +208,10 @@ def request(method, path, options)
raise NotFoundError
when HTTP_INTERNAL_SERVER_ERROR
raise InternalServerError
when HTTP_TOO_MANY_REQUESTS
retry_after = last_response.headers["Retry-After"]
error_message = "Rate limit of 200 RPM or 12 POST/PUT/DELETE requests per 5 seconds reached. Next request possible in #{retry_after} seconds."
raise TooManyRequestsError, error_message
else
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}"
end
Expand Down
16 changes: 16 additions & 0 deletions spec/helpscout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,20 @@
expect(req).to have_been_requested
end
end

describe 'general rate limiting error' do
it 'returns TooManyRequestsError' do
url = 'https://api.helpscout.net/v1/conversations/1337.json'
stub_request(:get, url).
to_return(
status: 429,
headers: {
retry_after: '10',
}
)

error_message = "Rate limit of 200 RPM or 12 POST/PUT/DELETE requests per 5 seconds reached. Next request possible in 10 seconds."
expect { client.get_conversation(1337) }.to raise_error(HelpScout::TooManyRequestsError, error_message)
end
end
end