Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #7452: add_error_option.2.diff

File add_error_option.2.diff, 3.5 kB (added by rogerdpack, 11 months ago)

v2, with applied suggestions: this patch takes out redundant inner rescue block, slightly nicer output, should keep the original verbosity of it intact.

  • dependencies.rb

    old new  
    22require 'active_support/core_ext/module/attribute_accessors' 
    33require 'active_support/core_ext/load_error' 
    44require 'active_support/core_ext/kernel' 
    5  
    65module Dependencies #:nodoc: 
    76  extend self 
    87 
     
    109  mattr_accessor :warnings_on_first_load 
    1110  self.warnings_on_first_load = false 
    1211 
     12  # Should we warn on all errors? 
     13  mattr_accessor :warn_on_all_errors 
     14  self.warn_on_all_errors = false 
     15   
    1316  # All files ever loaded. 
    1417  mattr_accessor :history 
    1518  self.history = Set.new 
     
    5861  def depend_on(file_name, swallow_load_errors = false) 
    5962    path = search_for_file(file_name) 
    6063    require_or_load(path || file_name) 
    61   rescue LoadError 
     64  rescue LoadError => detail 
     65    log_error "depend on #{file_name} returned LoadError: #{detail}" 
    6266    raise unless swallow_load_errors 
    6367  end 
    6468 
     
    9599        else 
    96100          enable_warnings { result = load_file(*load_args) } 
    97101        end 
    98       rescue Exception 
     102      rescue Exception => detail 
     103        log_error "error during a load #{file_name} #{detail}" 
    99104        loaded.delete expanded 
    100105        raise 
    101106      end 
     
    246251    file_path = search_for_file(path_suffix) 
    247252    if file_path && ! loaded.include?(File.expand_path(file_path)) # We found a matching file to load 
    248253      require_or_load file_path 
    249       raise LoadError, "Expected #{file_path} to define #{qualified_name}" unless from_mod.const_defined?(const_name) 
     254      raise LoadError, "Expected #{file_path} to define #{qualified_name}--check path, and logs, modify warnings levels in #{__FILE__}." unless from_mod.const_defined?(const_name) 
    250255      return from_mod.const_get(const_name) 
    251256    elsif mod = autoload_module!(from_mod, const_name, qualified_name, path_suffix) 
    252257      return mod 
     
    341346    begin 
    342347      yield # Now yield to the code that is to define new constants. 
    343348      aborting = false 
     349    rescue Exception => detail 
     350      log_error "error thrown in #{descs.inspect} #{detail}" # log it here, too 
     351      raise 
    344352    ensure 
    345353      # Find the new constants. 
    346354      new_constants = watch_frames.collect do |mod_name, prior_constants| 
     
    369377        new_constants.clear 
    370378      end 
    371379    end 
    372      
    373380    return new_constants 
    374381  ensure 
    375382    # Remove the stack frames that we added. 
    376     if defined?(watch_frames) && ! watch_frames.empty? 
     383    if defined?(watch_frames) && watch_frames && ! watch_frames.empty? 
    377384      frame_ids = watch_frames.collect(&:object_id) 
    378385      constant_watch_stack.delete_if do |watch_frame| 
    379386        frame_ids.include? watch_frame.object_id 
     
    429436    log "called #{selector}(#{arg_str})" 
    430437  end 
    431438   
    432   def log(msg) 
    433     if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER && log_activity 
    434       RAILS_DEFAULT_LOGGER.debug "Dependencies: #{msg}" 
     439  def log_error(msg) 
     440    if warn_on_all_errors 
     441     log(msg, true) 
     442    else 
     443     log(msg, false) 
     444    end 
     445  end 
     446     
     447  def log(msg, is_error = false) 
     448    new_message = "Dependencies: #{msg}" 
     449    if defined?(RAILS_DEFAULT_LOGGER) && RAILS_DEFAULT_LOGGER 
     450      if is_error 
     451        RAILS_DEFAULT_LOGGER.error new_message # log error activity high, as requested. 
     452      elsif log_activity  
     453        RAILS_DEFAULT_LOGGER.debug new_message # log normal activity 
     454      end 
     455    else  
     456      if log_activity or is_error 
     457        print new_message + "\n" # print it to stdout, at least, as there's no logger around 
     458      end 
    435459    end 
    436460  end 
    437461