Sha256: 799a15edaa555fb571a9a5e27cbfdc69a970ade05988cdebfb71ed6d431b2dfd

Contents?: true

Size: 1.71 KB

Versions: 16

Compression:

Stored size: 1.71 KB

Contents

# A mix-in for Rails controllers with some standard error recovery
# logic. 
module Umlaut::ErrorHandling
  extend ActiveSupport::Concern
  
  included do
    # Only custom errors in production
    unless  Rails.application.config.consider_all_requests_local
      
      # generic catch-all comes first, later ones will take priority
      rescue_from Exception, :with => :handle_general_error

      rescue_from ::StoreController::NotFound, ActiveRecord::RecordNotFound, :with => :handle_404_error
    end
  end
  
  
  protected 
  
  def handle_general_error(exception)
    log_error_with_context(exception)
    
    @page_title = "Error!"
    render "error", :status => 500
  end
  
  # Just returns a generic 404 page. 
  # Uses generic 404 page already stored in public/404.html as rails convention.     
  def handle_404_error(exception=nil)
    render :file=>File.join(Rails.root ,"public/404.html"), :layout=>false, :status=>404
  end
  
  
  def log_error_with_context(exception, severity = :fatal)
    message = "\n#{exception.class} (#{exception.message}):\n"
    message << "  uri: #{request.fullpath}\n\n"
    message << "  params: #{params.inspect}\n\n"    
    message << "  Referer: #{request.referer}\n" if request.referer
    message << "  User-Agent: #{request.user_agent}\n"
    message << "  Client IP: #{request.remote_addr}\n\n"
    
    message << exception.annoted_source_code.to_s if exception.respond_to?(:annoted_source_code)
    # mysterious :noise param seems to match how default rails does it, so
    # we actually get a backtrace. 
    message << "  " << Rails.backtrace_cleaner.clean(exception.backtrace, :noise).join("\n  ")    
                    
    logger.send(severity, "#{message}\n\n")          
  end
  
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
umlaut-3.0.5 app/controllers/umlaut/error_handling.rb
umlaut-3.0.4 app/controllers/umlaut/error_handling.rb
umlaut-3.0.3 app/controllers/umlaut/error_handling.rb
umlaut-3.0.2 app/controllers/umlaut/error_handling.rb
umlaut-3.0.1 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0rc1 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0beta10 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0beta9 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0beta8 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0beta7 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0beta6 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0beta5 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0beta4 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0beta3 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0beta2 app/controllers/umlaut/error_handling.rb