postmark-rails: How to we catch and handle Postmark::InvalidMessageError

I noticed in my error log today a number of these:

Postmark::InvalidMessageError
You tried to send to a recipient that has been marked as inactive. Found inactive addresses:

Since we don’t do anything special except use Rails 2.3’s normal ActiveMailer for sending mail, how do we capture and act on these errors that the Gem is returning?

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 16 (3 by maintainers)

Most upvoted comments

@davidpelaez @temochka Is there a way to capture these errors in the application_controller.rb rather than putting a begin-recue at every e-mail call? That just feels clunky to me.

I tried

 rescue_from Postmark::InvalidMessageError do |exception|
    NotificationMailer.postmark_issue(false, exception).deliver
    render nothing: true, status: 200
 end

but this doesn’t seem to work. I am still getting 500 errors in my newrelic logs.

Maybe what you are asking for is something like this:

            puts "Trying to send notification"
            begin
                XMailer.deliver_notifications_for(self)
            rescue Postmark::InvalidMessageError
                puts "An exception was raised while trying to notify. However this was rescued."
                return true
            end

Sorry to comment on such an old issue, but in my case some Postmark::InactiveRecipientError exceptions started blowing up my Sentry error reports while I was testing Rebound with an intentional hard bounce. I send my emails in a background job with Sidekiq, so it would keep retrying the job for the next few days if I didn’t delete it manually.

To prevent this from happening in the future, I ignored the exception in my Raven configuration:

# config/initializers/_sentry.rb
Raven.configure do |config|
  config.excluded_exceptions << 'Postmark::InactiveRecipientError'
end

UPDATE: Actually this just prevents the error from making it to Sentry, but the Sidekiq jobs will continue being retried until they get stale.

This rescue_from fix looks like the right way to go.

LAST UPDATE: The wiki has a section about error handling. I should have read that first!

Has anyone got this working along with DelayedJob? I’d like to mark invitations sent to failing emails as so and we are only sending emails through background processes … I think that DelayedJob exception handling must be patched, but I cannot find any suitable solution out there.

Thanks!

I’m not sure what your question is about. You can handle these errors the same way you would handle any other exception in Ruby. Wrap your #deliver call with begin/rescue and then log them somewhere, reactivate automatically or do whatever you need. You can introduce a new BaseMailer class and override ActionMailer::Base#deliver method there. Postmark::InvalidMessageError just means that message wasn’t delivered to recipient, so it should be processed as any other delivery error. You can also configure ActionMailer not to raise any delivery errors if that’s what you need:

config.action_mailer.raise_delivery_errors = false

Let me know if I can provide any other help on this.