Question

I'm trying to get exception_notification and rails to work together. I have looked at a bunch of other questions, and read through the ReadMe heavily on the exception_notification gem page. However, most other tutorials and questions all seem to be out of date by a year or so, before the major version switch to 4.0. What's extra weird is that the proper emails seem to be getting sent when in development mode, but NOT in production mode, which is where I really want them.

Here's the relevant code:

I installed with gem 'exception_notification' as it states in the ReadMe.

then... inside 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>'],
        }

As I said, when I copy/paste this into development.rb, it seems to work properly and send e-mails (though I'm not actually receiving them, Paperclip is catching them and opening them in the browser, and they have what I want in them). Then in production, nothing happens, and when I check the log files, it doesn't appear to be even attempting to send the e-mail. No errors received, except for the one I purposely throw.

I saw there was some issue related to SMTP stuff, so I also tried directly putting the smtp_settings directly into the email config hash of ExceptionNotification (while still having the same settings outside), but no luck with that.

I feel like I'm missing something silly. It's supposed to be dead simple to use. Any thoughts? Much thanks!

Was it helpful?

Solution

Ok, so the problem was that apparently previous devs (I inherited this project) had already set up a different kind of error catching that simply rendered different error pages. That function only ran in production, and it was blocking the call to exception_notification, which is why it just wasn't doing anything in production, but was working fine in development. So... all I did in the end was this...

In the app... this was already in the controllers/application_controller.rb file:

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

And the render_error function had some just well, rendered the error page. So I added in a step to set off the call to exception_notification like so...

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

OTHER TIPS

Please add in Gemfile gem 'exception_notification', '4.1.0.rc1' and in your production.rb file

      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 } 
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top