Skip to content

Commit 309e46d

Browse files
committed
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.
1 parent 5476618 commit 309e46d

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

lib/mailgun/base.rb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,11 +102,7 @@ def self.submit(method, url, parameters={})
102102
:code => error_code || nil,
103103
:message => error_message || nil
104104
)
105-
if error.handle.kind_of? Mailgun::ErrorBase
106-
raise error.handle
107-
else
108-
raise error
109-
end
105+
raise error.handle if error.handle
110106
end
111107
end
112108

lib/mailgun/mailgun_error.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def handle
3939

4040
class NotFound < ErrorBase
4141
def handle
42-
return nil
42+
nil
4343
end
4444
end
4545

spec/base_spec.rb

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,37 @@
6767

6868
Mailgun.submit :test_method, '/', :arg1=>"val1"
6969
end
70+
71+
context "when the client raises a 400 http error" do
72+
it "should re-raise a bad request mailgun error" do
73+
client_error = Mailgun::ClientError.new
74+
client_error.http_code = 400
75+
client_error.http_body = { "message" => "Expression is missing" }.to_json
76+
expect(Mailgun::Client).to receive(:new)
77+
.with('/')
78+
.and_return(client_double)
79+
expect(client_double).to receive(:test_method)
80+
.with({:arg1=>"val1"})
81+
.and_raise(client_error)
82+
83+
expect { Mailgun.submit :test_method, '/', :arg1=>"val1" }.to raise_exception Mailgun::BadRequest
84+
end
85+
end
86+
context "when the client raises a 404 http error" do
87+
it "should return nil instead of raising an exception" do
88+
client_error = Mailgun::ClientError.new
89+
client_error.http_code = 404
90+
client_error.http_body = { "message" => "Not Found" }.to_json
91+
expect(Mailgun::Client).to receive(:new)
92+
.with('/')
93+
.and_return(client_double)
94+
expect(client_double).to receive(:test_method)
95+
.with({:arg1=>"val1"})
96+
.and_raise(client_error)
97+
98+
expect(Mailgun.submit :test_method, '/', :arg1=>"val1").to be_nil
99+
end
100+
end
70101
end
71102
end
72103

0 commit comments

Comments
 (0)