Skip to content

Commit

Permalink
Fixes "TypeError" for all http 404 responses
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
melcher committed Dec 22, 2016
1 parent 5476618 commit 309e46d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
6 changes: 1 addition & 5 deletions lib/mailgun/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/mailgun/mailgun_error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def handle

class NotFound < ErrorBase
def handle
return nil
nil
end
end

Expand Down
31 changes: 31 additions & 0 deletions spec/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 309e46d

Please sign in to comment.