1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Stop overriding LoadError.new to return a MissingSourceError (and sometimes nil!)

This commit is contained in:
Yehuda Katz 2010-01-19 22:35:09 -08:00
parent 5ebfa62427
commit 1a50d2e66a
6 changed files with 38 additions and 35 deletions

View file

@ -100,7 +100,7 @@ module ActionController
module_path = module_name.underscore module_path = module_name.underscore
helper module_path helper module_path
rescue MissingSourceFile => e rescue MissingSourceFile => e
raise e unless e.is_missing? "#{module_path}_helper" raise e unless e.is_missing? "helpers/#{module_path}_helper"
rescue NameError => e rescue NameError => e
raise e unless e.missing_name? "#{module_name}Helper" raise e unless e.missing_name? "#{module_name}Helper"
end end

View file

@ -22,7 +22,7 @@ module Dispatching
end end
def show_actions def show_actions
render :text => "actions: #{action_methods.to_a.join(', ')}" render :text => "actions: #{action_methods.to_a.sort.join(', ')}"
end end
protected protected

View file

@ -53,6 +53,8 @@ module ActiveModel
assert_kind_of String, model_name assert_kind_of String, model_name
assert_kind_of String, model_name.human assert_kind_of String, model_name.human
assert_kind_of String, model_name.partial_path assert_kind_of String, model_name.partial_path
assert_kind_of String, model_name.singular
assert_kind_of String, model_name.plural
end end
# errors # errors

View file

@ -1,36 +1,22 @@
class MissingSourceFile < LoadError #:nodoc:
attr_reader :path
def initialize(message, path)
super(message)
@path = path
end
def is_missing?(path)
path.gsub(/\.rb$/, '') == self.path.gsub(/\.rb$/, '')
end
def self.from_message(message)
REGEXPS.each do |regexp, capture|
match = regexp.match(message)
return MissingSourceFile.new(message, match[capture]) unless match.nil?
end
nil
end
REGEXPS = [
[/^no such file to load -- (.+)$/i, 1],
[/^Missing \w+ (file\s*)?([^\s]+.rb)$/i, 2],
[/^Missing API definition file in (.+)$/i, 1],
[/win32/, 0]
] unless defined?(REGEXPS)
end
class LoadError class LoadError
def self.new(*args) REGEXPS = [
if self == LoadError /^no such file to load -- (.+)$/i,
MissingSourceFile.from_message(args.first) /^Missing \w+ (?:file\s*)?([^\s]+.rb)$/i,
else /^Missing API definition file in (.+)$/i,
super ]
def path
@path ||= begin
REGEXPS.find do |regex|
message =~ regex
end
$1
end end
end end
def is_missing?(location)
location.sub(/\.rb$/, '') == path.sub(/\.rb$/, '')
end
end end
MissingSourceFile = LoadError

View file

@ -236,7 +236,7 @@ module ActiveSupport #:nodoc:
rescue LoadError => load_error rescue LoadError => load_error
unless swallow_load_errors unless swallow_load_errors
if file_name = load_error.message[/ -- (.*?)(\.rb)?$/, 1] if file_name = load_error.message[/ -- (.*?)(\.rb)?$/, 1]
raise MissingSourceFile.new(message % file_name, load_error.path).copy_blame!(load_error) raise LoadError.new(message % file_name).copy_blame!(load_error)
end end
raise raise
end end

View file

@ -15,3 +15,18 @@ class TestMissingSourceFile < Test::Unit::TestCase
end end
end end
end end
class TestLoadError < Test::Unit::TestCase
def test_with_require
assert_raise(LoadError) { require 'no_this_file_don\'t_exist' }
end
def test_with_load
assert_raise(LoadError) { load 'nor_does_this_one' }
end
def test_path
begin load 'nor/this/one.rb'
rescue LoadError => e
assert_equal 'nor/this/one.rb', e.path
end
end
end