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!(block = nil) ⇒ 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


73
74
75
76
77
78
79
80
81
# File 'lib/pry-rescue/core_ext.rb', line 73

def enable_rescuing!(block=nil)
  Interception.listen(block) do |exception, binding|
    bindings = binding.respond_to?(:callers) ? binding.callers : [binding]
    unless exception.instance_variable_defined?(:@rescue_bindings)
      exception.instance_variable_set(:@rescue_bindings, bindings)
      exception.instance_variable_set(:@rescue_cause, $!)
    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
# File 'lib/pry-rescue/core_ext.rb', line 11

def rescue(&block)
  loop do
    catch(:try_again) do
      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


35
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
# File 'lib/pry-rescue/core_ext.rb', line 35

def rescued(e=$!)
  if e.instance_variable_defined?(:@rescue_bindings)
    PryRescue.enter_exception_context(e)
  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

end