reraise 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/reraise.rb +30 -0
  2. data/spec/reraise_spec.rb +62 -0
  3. metadata +48 -0
@@ -0,0 +1,30 @@
1
+
2
+ module ::ReRaise
3
+
4
+ #############
5
+ # reraise #
6
+ #############
7
+
8
+ ###
9
+ # Re-raise exception, adding a line to the Exception's backtrace
10
+ # corresponding to line where missing call originated.
11
+ #
12
+ # @param depth_of_exception_from_originating_missing_call
13
+ #
14
+ # [Integer]
15
+ #
16
+ def reraise( depth_of_exception_from_originating_missing_call )
17
+
18
+ backtrace_array = backtrace
19
+ missing_method_call = backtrace_array[ depth_of_exception_from_originating_missing_call ]
20
+ backtrace_array.unshift( missing_method_call )
21
+
22
+ raise exception
23
+
24
+ end
25
+
26
+ end
27
+
28
+ class ::Exception
29
+ include( ::ReRaise )
30
+ end
@@ -0,0 +1,62 @@
1
+
2
+ require_relative '../lib/reraise.rb'
3
+
4
+ describe ::ReRaise do
5
+
6
+ it 'can catch an exception and re-raise it - for instance, after method_missing' do
7
+
8
+ class ::ReRaise::MockMethodMissing
9
+
10
+ def method_missing( method, *args )
11
+
12
+ begin
13
+ super
14
+ rescue ::Exception => exception
15
+ exception.reraise( 1 )
16
+ end
17
+
18
+ end
19
+
20
+ end
21
+
22
+ begin
23
+ ::ReRaise::MockMethodMissing.new.some_method
24
+ rescue ::Exception => reraised_exception
25
+ reraised_exception.backtrace[ 0 ].should == __FILE__ + ":23:in `block (2 levels) in <top (required)>'"
26
+ end
27
+
28
+ end
29
+
30
+ it 'can catch an exception and re-raise it - for instance, with a proxy' do
31
+
32
+ class ::ReRaise::MockProxy
33
+
34
+ def <=>( object )
35
+
36
+ if some_other_object.respond_to?( :"<=>" )
37
+
38
+ some_other_object <=> object
39
+
40
+ else
41
+
42
+ begin
43
+ method_missing( __method__, object )
44
+ rescue ::Exception => exception
45
+ exception.reraise( 2 )
46
+ end
47
+
48
+ end
49
+
50
+ end
51
+
52
+ end
53
+
54
+ begin
55
+ ::ReRaise::MockProxy.new == 42
56
+ rescue ::Exception => reraised_exception
57
+ reraised_exception.backtrace[ 0 ].should == __FILE__ + ":55:in `block (2 levels) in <top (required)>'"
58
+ end
59
+
60
+ end
61
+
62
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reraise
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Asher
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-02 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Adds a line to the Exception's backtrace corresponding to line where
15
+ missing call originated. Does not modify backtrace otherwise. Requires specifying
16
+ how many levels under missing call re-raise occurs.
17
+ email: asher@ridiculouspower.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - lib/reraise.rb
23
+ - spec/reraise_spec.rb
24
+ homepage: http://rubygems.org/gems/reraise
25
+ licenses: []
26
+ post_install_message:
27
+ rdoc_options: []
28
+ require_paths:
29
+ - lib
30
+ required_ruby_version: !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: 1.9.1
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ! '>='
40
+ - !ruby/object:Gem::Version
41
+ version: '0'
42
+ requirements: []
43
+ rubyforge_project: reraise
44
+ rubygems_version: 1.8.24
45
+ signing_key:
46
+ specification_version: 3
47
+ summary: Re-raise a rescued Exception to refer to line where missing call originated.
48
+ test_files: []