Skip to content

Pass on specific error message returned from Mailgun#71

Open
TylerRick wants to merge 1 commit intojorgemanrubia:masterfrom
TylerRick:pass_on_specific_mailgun_error_message
Open

Pass on specific error message returned from Mailgun#71
TylerRick wants to merge 1 commit intojorgemanrubia:masterfrom
TylerRick:pass_on_specific_mailgun_error_message

Conversation

@TylerRick
Copy link
Contributor

@TylerRick TylerRick commented Oct 17, 2016

When the Mailgun API returns a "400 Bad Request" error, it includes the specific error message (for example, "'to' parameter is missing") from 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 generic (and therefore useless) RestClient::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.

@TylerRick
Copy link
Contributor Author

Since a picture is worth a 800 words, here's what the error looked like before:

image

and here's how the same error shows up now:

image

When the Mailgun API returns a "400 Bad Request" error, it includes the specific error message (for
example, "'to' parameter is missing") from 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 generic (and therefore useless) RestClient::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.
@TylerRick TylerRick force-pushed the pass_on_specific_mailgun_error_message branch from 054c35c to b7338ad Compare October 17, 2016 22:01
@TylerRick
Copy link
Contributor Author

Does this look okay? Anything I need to do to get it merged in?

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants