Brad Rice
Brad Rice

Reputation: 1344

ExceptionNotifier constant uninitialized Rails 3

I've read over many questions on this site as well as the GitHub documentation for ExceptionNotifier and its implementation in Rails 3, and I can't figure out why it won't work for me on my production server. On my local machine (a Mac), it works perfectly in the production environment. But after I deploy with capistrano and run the app using unicorn on a Linux box, my unicorn log complains about an uninitialized constant ExceptionNotifier in my environments/production.rb file, even though my local machine seems to think it's fine. I've tried with both implementations: gem 'exception_notification', :require => 'exception_notifier' and gem 'exception_notification_rails3', :require => 'exception_notifier'. Both are throwing this error. Here's my production.rb

Rl4::Application.configure do
  # code omitted
  # Enable ExceptionNotifier, having it ignore its default exceptions
  config.middleware.use ExceptionNotifier,
    sender_address: '[email protected]',
    exception_recipients: '[email protected]',
    ignore_exceptions: ExceptionNotifier.default_ignore_exceptions
end

Here's my Gemfile

source 'https://rubygems.org'
# lots omitted
gem 'exception_notification_rails3', :require => 'exception_notifier'
# lots more omitted

And here's my application_controller.rb, which implements the gem:

class ApplicationController < ActionController::Base
  protect_from_forgery

  # By default, only production ENV considers requests not local
  unless Rails.application.config.consider_all_requests_local
    rescue_from Exception, :with => :server_error
    rescue_from ActionController::UnknownAction, :with => :not_found
    rescue_from ActionController::UnknownController, :with => :not_found
    rescue_from ActionView::MissingTemplate, :with => :not_found
  end

  private
    def server_error(exception)
      ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver
      respond_to do |format|
        format.html { render template: '/500.html', layout: 'application/layout', status: 500 }
        format.all { render nothing: true, status: 500 }
      end
    end

    def not_found(exception)
      ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver
      respond_to do |format|
        format.html { render template: '/404.html', layout: 'application/layout', status: 404 }
        format.all { render nothing: true, status: 404 }
      end
    end
end

Again, the implementation isn't my problem at the moment. Somehow, the gem isn't installing correctly or it's not being initialized properly. I did run a sudo gem install on the Linux box for both versions of the gem, as well as 'exception_notifier'. No dice. Thoughts?

Additional info I just tried to reinstall the gem as a plug-in, and got the same errors after changing the production.rb file to use config.middleware.use ::ExceptionNotifier instead. However, those modifications still worked on my local machine. Maybe I'm missing another piece of software on the Linux box? Nothing seems to be mentioned on the documentation...

Request for gem version

I'm using exception_notification, v. 3.0.1

Request for full stacktrace

I, [2013-04-24T17:34:44.113788 #16877]  INFO -- : Refreshing Gem list
E, [2013-04-24T17:34:46.562588 #16877] ERROR -- : uninitialized constant ExceptionNotifier (NameError)
/home/deployer/rl4/releases/20130424201053/config/environments/production.rb:80:in `block in <top (required)>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:24:in `class_eval'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:24:in `configure'
/home/deployer/rl4/releases/20130424201053/config/environments/production.rb:1:in `<top (required)>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/engine.rb:571:in `block in <class:Engine>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `instance_exec'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:30:in `run'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:55:in `block in run_initializers'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `each'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/initializable.rb:54:in `run_initializers'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/application.rb:136:in `initialize!'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/railties-3.2.11/lib/rails/railtie/configurable.rb:30:in `method_missing'
/home/deployer/rl4/releases/20130424201053/config/environment.rb:5:in `<top (required)>'
config.ru:4:in `require'
config.ru:4:in `block in <main>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `instance_eval'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/rack-1.4.5/lib/rack/builder.rb:51:in `initialize'
config.ru:1:in `new'
config.ru:1:in `<main>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn.rb:44:in `eval'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn.rb:44:in `block in builder'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:722:in `call'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:722:in `build_app!'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:595:in `init_worker_process'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:615:in `worker_loop'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:500:in `spawn_missing_workers'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:511:in `maintain_worker_count'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/lib/unicorn/http_server.rb:277:in `join'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/gems/unicorn-4.6.2/bin/unicorn:126:in `<top (required)>'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/bin/unicorn:19:in `load'
/home/deployer/rl4/shared/bundle/ruby/1.9.1/bin/unicorn:19:in `<main>'

Upvotes: 3

Views: 1858

Answers (2)

Brad Rice
Brad Rice

Reputation: 1344

It shames me to say that my fix for this problem was so trivial.

$ cap unicorn:stop
$ cap unicorn:start

My restart task in my deployment process was never actually stopping the instance of the server, so the new libraries weren't being loaded, and thus, the exceptions.

Ladies and gents, don't forget to try turning it off and back on again. Ever. Thanks to everyone who helped me through this problem.

Upvotes: 1

TheAshwaniK
TheAshwaniK

Reputation: 1826

Here is how you can solve it.

Considring you already have SMTP setting in your development and production.rb

Gemfile

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

application.rb

config.middleware.use ExceptionNotifier,
  :email_prefix => "[ERROR]",
  :sender_address => %{"Exception Notifier" <[email protected]>},
  :exception_recipients => %w{[email protected]}

application_controller.rb

#render 500 error 
def render_error(exception)
  ExceptionNotifier::Notifier.exception_notification(request.env, exception).deliver
  respond_to do |f| 
    ...
  end
end

For me it works both in development and production(Heroku).

Hope it will help you as well.
Cheers:)

Upvotes: 1

Related Questions