diff --git a/lib/mailgun/base.rb b/lib/mailgun/base.rb index 0811a7b..7dfbee6 100644 --- a/lib/mailgun/base.rb +++ b/lib/mailgun/base.rb @@ -102,11 +102,7 @@ def self.submit(method, url, parameters={}) :code => error_code || nil, :message => error_message || nil ) - if error.handle.kind_of? Mailgun::ErrorBase - raise error.handle - else - raise error - end + raise error.handle end end diff --git a/lib/mailgun/mailgun_error.rb b/lib/mailgun/mailgun_error.rb index 0644701..0d080b6 100644 --- a/lib/mailgun/mailgun_error.rb +++ b/lib/mailgun/mailgun_error.rb @@ -38,9 +38,6 @@ def handle end class NotFound < ErrorBase - def handle - return nil - end end class BadRequest < ErrorBase diff --git a/spec/base_spec.rb b/spec/base_spec.rb index df3f77e..dc2d1b0 100644 --- a/spec/base_spec.rb +++ b/spec/base_spec.rb @@ -67,6 +67,37 @@ Mailgun.submit :test_method, '/', :arg1=>"val1" end + + context "when the client raises a 400 http error" do + it "should re-raise a bad request mailgun error" do + client_error = Mailgun::ClientError.new + client_error.http_code = 400 + client_error.http_body = { "message" => "Expression is missing" }.to_json + expect(Mailgun::Client).to receive(:new) + .with('/') + .and_return(client_double) + expect(client_double).to receive(:test_method) + .with({:arg1=>"val1"}) + .and_raise(client_error) + + expect { Mailgun.submit :test_method, '/', :arg1=>"val1" }.to raise_exception Mailgun::BadRequest + end + end + context "when the client raises a 404 http error" do + it "should re-raise a not found mailgun error" do + client_error = Mailgun::ClientError.new + client_error.http_code = 404 + client_error.http_body = { "message" => "Not Found" }.to_json + expect(Mailgun::Client).to receive(:new) + .with('/') + .and_return(client_double) + expect(client_double).to receive(:test_method) + .with({:arg1=>"val1"}) + .and_raise(client_error) + + expect { Mailgun.submit :test_method, '/', :arg1=>"val1" }.to raise_exception Mailgun::NotFound + end + end end end