0

我正在尝试让 exception_notification 和 rails 一起工作。我查看了许多其他问题,并在exception_notification gem页面上大量阅读了自述文件。但是,在主要版本切换到 4.0 之前,大多数其他教程和问题似乎都已经过时了一年左右。更奇怪的是,在开发模式下似乎会发送正确的电子邮件,但不是在生产模式下,这是我真正想要的。

以下是相关代码:

gem 'exception_notification'按照自述文件中的说明安装了。

然后...在 config/environments/production.rb

Whatever::Application.configure do


  config.action_mailer.raise_delivery_errors = true

      config.action_mailer.delivery_method = :smtp

      config.action_mailer.smtp_settings = {
        :address              => "smtp.gmail.com",
        :port                 => 587,
        :user_name            => 'blah@blah.com',
        :password             => 'blahblah',
        :authentication       => 'plain',
        :enable_starttls_auto => true
      }

      config.action_mailer.perform_deliveries = true

      config.action_mailer.default_url_options = {
        :host => 'www.blah.com'
      }

      ActionMailer::Base.default :from => 'BlahApp <reply@blah.com>'

      Paperclip.options[:command_path] = "/usr/bin/"

      config.middleware.use ExceptionNotification::Rack,
        :email => {
          :email_prefix => "[Error!] ",
          :sender_address => %{<reply@blah.com>},
          :exception_recipients => ['"Test Recipient" <tester@blah.com>', '"Test 2" <test2@gmail.com>'],
        }

正如我所说,当我将它复制/粘贴到 development.rb 中时,它似乎可以正常工作并发送电子邮件(虽然我实际上并没有收到它们,但 Paperclip 正在捕捉它们并在浏览器中打开它们,它们有什么我想要他们)。然后在生产中,什么都没有发生,当我检查日志文件时,它似乎甚至没有尝试发送电子邮件。没有收到任何错误,除了我故意抛出的错误。

我看到有一些与 SMTP 相关的问题,所以我也尝试直接将 smtp_settings 直接放入 ExceptionNotification 的电子邮件配置哈希中(同时在外部仍然具有相同的设置),但没有运气。

我觉得我错过了一些愚蠢的东西。它应该非常简单易用。有什么想法吗?非常感谢!

4

2 回答 2

0

好的,所以问题是显然以前的开发人员(我继承了这个项目)已经设置了一种不同类型的错误捕获,它只是呈现不同的错误页面。该函数仅在生产中运行,并且它阻止了对 exception_notification 的调用,这就是为什么它在生产中没有做任何事情,但在开发中工作正常。所以……我最后做的就是这个……

在应用程序中......这已经在 controllers/application_controller.rb 文件中:

unless Rails.application.config.consider_all_requests_local
    #Commenting this would show user a blank page but also show a detailed error message. Don't do this
    #Unless absolutely necessary.
    rescue_from Exception, with: lambda { |exception| render_error 500, exception }
    rescue_from ActionController::RoutingError, ActionController::UnknownController, AbstractController::ActionNotFound, ActiveRecord::RecordNotFound, with: lambda { |exception| render_error 404, exception }
  end

并且 render_error 函数有一些恰到好处,呈现了错误页面。所以我添加了一个步骤来触发对 exception_notification 的调用,就像这样......

def render_error(status, exception)
    if (status != 404)
      ExceptionNotifier::Notifier
        .exception_notification(exception, options.merge(:env => env))
        .deliver
    end
    respond_to do |format|
      format.html { render template: "home/error_#{status}", layout: 'layouts/heropage', status: status }
      format.all { render nothing: true, status: status }
    end
  end
于 2014-03-13T03:43:10.310 回答
0

请添加 Gemfilegem 'exception_notification', '4.1.0.rc1'和您的 production.rb 文件

      config.consider_all_requests_local = false
      config.action_mailer.raise_delivery_errors = true
      config.action_mailer.perform_deliveries = true
      config.action_mailer.delivery_method = :smtp
      config.action_mailer.default_url_options{host:'http://yourUrl.herokuapp.com/'     
       }
     config.action_mailer.smtp_settings = {
     address: "smtp.gmail.com",
     port: 587,
     domain: 'gmail.com',
     authentication: "plain",
     enable_starttls_auto: true,
     user_name: "youremail@gmail.com",
     password: "Password"
   }

    config.middleware.use ExceptionNotification::Rack,
     :email => {
     :email_prefix => "[Notifer] ",
     :sender_address => %{"Exception Notification" < sender@domain.com>},
     :exception_recipients => %w{first@domian.com second@domain.com } 
    }
于 2015-05-15T08:34:02.453 回答