From 309e46d1da8241acbed762c6e85b41ae8339c4a5 Mon Sep 17 00:00:00 2001 From: Graham Melcher Date: Wed, 21 Dec 2016 18:26:46 -0800 Subject: [PATCH] Fixes "TypeError" for all http 404 responses Prior to this PR, any `404` response from mailgun would cause a `TypeError: exception class/object expected` to be thrown from the mailgun gem. Now they return nil. --- lib/mailgun/base.rb | 6 +----- lib/mailgun/mailgun_error.rb | 2 +- spec/base_spec.rb | 31 +++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) diff --git a/lib/mailgun/base.rb b/lib/mailgun/base.rb index 0811a7b..cbd398c 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 if error.handle end end diff --git a/lib/mailgun/mailgun_error.rb b/lib/mailgun/mailgun_error.rb index 0644701..f9d4e03 100644 --- a/lib/mailgun/mailgun_error.rb +++ b/lib/mailgun/mailgun_error.rb @@ -39,7 +39,7 @@ def handle class NotFound < ErrorBase def handle - return nil + nil end end diff --git a/spec/base_spec.rb b/spec/base_spec.rb index df3f77e..3074c42 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 return nil instead of raising an exception" 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 be_nil + end + end end end