Ruby on rails action mailer可以引发哪些异常
我查看了这个类,但没有看到rails 3中发送smtp电子邮件可能引发的异常列表Ruby on rails action mailer可以引发哪些异常,ruby-on-rails,exception,actionmailer,raise,Ruby On Rails,Exception,Actionmailer,Raise,我查看了这个类,但没有看到rails 3中发送smtp电子邮件可能引发的异常列表 有人知道吗?取决于您对如何发送邮件的设置。如果您通过smtp发送邮件,ActionMailer将使用。在那里,您将发现可能出现的错误 如果应用程序配置为使用sendmail,ActionMailer将使用 这篇关于thoughtbot的文章总结了所有可能的SMTP异常,并为您提供了一种相当优雅的方式来处理所有这些异常 以下是可能的例外情况: SMTP_SERVER_ERRORS = [TimeoutError,
有人知道吗?取决于您对如何发送邮件的设置。如果您通过smtp发送邮件,ActionMailer将使用。在那里,您将发现可能出现的错误
如果应用程序配置为使用
sendmail
,ActionMailer将使用 这篇关于thoughtbot的文章总结了所有可能的SMTP异常,并为您提供了一种相当优雅的方式来处理所有这些异常
以下是可能的例外情况:
SMTP_SERVER_ERRORS = [TimeoutError,
IOError,
Net::SMTPUnknownError,
Net::SMTPServerBusy,
Net::SMTPAuthenticationError]
SMTP_CLIENT_ERRORS = [Net::SMTPFatalError, Net::SMTPSyntaxError]
我们发现此列表对于您可能希望重试的标准错误非常有效:
[ EOFError,
IOError,
TimeoutError,
Errno::ECONNRESET,
Errno::ECONNABORTED,
Errno::EPIPE,
Errno::ETIMEDOUT,
Net::SMTPAuthenticationError,
Net::SMTPServerBusy,
Net::SMTPSyntaxError,
Net::SMTPUnknownError,
OpenSSL::SSL::SSLError
]
请注意,我没有包括
Net::SMTPFatalError
,因为这通常是一个永久性故障(如被列入黑名单的电子邮件地址)。根据您使用的交付方法,可能会出现更多错误。如果您通过aws SES gem使用Amazon SES服务,请将以下错误添加到阵列中
AWS::SES::ResponseError
您可以使用类似这样的代码来捕获错误
# some_utility_class.rb
# Return false if no error, otherwise returns the error
def try_delivering_email(options = {}, &block)
begin
yield
return false
rescue EOFError,
IOError,
TimeoutError,
Errno::ECONNRESET,
Errno::ECONNABORTED,
Errno::EPIPE,
Errno::ETIMEDOUT,
Net::SMTPAuthenticationError,
Net::SMTPServerBusy,
Net::SMTPSyntaxError,
Net::SMTPUnknownError,
AWS::SES::ResponseError,
OpenSSL::SSL::SSLError => e
log_exception(e, options)
return e
end
end
# app/controller/your_controller.rb
if @foo.save
send_email
...
private
def send_email
if error = Utility.try_delivering_email { MyMailer.my_action.deliver_now }
flash('Could not send email : ' + error.message)
end
end