Class: PryRescue

Inherits:
Object
  • Object
show all
Defined in:
lib/pry-rescue.rb,
lib/pry-rescue/peek.rb,
lib/pry-rescue/rack.rb,
lib/pry-rescue/rails.rb

Overview

PryRescue provides the ability to open a Pry shell whenever an unhandled exception is raised in your code.

The main API is exposed via the Pry object, but here are a load of helpers that I didn’t want to pollute the Pry namespace with.

See Also:

  • {Pry{Pry::rescue}

Defined Under Namespace

Classes: Rack, Railtie

Class Method Summary collapse

Class Method Details

.enter_exception_context(raised) ⇒ Object

Start a Pry session in the context of the exception.

Parameters:

  • raised (Array<Exception, Array<Binding>>)

    The exceptions raised



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/pry-rescue.rb', line 44

def enter_exception_context(raised)
  @exception_context_depth ||= 0
  @exception_context_depth += 1

  raised = raised.map do |e, bs|
    [e, without_bindings_below_raise(bs)]
  end

  raised.pop if phantom_load_raise?(*raised.last)
  exception, bindings = raised.last
  bindings = without_duplicates(bindings)

  with_program_name "#$PROGRAM_NAME [in pry-rescue @ #{Dir.pwd}]" do
    if defined?(PryStackExplorer)
      pry :call_stack => bindings,
          :hooks => pry_hooks(exception, raised),
          :initial_frame => initial_frame(bindings)
    else
      Pry.start bindings.first, :hooks => pry_hooks(exception, raised)
    end
  end
ensure
  @exception_context_depth -= 1
end

.in_exception_context?Boolean

Is the user currently inside pry rescue?

Returns:

  • (Boolean)


77
78
79
# File 'lib/pry-rescue.rb', line 77

def in_exception_context?
  @exception_context_depth && @exception_context_depth > 0
end

.load(script) ⇒ Object

Load a script wrapped in Pry::rescue{ }

Parameters:

  • script (String)

    The name of the script



71
72
73
# File 'lib/pry-rescue.rb', line 71

def load(script)
  Pry::rescue{ Kernel.load script }
end

.peek!Object

Called when rescue –peek is used and the user hits <Ctrl+C> or sends whichever signal is configured.



8
9
10
11
12
13
14
15
16
17
# File 'lib/pry-rescue/peek.rb', line 8

def self.peek!(*)
  puts 'Preparing to peek via pry!' unless ENV['NO_PEEK_STARTUP_MESSAGE']
  require 'pry'
  unless binding.respond_to?(:of_caller)
    raise "pry-stack_explorer is not installed"
  end
  throw :raise_up, Interrupt if Pry === binding.of_caller(1).eval('self')
  binding.of_caller(1).pry
  # TODO pry :call_stack => binding.of_callers, :initial_frame => 1
end

.peek_on_signal(signal) ⇒ Object



2
3
4
# File 'lib/pry-rescue/peek.rb', line 2

def self.peek_on_signal signal
  trap signal, &method(:peek!)
end