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

Properly handle 403 forbidden error #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions lib/sendgrid_toolkit/abstract_sendgrid_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def api_post(module_name, action_name, opts = {})
response = HTTParty.post("https://#{SendgridToolkit.base_uri}/#{base_path}.json",
:body => get_credentials.merge(opts),
:format => :json)
if response.code > 401
if response.code > 403
raise(SendgridToolkit::SendgridServerError, "The SendGrid server returned an error. #{response.inspect}")
elsif has_error?(response) and
response['error'].respond_to?(:has_key?) and
response['error'].has_key?('code') and
response['error']['code'].to_i == 401
elsif has_error?(response) &&
response['error'].respond_to?(:has_key?) &&
response['error'].has_key?('code') &&
[401, 403].include?(response['error']['code'].to_i)
raise SendgridToolkit::AuthenticationFailed
elsif has_error?(response)
raise(SendgridToolkit::APIError, response['error'])
Expand Down
7 changes: 7 additions & 0 deletions spec/lib/sendgrid_toolkit/abstract_sendgrid_client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
@obj.send(:api_post, "profile", "get", {})
}.should raise_error SendgridToolkit::AuthenticationFailed
end
it "throws error when bad username or password presented" do
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI}/stats\.getAdvanced\.json\?|, :body => '{"error":{"code":403,"message":"Bad username / password"}}', :status => ['403', 'Forbidden'])
@obj = SendgridToolkit::AbstractSendgridClient.new("fakeuser", "fakepass")
expect {
@obj.send(:api_post, "stats", "getAdvanced", {})
}.to raise_error SendgridToolkit::AuthenticationFailed
end
it "thows error when sendgrid response is a server error" do
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI}/profile\.get\.json|, :body => '{}', :status => ['500', 'Internal Server Error'])
@obj = SendgridToolkit::AbstractSendgridClient.new("someuser", "somepass")
Expand Down