interception 0.1.pre.2 → 0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2012 Conrad Irwin <conrad.irwin@gmail.com>
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
+ THE SOFTWARE.
@@ -0,0 +1,85 @@
1
+
2
+ Interception (intercept + exception) allows you to intercept all exceptions as they are
3
+ raised.
4
+
5
+ Installation
6
+ ============
7
+
8
+ As with all rubygems, use gem to install:
9
+
10
+ ```shell
11
+ gem install interception
12
+ ```
13
+
14
+ Or, if you're using bundler:
15
+
16
+ ```ruby
17
+ source :rubygems
18
+ gem 'interception'
19
+ ```
20
+
21
+ Usage
22
+ =====
23
+
24
+ Add and remove listeners. They'll be called whenever an exception is raised (whether or
25
+ not it would later be rescued) with both the exception object, and the binding from which
26
+ it was raised. The binding can be used to discover further information about the context.
27
+
28
+ ```ruby
29
+ require 'interception'
30
+ listener = lambda{ |exception, binding|
31
+ puts "raised: #{exception.inspect}"
32
+ }
33
+
34
+ Interception.listen(listener)
35
+
36
+ begin
37
+ raise "oopsy"
38
+ rescue => exception
39
+ puts "rescued: #{exception.inspect}"
40
+ end
41
+
42
+ raise "daisy"
43
+
44
+ Interception.unlisten(listener)
45
+ ```
46
+
47
+ In the common case that you want to listen to events for the duration of a block, you can
48
+ also pass that block to listen:
49
+
50
+ ```ruby
51
+ require 'interception'
52
+ def log_exceptions(&block)
53
+ Interception.listen(block) do |exception, binding|
54
+ puts "raised: #{exception.inspect} from #{binding.eval("__method__")}"
55
+ end
56
+ end
57
+
58
+ def hello
59
+ raise "oopsy"
60
+ rescue => exception
61
+ puts "rescued: #{exception.inspect} in #{__method__}"
62
+ raise "daisy"
63
+ end
64
+
65
+ log_exceptions do
66
+ hello
67
+ end
68
+ ```
69
+
70
+ Your listen block is run as through it were called by `Kernel#raise`, so try not to raise
71
+ an exception from within it, or you will loose the original exception.
72
+
73
+ Known bugs
74
+ ==========
75
+
76
+ * On rubinius we don't catch some low-level exceptions (like `ZeroDivisionError`).
77
+ * On jruby, the binding sometimes has the wrong value for `self` (in `NoMethodError`s).
78
+ * On MRI-1.8.7, the binding sometimes has the wrong value for `self`.
79
+ * No known bugs on MRI-1.9.3
80
+
81
+ Meta-fu
82
+ =======
83
+
84
+ Interception is released under the MIT license (see `LICENSE.MIT` for details).
85
+ Contributions and bug reports are welcome.
@@ -1,21 +1,16 @@
1
- $:.unshift File.expand_path '../../lib', __FILE__
2
- require 'pryception'
1
+ require 'interception'
2
+ listener = lambda{ |exception, binding|
3
+ puts "raised: #{exception.inspect}"
4
+ }
3
5
 
4
- Interception.prycept do
6
+ Interception.listen(listener)
5
7
 
6
- def a
7
- begin
8
- begin
9
- raise "foo"
10
-
11
- rescue => e
12
- raise "bar"
13
- end
8
+ begin
9
+ raise "oopsy"
10
+ rescue => exception
11
+ puts "rescued: #{exception.inspect}"
12
+ end
14
13
 
15
- rescue => e
16
- 1 / 0
14
+ raise "daisy"
17
15
 
18
- end
19
- end
20
- a
21
- end
16
+ Interception.unlisten(listener)
@@ -1,20 +1,17 @@
1
- $:.unshift File.expand_path '../../lib', __FILE__
2
- require 'pryception'
3
-
4
- def alpha
5
- x = 1
6
- beta
7
- end
8
-
9
- def beta
10
- y = 30
11
- gamma(1, 2)
1
+ require 'interception'
2
+ def log_exceptions(&block)
3
+ Interception.listen(block) do |exception, binding|
4
+ puts "raised: #{exception.inspect} from #{binding.eval("__method__")}"
5
+ end
12
6
  end
13
7
 
14
- def gamma(x)
15
- greeting = x
8
+ def hello
9
+ raise "oopsy"
10
+ rescue => exception
11
+ puts "rescued: #{exception.inspect} in #{__method__}"
12
+ raise "daisy"
16
13
  end
17
14
 
18
- Interception.prycept do
19
- alpha
15
+ log_exceptions do
16
+ hello
20
17
  end
@@ -13,12 +13,11 @@ public class InterceptionEventHook extends EventHook {
13
13
  private RubyProc proc;
14
14
 
15
15
  public InterceptionEventHook(RubyProc proc) {
16
- super();
17
16
  this.proc = proc;
18
17
  }
19
18
 
20
19
  public boolean isInterestedInEvent(RubyEvent event) {
21
- return event.getName().equals(RubyEvent.RAISE.getName());
20
+ return event == RubyEvent.RAISE;
22
21
  }
23
22
 
24
23
  public void eventHandler(ThreadContext context, String eventName, String file, int line, String name, IRubyObject type) {
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "interception"
3
- s.version = "0.1.pre.2"
3
+ s.version = "0.1"
4
4
  s.author = "Conrad Irwin"
5
5
  s.email = "conrad.irwin@gmail.com"
6
6
  s.homepage = "http://github.com/ConradIrwin/interception"
metadata CHANGED
@@ -1,48 +1,63 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: interception
3
- version: !ruby/object:Gem::Version
4
- prerelease: 4
5
- version: 0.1.pre.2
3
+ version: !ruby/object:Gem::Version
4
+ hash: 9
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 1
9
+ version: "0.1"
6
10
  platform: ruby
7
- authors:
11
+ authors:
8
12
  - Conrad Irwin
9
- autorequire:
13
+ autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
- date: 2012-08-11 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
16
+
17
+ date: 2012-08-12 00:00:00 Z
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
15
20
  name: rake
16
- version_requirements: &2056 !ruby/object:Gem::Requirement
17
- requirements:
18
- - - ! '>='
19
- - !ruby/object:Gem::Version
20
- version: '0'
21
- none: false
22
- requirement: *2056
23
21
  prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 0
30
+ version: "0"
24
31
  type: :development
25
- - !ruby/object:Gem::Dependency
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
26
34
  name: rspec
27
- version_requirements: &2074 !ruby/object:Gem::Requirement
28
- requirements:
29
- - - ! '>='
30
- - !ruby/object:Gem::Version
31
- version: '0'
32
- none: false
33
- requirement: *2074
34
35
  prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ hash: 3
42
+ segments:
43
+ - 0
44
+ version: "0"
35
45
  type: :development
46
+ version_requirements: *id002
36
47
  description: Provides a cross-platform ability to intercept all exceptions as they are raised.
37
48
  email: conrad.irwin@gmail.com
38
49
  executables: []
39
- extensions:
50
+
51
+ extensions:
40
52
  - ext/extconf.rb
41
53
  extra_rdoc_files: []
42
- files:
54
+
55
+ files:
43
56
  - .gitignore
44
57
  - .travis.yml
45
58
  - Gemfile
59
+ - LICENSE.MIT
60
+ - README.md
46
61
  - Rakefile
47
62
  - examples/example.rb
48
63
  - examples/example2.rb
@@ -52,33 +67,41 @@ files:
52
67
  - interception.gemspec
53
68
  - lib/cross_platform.rb
54
69
  - lib/interception.rb
55
- - lib/pryception.rb
56
- - pryly.rb
57
70
  - spec/interception_spec.rb
58
71
  - spec/spec_helpers.rb
59
72
  homepage: http://github.com/ConradIrwin/interception
60
73
  licenses: []
61
- post_install_message:
74
+
75
+ post_install_message:
62
76
  rdoc_options: []
63
- require_paths:
77
+
78
+ require_paths:
64
79
  - lib
65
- required_ruby_version: !ruby/object:Gem::Requirement
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
80
+ required_ruby_version: !ruby/object:Gem::Requirement
70
81
  none: false
71
- required_rubygems_version: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ! '>'
74
- - !ruby/object:Gem::Version
75
- version: 1.3.1
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ hash: 3
86
+ segments:
87
+ - 0
88
+ version: "0"
89
+ required_rubygems_version: !ruby/object:Gem::Requirement
76
90
  none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 3
95
+ segments:
96
+ - 0
97
+ version: "0"
77
98
  requirements: []
78
- rubyforge_project:
79
- rubygems_version: 1.8.15
80
- signing_key:
99
+
100
+ rubyforge_project:
101
+ rubygems_version: 1.8.24
102
+ signing_key:
81
103
  specification_version: 3
82
104
  summary: Intercept exceptions as they are being raised
83
105
  test_files: []
84
- ...
106
+
107
+ has_rdoc:
@@ -1,66 +0,0 @@
1
- require 'rubygems'
2
- require 'interception'
3
- require 'pry'
4
-
5
- begin
6
- require 'pry-stack_explorer'
7
- rescue LoadError
8
- end
9
-
10
- module Interception
11
-
12
- class << self
13
-
14
- # Intercept all exceptions that arise in the block and start a Pry session
15
- # at the fail site.
16
- def prycept(&block)
17
- raised = []
18
-
19
- Interception.listen(block) do |exception, binding|
20
- if defined?(PryStackExplorer)
21
- raised << [exception, binding.callers]
22
- else
23
- raised << [exception, Array(binding)]
24
- end
25
- end
26
-
27
- ensure
28
- if raised.any?
29
- exception, bindings = raised.last
30
- enter_exception_context(exception, bindings)
31
- end
32
- end
33
-
34
- private
35
-
36
- # Sanitize the call stack.
37
- # @param [Array<Binding>] bindings The call stack.
38
- def prune_call_stack!(bindings)
39
- bindings.delete_if { |b| b.eval("self") == self || b.eval("__method__") == :prycept }
40
- end
41
-
42
- # Start a Pry session in the context of the exception.
43
- # @param [Exception] exception The exception.
44
- # @param [Array<Binding>] bindings The call stack.
45
- def enter_exception_context(exception, bindings)
46
- inject_local("_ex_", exception, bindings.first)
47
- inject_local("_raised_", [exception, bindings.first], bindings.first)
48
-
49
- prune_call_stack!(bindings)
50
- if defined?(PryStackExplorer)
51
- pry :call_stack => bindings
52
- else
53
- bindings.first.pry
54
- end
55
- end
56
-
57
- # Inject a local variable into a binding.
58
- def inject_local(var, object, binding)
59
- Thread.current[:__intercept_var__] = object
60
- binding.eval("#{var} = Thread.current[:__intercept_var__]")
61
- ensure
62
- Thread.current[:__intercept_var__] = nil
63
- end
64
- end
65
- end
66
-
data/pryly.rb DELETED
@@ -1,41 +0,0 @@
1
- require 'rubygems'
2
- require 'interception'
3
- require 'pry'
4
- require 'pry-stack_explorer'
5
- def pryly(&block)
6
- raised = []
7
-
8
- Interception.listen(block) do |exception, binding|
9
- raised << [exception, binding.callers]
10
- end
11
-
12
- ensure
13
- if raised.last
14
- e, bindings = raised.last
15
- $foo = e
16
- $bar = raised
17
- bindings.first.eval("_ex_ = $foo")
18
- bindings.first.eval("_raised_ = $bar")
19
- bindings = bindings.drop_while { |b| b.eval("self") == Interception || b.eval("__method__") == :pryly }
20
- pry :call_stack => bindings
21
- end
22
- end
23
-
24
- pryly do
25
-
26
- def a
27
- begin
28
- begin
29
- raise "foo"
30
-
31
- rescue => e
32
- raise "bar"
33
- end
34
-
35
- rescue => e
36
- 1 / 0
37
-
38
- end
39
- end
40
- a
41
- end