Skip to content

Commit 054c35c

Browse files
committed
Pass on specific error message returned from Mailgun
If Mailgun raises a RestClient::BadRequest "400 Bad Request" error, for example, there is also probably (always?) the specific error message (for example, "'to' parameter is missing") from Mailgun embedded in the JSON response. However, there was no easy way to see what that specific error message actually was, and you'd end up with a BadRequest error from your Rails app that you had no idea the actual cause of. This change causes it to automatically extract that specific message from the JSON response and use that as the Ruby exception message.
1 parent ebcaaa3 commit 054c35c

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/mailgun_rails/deliverer.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ def verify_ssl
2121
end
2222

2323
def deliver!(rails_message)
24-
response = mailgun_client.send_message build_mailgun_message_for(rails_message)
24+
begin
25+
response = mailgun_client.send_message build_mailgun_message_for(rails_message)
26+
rescue Exception => mailgun_error
27+
json = JSON.parse(mailgun_error.http_body.to_s) rescue raise(mailgun_error)
28+
mailgun_error.define_singleton_method(:message) { json["message"] }
29+
raise mailgun_error
30+
end
2531
if response.code == 200
2632
mailgun_message_id = JSON.parse(response.to_str)["id"]
2733
rails_message.message_id = mailgun_message_id

spec/lib/mailgun_rails/deliverer_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,30 @@
115115
msg.message_id.should eq "20111114174239.25659.5817@samples.mailgun.org"
116116
end
117117

118+
it "should include the error message returned from Mailgun in the Ruby exception message" do
119+
msg = Mail::Message.new(to: [], cc: 'cc@email.com', from: 'from@email.com')
120+
expectation = { to: [], cc: ['cc@email.com'], from: ['from@email.com']}
121+
check_mailgun_error msg, expectation, RestClient::BadRequest, "'to' parameter is missing"
122+
end
123+
118124
def check_mailgun_message(rails_message, mailgun_message)
119125
rest_response = double(:code => 200, :to_str => '{"message": "Queued. Thank you.","id": "<20111114174239.25659.5817@samples.mailgun.org>"}')
120126
mailgun_client.should_receive(:send_message).with(mailgun_message).and_return(rest_response)
121127
MailgunRails::Deliverer.new(api_key: api_key, domain: domain).deliver!(rails_message)
122128
end
123129

130+
def check_mailgun_error(rails_message, mailgun_message, exception, message)
131+
body = %{{"message": #{JSON.dump message}}}
132+
response = double(code: 400, body: body)
133+
exception = exception.new(response)
134+
allow(mailgun_client).to receive(:send_message).and_raise(exception)
135+
expect {
136+
MailgunRails::Deliverer.new(api_key: api_key, domain: domain).deliver!(rails_message)
137+
}.to raise_exception(RestClient::BadRequest) {|exception|
138+
expect(exception.message).to eq message
139+
}
140+
end
141+
124142
def rails_message_with_attachment
125143
msg = basic_multipart_rails_message
126144
msg.attachments["attachment.jpg"] = "\312\213\254\232"

0 commit comments

Comments
 (0)