mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Improved error reporting especially around never shallowing exceptions. Debugging helpers should be much easier now #980 [Nicholas Seckar]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@985 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
parent
3697df1dd2
commit
373adc7f86
3 changed files with 62 additions and 0 deletions
|
@ -1,3 +1,14 @@
|
|||
*SVN*
|
||||
|
||||
* Added Object#suppress which allows you to make a saner choice around with exceptions to swallow #980. Example:
|
||||
|
||||
suppress(ZeroDivisionError) { 1/0 }
|
||||
|
||||
...instead of:
|
||||
|
||||
1/0 rescue nil # BAD, EVIL, DIRTY.
|
||||
|
||||
|
||||
*1.0.2*
|
||||
|
||||
* Added Kernel#returning -- a Ruby-ized realization of the K combinator, courtesy of Mikael Brockman.
|
||||
|
|
34
activesupport/lib/active_support/core_ext/load_error.rb
Normal file
34
activesupport/lib/active_support/core_ext/load_error.rb
Normal file
|
@ -0,0 +1,34 @@
|
|||
class MissingSourceFile < LoadError
|
||||
attr_reader :path
|
||||
def initialize(message, path)
|
||||
super(message)
|
||||
@path = path
|
||||
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]
|
||||
]
|
||||
end
|
||||
|
||||
module ActiveSupport
|
||||
module CoreExtensions
|
||||
module LoadErrorExtensions
|
||||
module LoadErrorClassMethods
|
||||
def new(*args)
|
||||
(self == LoadError && MissingSourceFile.from_message(args.first)) || super
|
||||
end
|
||||
end
|
||||
::LoadError.extend(LoadErrorClassMethods)
|
||||
end
|
||||
end
|
||||
end
|
17
activesupport/test/core_ext/load_error_tests.rb
Normal file
17
activesupport/test/core_ext/load_error_tests.rb
Normal file
|
@ -0,0 +1,17 @@
|
|||
require 'test/unit'
|
||||
require File.dirname(__FILE__) + '/../../lib/active_support/core_ext/load_error'
|
||||
|
||||
class TestMissingSourceFile < Test::Unit::TestCase
|
||||
def test_with_require
|
||||
assert_raises(MissingSourceFile) { require 'no_this_file_don\'t_exist' }
|
||||
end
|
||||
def test_with_load
|
||||
assert_raises(MissingSourceFile) { load 'nor_does_this_one' }
|
||||
end
|
||||
def test_path
|
||||
begin load 'nor/this/one.rb'
|
||||
rescue MissingSourceFile => e
|
||||
assert_equal 'nor/this/one.rb', e.path
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in a new issue