Sha256: 525b26d50e92b35c84a7a501d12f3c6ee49c52e6e4509b74e76eebde3748a356

Contents?: true

Size: 1.6 KB

Versions: 16

Compression:

Stored size: 1.6 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)        
    message << "  " << Rails.backtrace_cleaner.clean(exception.backtrace).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.0beta1 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha15 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha14 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha13 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha12 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha11 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha10 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha9 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha8 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha7 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha6 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha5 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha4 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha3 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha2 app/controllers/umlaut/error_handling.rb
umlaut-3.0.0alpha1 app/controllers/umlaut/error_handling.rb