Skip to content

Commit b21c7fd

Browse files
committed
Properly handle 403 forbidden error
1 parent b721d4c commit b21c7fd

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

lib/sendgrid_toolkit/abstract_sendgrid_client.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ def api_post(module_name, action_name, opts = {})
1616
response = HTTParty.post("https://#{SendgridToolkit.base_uri}/#{base_path}.json",
1717
:body => get_credentials.merge(opts),
1818
:format => :json)
19-
if response.code > 401
19+
if response.code > 403
2020
raise(SendgridToolkit::SendgridServerError, "The SendGrid server returned an error. #{response.inspect}")
21-
elsif has_error?(response) and
22-
response['error'].respond_to?(:has_key?) and
23-
response['error'].has_key?('code') and
24-
response['error']['code'].to_i == 401
21+
elsif has_error?(response) &&
22+
response['error'].respond_to?(:has_key?) &&
23+
response['error'].has_key?('code') &&
24+
[401, 403].include?(response['error']['code'].to_i)
2525
raise SendgridToolkit::AuthenticationFailed
2626
elsif has_error?(response)
2727
raise(SendgridToolkit::APIError, response['error'])

spec/lib/sendgrid_toolkit/abstract_sendgrid_client_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@
1616
@obj.send(:api_post, "profile", "get", {})
1717
}.should raise_error SendgridToolkit::AuthenticationFailed
1818
end
19+
it "throws error when bad username or password presented" do
20+
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI}/stats\.getAdvanced\.json\?|, :body => '{"error":{"code":403,"message":"Bad username / password"}}', :status => ['403', 'Forbidden'])
21+
@obj = SendgridToolkit::AbstractSendgridClient.new("fakeuser", "fakepass")
22+
expect {
23+
@obj.send(:api_post, "stats", "getAdvanced", {})
24+
}.to raise_error SendgridToolkit::AuthenticationFailed
25+
end
1926
it "thows error when sendgrid response is a server error" do
2027
FakeWeb.register_uri(:post, %r|https://#{REGEX_ESCAPED_BASE_URI}/profile\.get\.json|, :body => '{}', :status => ['500', 'Internal Server Error'])
2128
@obj = SendgridToolkit::AbstractSendgridClient.new("someuser", "somepass")

0 commit comments

Comments
 (0)