Skip to content

Commit

Permalink
Added server error handling for when sendgrid does not return a nice …
Browse files Browse the repository at this point in the history
…error message.

Status codes > 401 will raise a SendgridServerError
  • Loading branch information
James Brennan committed Jan 28, 2011
1 parent 63ef70b commit 83ab7f5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/sendgrid_toolkit/abstract_sendgrid_client.rb
Expand Up @@ -13,6 +13,7 @@ def initialize(api_user = nil, api_key = nil)

def api_post(module_name, action_name, opts = {})
response = HTTParty.post("https://#{BASE_URI}/#{module_name}.#{action_name}.json?", :query => get_credentials.merge(opts), :format => :json)
raise(SendgridToolkit::SendgridServerError, "The sengrid server returned an error. #{response.inspect}") if response.code > 401
raise SendgridToolkit::AuthenticationFailed if has_error?(response) && response['error'].has_key?('code') && response['error']['code'].to_i == 401
response
end
Expand Down
1 change: 1 addition & 0 deletions lib/sendgrid_toolkit/sendgrid_error.rb
Expand Up @@ -5,4 +5,5 @@ class NoAPIUserSpecified < StandardError; end
class UnsubscribeEmailAlreadyExists < StandardError; end
class UnsubscribeEmailDoesNotExist < StandardError; end
class EmailDoesNotExist < StandardError; end
class SendgridServerError < StandardError; end
end
21 changes: 14 additions & 7 deletions spec/lib/sendgrid_toolkit/abstract_sendgrid_client_spec.rb
Expand Up @@ -7,7 +7,7 @@
after do
restore_env
end

describe "#api_post" do
it "throws error when authentication fails" do
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/profile\.get\.json\?|, :body => '{"error":{"code":401,"message":"Permission denied, wrong credentials"}}')
Expand All @@ -16,37 +16,44 @@
@obj.send(:api_post, "profile", "get", {})
}.should raise_error SendgridToolkit::AuthenticationFailed
end
it "thows error when sendgrid response is an error" do
FakeWeb.register_uri(:post, %r|https://sendgrid\.com/api/profile\.get\.json\?|, :body => 'A server error occured', :status => ['500', 'Internal Server Error'])
@obj = SendgridToolkit::AbstractSendgridClient.new("someuser", "somepass")
lambda {
@obj.send(:api_post, "profile", "get", {})
}.should raise_error SendgridToolkit::SendgridServerError
end
end

describe "#initialize" do
it "stores api credentials when passed in" do
ENV['SMTP_USERNAME'] = "env_username"
ENV['SMTP_PASSWORD'] = "env_apikey"

@obj = SendgridToolkit::AbstractSendgridClient.new("username", "apikey")
@obj.instance_variable_get("@api_user").should == "username"
@obj.instance_variable_get("@api_key").should == "apikey"
end
it "resorts to environment variables when no credentials specified" do
ENV['SMTP_USERNAME'] = "env_username"
ENV['SMTP_PASSWORD'] = "env_apikey"

@obj = SendgridToolkit::AbstractSendgridClient.new()
@obj.instance_variable_get("@api_user").should == "env_username"
@obj.instance_variable_get("@api_key").should == "env_apikey"
end
it "throws error when no credentials are found" do
ENV['SMTP_USERNAME'] = nil
ENV['SMTP_PASSWORD'] = nil

lambda {
@obj = SendgridToolkit::AbstractSendgridClient.new()
}.should raise_error SendgridToolkit::NoAPIUserSpecified

lambda {
@obj = SendgridToolkit::AbstractSendgridClient.new(nil, "password")
}.should raise_error SendgridToolkit::NoAPIUserSpecified

lambda {
@obj = SendgridToolkit::AbstractSendgridClient.new("user", nil)
}.should raise_error SendgridToolkit::NoAPIKeySpecified
Expand Down

0 comments on commit 83ab7f5

Please sign in to comment.