Class: Pry

Inherits:
Object
  • Object
show all
Defined in:
lib/pry-rescue/core_ext.rb

Overview

Additional methods provided by pry-rescue.

Class Method Summary collapse

Class Method Details

.enable_rescuing!Object

Allow Pry::rescued(e) to work at any point in your program.

Examples:

Pry::enable_rescuing!

begin
  raise "foo"
rescue => e
  Pry::rescued(e)
end


58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pry-rescue/core_ext.rb', line 58

def enable_rescuing!
  @raised = []
  @rescuing = true
  Interception.listen do |exception, binding|
    if defined?(PryStackExplorer)
      @raised << [exception, binding.callers]
    else
      @raised << [exception, Array(binding)]
    end
  end
end

.rescue(&block) ⇒ Object

Start a pry session on any unhandled exceptions within this block.

Examples:

Pry::rescue do
  raise "foo"
end

Returns:

  • (Object)

    The return value of the block



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/pry-rescue/core_ext.rb', line 11

def rescue(&block)
  loop do
    catch(:try_again) do
      @raised = []
      begin
        return with_rescuing(&block)
      rescue Exception => e
        rescued e unless SystemExit === e || SignalException === e
        raise e
      end
    end
  end
end

.rescued(e = $!) ⇒ Object

Start a pry session on an exception that you rescued within a Pry::rescue{ }.

Examples:

Pry::rescue do
  begin
    raise "foo"
  rescue => e
    Pry::rescued(e)
  end
end


36
37
38
39
40
41
42
43
44
45
# File 'lib/pry-rescue/core_ext.rb', line 36

def rescued(e=$!)
  if i = (@raised || []).index{ |(ee, _)| ee == e }
    PryRescue.enter_exception_context(@raised[0..i])
  else
    raise "Tried to inspect an exception that was not raised in a Pry::rescue{ } block"
  end

ensure
  @raised = []
end