Permalink
Browse files

Improved error reporting especially around never shallowing exception…

…s. 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
  • Loading branch information...
1 parent 3697df1 commit 373adc7f86f68f7b16d136835bb9254aaffd797f @dhh dhh committed Mar 23, 2005
View
@@ -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.
@@ -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
@@ -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

0 comments on commit 373adc7

Please sign in to comment.