3

I use exception_notification gem for handling an errors in an app. My ApplicationController looks like this:

unless Rails.application.config.consider_all_requests_local
    rescue_from Exception,
                :with => :render_error
    rescue_from ActiveRecord::RecordNotFound,
                :with => :render_not_found
    rescue_from ActionController::RoutingError,
                :with => :render_not_found
    rescue_from ActionController::UnknownController,
                :with => :render_not_found
    rescue_from ActionController::UnknownAction,
                :with => :render_not_found
  end

  def render_not_found(exception)
    ExceptionNotifier::Notifier
      .exception_notification(request.env, exception)
      .deliver
    render :template => "/errors/404.html.erb",
      :layout => 'errors.html.erb'
    return     
  end

  def render_error(exception)
    ExceptionNotifier::Notifier
      .exception_notification(request.env, exception)
      .deliver
    render :template => "/errors/500.html.erb",
           :layout => 'errors.html.erb'
    return
  end

In /config/enviroments/productions.rg in the end of the file I have:

config.middleware.use ExceptionNotifier,
  :email_prefix => "[MY APP| Error Report] ",
  :sender_address => %{"MY APP" <[email protected]>},
  :exception_recipients => '[email protected]'
end

when I get the error on the app - eg. Article.find(not-existing-ID), I'll get the standard error page (ERROR 500) from /public/500.html and not from the file specified in application controller... How is that possible? Past hours I tried to find the problem, but I still don't know the issue.

2 Answers 2

3

This works for me - from application_controller.rb:

unless Rails.application.config.consider_all_requests_local
  rescue_from Exception, with: :render_500
  rescue_from ActionController::RoutingError, with: :render_404
  rescue_from ActionController::UnknownController, with: :render_404
  rescue_from ActionController::UnknownAction, with: :render_404
  rescue_from ActiveRecord::RecordNotFound, with: :render_404
end

and

  private
  def render_404(exception)
    ExceptionNotifier::Notifier.exception_notification(request.env, exception,
      :data => {:User => current_user.full_name, :Email => current_user.email, :UserID => current_user.id}).deliver
    @not_found_path = exception.message
    respond_to do |format|
      format.html { render template: 'pages/404', layout: 'layouts/application', status: 404 }
      format.all { render nothing: true, status: 404}
    end
  end
  def render_500(exception)
    ExceptionNotifier::Notifier.exception_notification(request.env, exception,
      :data => {:User => current_user.full_name, :Email => current_user.email, :UserID => current_user.id}).deliver
    @error = exception
    respond_to do |format|
      format.html { render template: 'pages/500', layout: 'layouts/application', status: 500 }
      format.all { render nothing: true, status: 500}
    end
  end

Note: I have my custom error pages in /app/views/pages. Called 500.html.haml and 404.html.haml

I also have this in my routes.rb (note a hash after 'pages rather than a forward-slash):

  unless Rails.application.config.consider_all_requests_local
    match '*not_found', to: 'pages#404'
  end

PS. See updated instructions here: http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages

Sign up to request clarification or add additional context in comments.

Comments

0

Hi this works for me try it!

In the application_controller.rb you can add these codes:

 if Rails.env.production?
  unless Rails.application.config.consider_all_requests_local
    rescue_from Exception, with: :render_500
    rescue_from ActionController::RoutingError, with: :render_404
    rescue_from ActionController::UnknownController, with: :render_404
    rescue_from ActionController::UnknownAction, with: :render_404
    rescue_from ActiveRecord::RecordNotFound, with: :render_404
  end
 end

This codes checks if you are running your rails app using production mode and catches different rescue errors.

In the same controller "application_controller.rb" add these codes:

def render_404(exception)
    @not_found_path = exception.message
    respond_to do |format|
      format.html { render template: 'errors/not_found', layout: 'layouts/application', status: 404 }
      format.all { render nothing: true, status: 404 }
    end
  end

  def render_500(exception)
    logger.info exception.backtrace.join("\n")
    respond_to do |format|
      format.html { render template: 'errors/internal_server_error', layout:      'layouts/application', status: 500 }
      format.all { render nothing: true, status: 500}
    end
  end

These methods will specify the page you want to display when your app catches an error, then create a controller named: errors_controller.rb then in your app/view/errors create a file named:

  • internal_server_error.html.erb and
  • not_found.html.erb

and in your routes.rb add these codes:

 match '/internal_server_error', :to => 'errors#internal_server_error'
 match '/not_found', :to => 'errors#not_found'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.