Warning: call_user_func() expects parameter 1 to be a valid callback, no array or string given in /home/robzbd/sites/blog.ubrio.us/wp-content/plugins/akismet/widget.php on line 126

Warning: ob_start(): non-static method wpGoogleAnalytics::get_links() should not be called statically in /home/robzbd/sites/blog.ubrio.us/wp-content/plugins/wp-google-analytics/wp-google-analytics.php on line 288
Rails 2.2 Custom Error Pages With Exception Loggable

Rails 2.2 Custom Error Pages With Exception Loggable

Well, with the release of Rails 2.2 and the ability to create custom error pages I figured I’d revisit my 404/500 pages and try to do it the new way.

Setting up the rescue_from methods were simple enough, but I noticed that when I was capturing my error 500s it wasn’t logging the exception. Here is a quick way to have it both display your custom 500 page and log the exception for later review:

  # application.rb
  include ExceptionLoggable

  unless ActionController::Base.consider_all_requests_local
    # yeah, its a long line
    rescue_from ActiveRecord::RecordNotFound, ActionController::RoutingError, ActionController::UnknownController, ActionController::UnknownAction, :with => :render_404
    rescue_from RuntimeError, :with => :render_500
  end

private

  def render_404
    render :template => "shared/error_404", :layout => 'application', :status => :not_found
  end

  def render_500
    # hacky, but works, this logs the exception in ExceptionLoggable
    log_exception $!
    render :template => "shared/error_500", :layout => 'application', :status => :internal_server_error
  end

To test this just change config.action_controller.consider_all_requests_local = true to config.action_controller.consider_all_requests_local = false in your config/environments/development.rb file.

Not the _best_ way or cleanest, but it gets the job done without too much trouble.




Comments

Add Yours

  • Author Avatar

    YOU


Comment Arrow




About Author

Rob Hurring

Ruby, Rails, PHP, bash... oh my!