aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport
diff options
context:
space:
mode:
Diffstat (limited to 'activesupport')
-rw-r--r--activesupport/CHANGELOG11
-rw-r--r--activesupport/lib/active_support/core_ext/load_error.rb34
-rw-r--r--activesupport/test/core_ext/load_error_tests.rb17
3 files changed, 62 insertions, 0 deletions
diff --git a/activesupport/CHANGELOG b/activesupport/CHANGELOG
index 42b01c05be..8168f989b1 100644
--- a/activesupport/CHANGELOG
+++ b/activesupport/CHANGELOG
@@ -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.
diff --git a/activesupport/lib/active_support/core_ext/load_error.rb b/activesupport/lib/active_support/core_ext/load_error.rb
new file mode 100644
index 0000000000..42f2d42e78
--- /dev/null
+++ b/activesupport/lib/active_support/core_ext/load_error.rb
@@ -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 \ No newline at end of file
diff --git a/activesupport/test/core_ext/load_error_tests.rb b/activesupport/test/core_ext/load_error_tests.rb
new file mode 100644
index 0000000000..0b24c47112
--- /dev/null
+++ b/activesupport/test/core_ext/load_error_tests.rb
@@ -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 \ No newline at end of file