Search code examples
ruby-on-railsexceptionactionmailersidekiqrails-activejob

Rails ActiveJob - What's the good way to handle exception in ActionMailer::DeliveryJob


I am using ActiveJob + Sidekiq in my Rails project for task processing.

I send my mails directly using MyMailer.some.deliver_later. It will automatically creates a ActionMailer::DeliveryJob task and put it in the Sidekiq queue.

The question is, what's the good to handle exceptions from there?

Best Regards.


Solution

  • According to http://edgeguides.rubyonrails.org/active_job_basics.html, I think the good way is to setup exception error handlers for ActionMailer::DeliveryJob in an initializer, somethinglike:

    ActionMailer::DeliveryJob.rescue_from(Net::SMTPSyntaxError) do |exception|
      unless ['501 Command parsing failed'].include?(exception.message.strip)
        raise exception
      end
    end