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


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/pry-rescue/core_ext.rb', line 76

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# 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
    stack = ''
    stack = "\n" + e.backtrace.join("\n") if e.backtrace
    case e
    when SystemStackError
      # Interception cannot reliably interept SystemStackErrors as it needs
      # to call a function at the point the stack runs out.
      # We use a special error message here, as it seems nicer to assume that
      # the user knows what they are doing, and it's the software that's
      # terrible.
      warn "WARNING: Insufficient stack space to inspect exception" + stack
    else
      # We used to raise an exception at this point, but that turned out to
      # not be very helpful as it obscured the original cause of the problem.
      # I considered adding an explicit 'raise e' here, but decided against
      # it on the grounds that the Pry::rescued call is normally in someone
      # else's error handler already.
      warn "WARNING: Tried to inspect exception outside of Pry::rescue{ }" + \
        stack
    end
  end

ensure
  @raised = []
end