Exception Notice - How do I display my own error pages?

I am using exception_notification gem to handle errors in the application. My ApplicationController is as follows:

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 at the end of the file I have:

config.middleware.use ExceptionNotifier,
  :email_prefix => "[MY APP| Error Report] ",
  :sender_address => %{"MY APP" <err@my-app.com>},
  :exception_recipients => 'my_email@gmail.com'
end

I have two problems:

  • when I get an error in the application - for example. Article.find(not-existing-ID), I will get the standard error page (ERROR 500) from /public/500.html, and not from the file specified in the application controller ... How is this possible? In the past hours, I tried to find the problem, but I still do not know this problem.

  • URL-, 404, . ? -, 404 , /errors/404.html.erb

- , ?

+3
2

:

- 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

  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

. /app/views/pages. 500.html.haml 404.html.haml

.rb( ', ):

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

: , №2 ( 404 ). ?

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

+3

, , !

application_controller.rb :

 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

, rails .

"application_controller.rb" :

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

, , , : errors_controller.rb // :

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

routes.rb :

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

All Articles