【问题标题】:What exceptions can be raised by action mailer动作邮件程序可以引发哪些异常
【发布时间】:2012-02-29 22:52:20
【问题描述】:

我查看了课程,但没有看到在 rails 3 中传递 smtp 电子邮件可能引发的异常列表。

有人知道吗?

【问题讨论】:

    标签: ruby-on-rails exception actionmailer raise


    【解决方案1】:

    可能会出现更多错误,具体取决于您使用的交付方式。如果您通过 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
    

    【讨论】:

      【解决方案2】:

      我们发现此列表非常适合您可能想要重试的标准错误:

      [ EOFError,
      IOError,
      TimeoutError,
      Errno::ECONNRESET,
      Errno::ECONNABORTED,
      Errno::EPIPE,
      Errno::ETIMEDOUT,
      Net::SMTPAuthenticationError,
      Net::SMTPServerBusy,
      Net::SMTPSyntaxError,
      Net::SMTPUnknownError,
      OpenSSL::SSL::SSLError
      ]
      

      请注意,我没有包含 Net::SMTPFatalError,因为它通常是永久性故障(例如列入黑名单的电子邮件地址)。

      【讨论】:

        【解决方案3】:

        thoughtbot 上的这篇文章总结了所有可能的 SMTP 异常,并为您提供了一种相当优雅的方式来处理所有这些异常。

        http://robots.thoughtbot.com/post/159806037/i-accidentally-the-whole-smtp-exception

        以下是可能的例外情况:

        SMTP_SERVER_ERRORS = [TimeoutError,
                              IOError,
                              Net::SMTPUnknownError,
                              Net::SMTPServerBusy,
                              Net::SMTPAuthenticationError]
        
        SMTP_CLIENT_ERRORS = [Net::SMTPFatalError, Net::SMTPSyntaxError]
        

        【讨论】:

          【解决方案4】:

          取决于您对如何发送邮件的设置。如果您通过smtp 发送邮件,ActionMailer 使用Net::SMTP。在那里您会发现可能引发的错误。

          如果您的应用程序配置为使用sendmail,ActionMailer 将使用IO

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2012-12-08
            • 1970-01-01
            • 2014-01-15
            • 1970-01-01
            • 2012-04-29
            • 2017-08-22
            • 2017-07-16
            相关资源
            最近更新 更多