Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/mailgun_rails/deliverer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ def verify_ssl
end

def deliver!(rails_message)
response = mailgun_client.send_message build_mailgun_message_for(rails_message)
begin
response = mailgun_client.send_message build_mailgun_message_for(rails_message)
rescue RestClient::Exception => mailgun_error
json = JSON.parse(mailgun_error.http_body.to_s) rescue raise(mailgun_error)
mailgun_error.define_singleton_method(:message) { json["message"] }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that bad idea to monkey patch RestClient::* classes on the fly. It is foreign classes and this changes can be counterintuitive for devs, who use API of RestClient and they can wait documented behaviour.

For this case better to use own Exception classes.
In additional case I think about using of API from mailgun-ruby gem into this gem (mailgun-ruby gem already have implemented all required api calls and no reason why we should duplicate this logic).

raise mailgun_error
end
if response.code == 200
mailgun_message_id = JSON.parse(response.to_str)["id"]
rails_message.message_id = mailgun_message_id
Expand Down
18 changes: 18 additions & 0 deletions spec/lib/mailgun_rails/deliverer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,12 +115,30 @@
msg.message_id.should eq "20111114174239.25659.5817@samples.mailgun.org"
end

it "should include the error message returned from Mailgun in the Ruby exception message" do
msg = Mail::Message.new(to: [], cc: 'cc@email.com', from: 'from@email.com')
expectation = { to: [], cc: ['cc@email.com'], from: ['from@email.com']}
check_mailgun_error msg, expectation, RestClient::BadRequest, "'to' parameter is missing"
end

def check_mailgun_message(rails_message, mailgun_message)
rest_response = double(:code => 200, :to_str => '{"message": "Queued. Thank you.","id": "<20111114174239.25659.5817@samples.mailgun.org>"}')
mailgun_client.should_receive(:send_message).with(mailgun_message).and_return(rest_response)
MailgunRails::Deliverer.new(api_key: api_key, domain: domain).deliver!(rails_message)
end

def check_mailgun_error(rails_message, mailgun_message, exception, message)
body = %{{"message": #{JSON.dump message}}}
response = double(code: 400, body: body)
exception = exception.new(response)
allow(mailgun_client).to receive(:send_message).and_raise(exception)
expect {
MailgunRails::Deliverer.new(api_key: api_key, domain: domain).deliver!(rails_message)
}.to raise_exception(RestClient::BadRequest) {|exception|
expect(exception.message).to eq message
}
end

def rails_message_with_attachment
msg = basic_multipart_rails_message
msg.attachments["attachment.jpg"] = "\312\213\254\232"
Expand Down