A
A
Anton Misyagin2017-03-29 13:13:43
Ruby on Rails
Anton Misyagin, 2017-03-29 13:13:43

How to catch an email sending error?

Gemfile:
gem 'resque_mailer', '2.2.7' # Send mail asynchronously
controller:

def some
 MyMailer.send(parameter).deliver
rescue Net::SMTPAuthenticationError, Net::SMTPServerBusy, Net::SMTPSyntaxError, Net::SMTPFatalError, Net::SMTPUnknownError => e
#Здесь я могу отловить ошибку, но здесь мне не надо
end

mailer:
class MyMailer < ActionMailer::Base
  include Resque::Mailer
  def send param
     smtp_sets = LoadSmtpSets(1)
       mail 	subject: 'some', from: '[email protected]', delivery_method_options: smtp_sets
  rescue Net::SMTPAuthenticationError, Net::SMTPServerBusy, Net::SMTPSyntaxError, Net::SMTPFatalError, Net::SMTPUnknownError => e
       smtp_sets = LoadSmtpSets(2)
       mail 	subject: 'some', from: '[email protected]', delivery_method_options: smtp_sets
     #этот блок никогда не вызывается, а надо отловить ошибку, которую дает яндекс, к примеру
  end
end

Essence: I'm trying to send from one mailbox, a sending error is possible, then sending from another mail is called.
Problem: The rescue block is not called,
I can catch an error only from the controller. Now we have a bunch of controllers from which this letter is sent. I want to tie the processing of the sending error into the mailer code, not the controller.
The rescue block of the mailer fires, for example, if there is a syntax error in the mailer code or I forcibly raise some error, but does not fire on a send error.
Thanks in advance

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton Misyagin, 2017-04-01
@sunnmas

Custom handling of errors that arise when sending a message is possible by assigning a lambda to the error_handler attribute. There are two supported lambdas for backwards compatiability.
The first lamba will be deprecated in a future release:
Resque::Mailer.error_handler = lambda { |mailer, message, error|
# some custom error handling code here in which you optionally re-raise the error
}
The new lamba contains two other arguments, action and args, which allows mailers to be requeued on failure:
Resque::Mailer.error_handler = lambda { |mailer, message, error, action, args|
# Necessary to re-enqueue jobs that receive the SIGTERM signal
if error.is_a?(Resque::TermException)
Resque enqueue(mailer, action, *args)
else
raise error
end

A
Andrey Demidenko, 2017-03-29
@Dem1

config.action_mailer.raise_delivery_errors = true

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question