Question

I want to be able to define the exception_recipients dynamically, based on the Rails environment. For example:

recipients = Rails.env == 'production'
   exceptions@myapp.com
else
   User.current.email
end

However, from the docs:

Whatever::Application.config.middleware.use ExceptionNotification::Rack,
  :email => {
    :email_prefix => "[Whatever] ",
    :sender_address => %{"notifier" <notifier@example.com>},
    :exception_recipients => %w{exceptions@example.com}
  }

In config/environments/production.rb where i don't have an ActiveRecord::Base connection yet.

How can I set the exceptions recipients after Rails has loaded?

Thanks

Was it helpful?

Solution

Custom Notifier

You can create a custom notifier, which inherits from the EmailNotifier, which will use User.current.email in the non-production environments.

# app/models/exception_notifier/custom_notifier.rb
#
module ExceptionNotifier
  class CustomNotifier < EmailNotifier

    def initialize(options)
      @fallback_exception_recipients = options[:fallback_exception_recipients]
      options[:exception_recipients] ||= options[:fallback_exception_recipients]
      super(options)
    end

    def call(exception, options = {})
      options[:exception_recipients] = [User.current.email] unless Rails.env.production?
      super(exception, options)
    end

  end
end

Initializer

The fallback address can, for example, be passed from the initializer.

# config/initializers/exception_notification.rb
#
Rails.application.config.middleware.use ExceptionNotification::Rack, {
  :custom => {
    :fallback_exception_recipients => %w{exceptions@myapp.com},
    # ...
  }
}

current_user instead of User.current

I'm not sure whether your User.current call will work here. But, you pass the current_user to the exception data as shown in the README.

# app/controllers/application_controller.rb
#
class ApplicationController < ActionController::Base
  before_filter :prepare_exception_notifier
  private
  def prepare_exception_notifier
    request.env["exception_notifier.exception_data"] = {
      :current_user => current_user
    }
  end
end

Then, replace the above ExceptionNotifier::CustomNotifier#call method with this one:

# app/models/exception_notifier/custom_notifier.rb
#
module ExceptionNotifier
  class CustomNotifier < EmailNotifier

    # ...

    def call(exception, options = {})
      unless Rails.env.production?
        if current_user = options[:env]['exception_notifier.exception_data'][:current_user]
          options[:exception_recipients] = [current_user.email]
        end
      end
      super(exception, options)
    end

  end
end

OTHER TIPS

This is not supported out of the box. You might be able to create your own custom notifier as described in the docs : https://github.com/smartinez87/exception_notification#custom-notifier

You can look at the built in EmailNotifier and potentially extend it and just override either the initialize or compose_email methods to make it do what you need.

https://github.com/smartinez87/exception_notification/blob/master/lib/exception_notifier/email_notifier.rb#L120

https://github.com/smartinez87/exception_notification/blob/master/lib/exception_notifier/email_notifier.rb#L90

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top