开发者

Exception Notification Gem and Rails 3

开发者 https://www.devze.com 2023-01-12 20:00 出处:网络
I\'m trying to get this up and running, but I see \"uninitialized constant ExceptionNotifier\" whenever I start my server.

I'm trying to get this up and running, but I see "uninitialized constant ExceptionNotifier" whenever I start my server.

开发者_如何学C

http://github.com/rails/exception_notification

In my Gemfile I have

gem "exception_notification", :git => "http://github.com/rails/exception_notification.git", :branch => "master"

I've tried putting the configuration as shown in the github readme inside of config/application.rb, config/environment.rb, and config.ru. I replaced "Whatever" with my application name.


All previous answers are outdated, you can now simply add this to your gemfile:

gem 'exception_notification', :require => 'exception_notifier'

And edit your production.rb config file as indicated in the readme:

config.middleware.use ExceptionNotifier,
  :email_prefix => "[Exception] ",
  :sender_address => %{"Exception Notifier" <support@example.com>},
  :exception_recipients => %w{you@me.com}


Latest version of official gem works with Rails 3, you can find it here: https://github.com/smartinez87/exception_notification.

Next gem release will make the :require => 'exception_notifier' option unnecessary.


Ok, its working now for me:

# Gemfile
gem "exception_notification", :git => "git://github.com/rails/exception_notification", :require => 'exception_notifier'

# application.rb, inside the config block
config.middleware.use ::ExceptionNotifier,
  :email_prefix => "ApplicationName-Errors: ",
  :sender_address => %w{office@application.com},
  :exception_recipients => %w{office@application.com}


Keep it simple silly

In gemfile

gem 'exception_notification', :require => 'exception_notifier'

In application.rb file

  config.middleware.use ExceptionNotifier,
 :email_prefix => "[ERROR] ",
 :sender_address => %{"Exception Notifier" <Dummy_email@exapmle.com>},
 :exception_recipients => %w{Dummy_email@example.com}

Your are Done.. :*


It seems that Rails 3 can't use this plugin in gem form. Maybe rack apps can't be loaded from gems? I installed it as a plugin instead and changed the config syntax to:

config.middleware.use "::ExceptionNotifier"

instead of

config.middleware.use ExceptionNotifier


The official repo on github is now: https://github.com/smartinez87/exception_notification

In the Gemfile

gem "exception_notification", :require => 'exception_notifier', :git => "https://github.com/smartinez87/exception_notification.git"

In config\initializers\exception_notification.rb

Rails.application.config.middleware.use ExceptionNotifier,
  :email_prefix => "[Whatever] ",
  :sender_address => %{"notifier" <notifier@example.com>},
  :exception_recipients => %w{exceptions@example.com}  


Actually, now, it is much easier. In your Gemfile you need to write:

gem "exception_notification", :git => "http://github.com/rails/exception_notification.git", :require => 'exception_notifier'

And all should be fixed. The :require option is crucial (i guess because the names differ you have to specify explicitly). All other patches mentioned before have been merged i presume.


I was able to get it to work with the following in production.rb:

config.after_initialize do
 config.middleware.use ExceptionNotifier,
      :email_prefix => "[Whatever] ",
      :sender_address => %{"notifier" <notifier@example.com>},
      :exception_recipients => %w{exceptions@example.com}
end


https://github.com/smartinez87/exception_notification

This gem has been updated for rails 3.x and I just tested on 3.0.7 and the installation is much simpler.

Gemfile:

gem 'exception_notification'

Initializer:

Rails.application.config.middleware.use ExceptionNotifier,
  :email_prefix => "[Whatever] ",
  :sender_address => %{"notifier" <notifier@example.com>},
  :exception_recipients => %w{exceptions@example.com}


If you are doing a rescue_from Exception, with: :render_500 to handle showing a 500 template/page it no longer sends an email with just this

    config.middleware.use ExceptionNotifier,
  :email_prefix => "[some prefix] ",
  :sender_address => %{"Notifier" <notify@domain.com>},
  :exception_recipients => %w{recipient@domain.com}

You'll need to manually send it in the method that handles the exception

def render_500(exception)
    # email an error email if there's a 500 in production mode
    if Rails.env.production?
        ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver
    end
end 

So put the config stuff in your environment (production.rb) and the Exception handling code in your application controller.


I had the same problem just now and resolved it this way:

Gemfile

source 'http://rubygems.org'
gem 'exception_notification_rails3', :require => 'exception_notifier'

application.rb

config.middleware.use ExceptionNotifier,
  :email_prefix => "[some prefix] ",
  :sender_address => %{"Notifier" <notify@domain.com>},
  :exception_recipients => %w{recipient@domain.com}

I'm refactoring a Rails 2.3 project to 3.0, so I haven't tried this on a fresh install.

Edit:

It might actually be better (or "more correct") to put the ExceptionNotifier initialization in a separate initializer file in config/initializers/ instead of application.rb.

config/initializers/exception_notifier.rb

MyApp::Application.config.middleware.use ExceptionNotifier,
  :email_prefix => "[some prefix] ",
  :sender_address => %{"Notifier" <notify@domain.com>},
  :exception_recipients => %w{recipient@domain.com}


Using Rails 3.0.3 this works for me:

gem "exception_notification", :git => "https://github.com/sickill/exception_notification.git", :require => 'exception_notifier'

:git part is imported because its a patched version to get around the 'undefined method controller_name error' and :require to require the right lib.

Then in my production.rb environment file i only have this (from the manual)

  config.middleware.use ExceptionNotifier,
    :email_prefix => "[MyApp] ",
    :sender_address => %{"notifier" <email@example.com>},
    :exception_recipients => %w{email@example.com}

Seems like there are many different ways to get this to work, but this was my way.

Cheers!


It took a bit of work but I got Exception Notifier working with Rails 3.0.0:

1- rails plugin install http://github.com/sickill/exception_notification.git

(If you don't want to use this fork, just apply his patch manually to the original Rails plugin: it is only 3 lines.) It fixes the 'undefined method controller_name error'

2- In application.rb:

config.middleware.use "::ExceptionNotifier" , :email_prefix => "[Whatever] ",
                           :sender_address => %{"notifier" <notifier@example.com>},
                           :exception_recipients => %w{whoever@example.com} 

3- Apply Lawrence Pit's patch. (UPDATE: This link appears to be broken) It fixes the uninitialized constant ActiveRecord::RecordNotFound error as documented here.

That's it.


till now ( 2012-Aug-03) the official site is : https://github.com/smartinez87/exception_notification, and according the README, it support Rails3 perfectly.

step1. edit your Gemfile:

gem 'exception_notification'

step2.

As of Rails 3 ExceptionNotification is used as a rack middleware, so you can configure its options on your config.ru file, or in the environment you want it to run. In most cases you would want ExceptionNotification to run on production. You can make it work by

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


I am using rails 3.0.4 and had the same issue as above. The only solution that worked for me was to install the v1.2 of the exception_notification for rails 3 (make sure you use the correct branch/version) as a PLUGIN

rails plugin install https://github.com/railsware/exception_notification.git

and use in production.rb the code everyone mentions:

config.middleware.use ExceptionNotifier,
  :email_prefix => "[some prefix] ",
  :sender_address => %{"Notifier" <notify@domain.com>},
  :exception_recipients => %w{recipient@domain.com}

It definitely did not work for me as a gem and the readme does say " Exception Notifier Plugin for Rails " and mentions nothing about installing it as gem.

Harry


UPDATED ANSWER as of 3/14...

You just need to do gem exception_notification in your gem file. No 'require' needed.

Also, other changes just straight from the docs...

"As of 4.x version the configuration syntax has changed. All email related options MUST BE nested under the :email key."

like so...

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


I copied and pasted the exception_notification config from old app to new one and it failed. It brought me here and none of the above answers were up-to-date. Since 4.x version the middleware was renamed to ExceptionNotification::Rack, so middleware config looks like that:

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

精彩评论

暂无评论...
验证码 换一张
取 消