Skip to content
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

Add specific error class for 503s #13

Merged
merged 1 commit into from
Mar 29, 2018
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
16 changes: 10 additions & 6 deletions lib/help_scout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class NotFoundError < StandardError; end
class TooManyRequestsError < StandardError; end
class InternalServerError < StandardError; end
class ForbiddenError < StandardError; end
class ServiceUnavailable < StandardError; end

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

attr_accessor :last_response

Expand Down Expand Up @@ -229,10 +231,9 @@ def request(method, path, options)
}.merge(options)

@last_response = HTTParty.send(method, uri, options)

case @last_response.code
case last_response.code
when HTTP_OK, HTTP_CREATED, HTTP_NO_CONTENT
@last_response.parsed_response
last_response.parsed_response
when HTTP_BAD_REQUEST
raise ValidationError, last_response.parsed_response["validationErrors"]
when HTTP_FORBIDDEN
Expand All @@ -242,12 +243,15 @@ def request(method, path, options)
when HTTP_INTERNAL_SERVER_ERROR
error_message = JSON.parse(last_response.body)["error"]
raise InternalServerError, error_message
when HTTP_SERVICE_UNAVAILABLE
raise ServiceUnavailable
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
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, 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}"
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
end
end
11 changes: 10 additions & 1 deletion spec/helpscout_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,23 @@
end
end

context 'with a 503 status code' do
it 'returns ServiceUnavailable' do
url = 'https://api.helpscout.net/v1/conversations/1337.json'
stub_request(:get, url).to_return(status: 503)

expect { client.get_conversation(1337) }.to raise_error(HelpScout::ServiceUnavailable)
end
end

context 'with a not implemented status code' do
it 'returns a not implemented error' do
data = { subject: "Help me!" }

url = 'https://api.helpscout.net/v1/conversations.json'
stub_request(:post, url).
to_return(
status: 503,
status: 402,
)

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