require 'logger' def load_path_candidates_for(filename) $LOAD_PATH.select { |path| File.exist?(File.join(path, filename)) } end def pick_first(candidates, *regexps) for r in regexps match = candidates.find { |c| c =~ r } return match if match end nil end candidate_paths = load_path_candidates_for('dispatcher.rb') built_in_dispatcher_path = pick_first(candidate_paths, /vendor/, /gems/) if built_in_dispatcher_path RAILS_DEFAULT_LOGGER.debug "Using pre-dispatcher from\n #{File.expand_path(__FILE__)})" RAILS_DEFAULT_LOGGER.debug "Loading built-in dispatcher from\n #{built_in_dispatcher_path}" Kernel.load File.join(built_in_dispatcher_path, 'dispatcher.rb') class Dispatcher class << self alias_method :original_dispatch, :dispatch def dispatch(cgi = nil, session_options = ActionController::CgiRequest::DEFAULT_SESSION_OPTIONS, output = $stdout) begin ActiveRecord::Base.connection rescue Exception => e RAILS_DEFAULT_LOGGER.debug \ "Pre-Dispatcher (#{__FILE__}) trapped exception: #{e.class} -- #{e}" if cgi ||= new_cgi(output) request = ActionController::CgiRequest.new(cgi, session_options) response = ActionController::CgiResponse.new(cgi) request.parameters[:error] = e begin DatabaseExceptionController.process(request, response).out(output) rescue Exception => f RAILS_DEFAULT_LOGGER.debug \ "Pre-Dispatcher (#{__FILE__}) trap failed with: #{f}" end end else original_dispatch(cgi, session_options, output) end end end end end