Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

log backtrace, rescue_from and custom error pages

I am trying to create custom error pages in a project of rails 3.0.20 and ruby 1.8.7.

Anyway in my application controller:

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

Then the render error method:

def render_error(exception)
  notify_airbrake(exception)
  Rails.logger.fatal exception
  Rails.logger.fatal exception.backtrace.join("\n")
  respond_to do |format|
    format.html { render :template => "errors/error_500", :layout => 'layouts/application'}
    format.all { render :nothing => true, :status => 500 }
  end
end

It seems that now my logs are being filled with a much longer then usual backtrace. Why is that happening? Is there a way to show just the "important" part of a backtrace? And is it correct to call airbrake here?

Thanks

like image 355
guy schaller Avatar asked Feb 18 '23 15:02

guy schaller


1 Answers

Check out http://api.rubyonrails.org/classes/ActiveSupport/BacktraceCleaner.html. Rails uses this to clean up your backrace before display.

You could use it like this:

Rails.backtrace_cleaner.clean(exception.backtrace)

I'm actually looking in the rails source code, because I thought your exception might have been cleaned already by the time it gets to your render_error method.

like image 142
rainkinz Avatar answered Feb 20 '23 05:02

rainkinz