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,
lib/pry-rescue/rspec.rb,
lib/pry-rescue/source_location.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

Modules: SourceLocation Classes: RSpec, Rack, Railtie

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.any_exception_capturedObject

Returns the value of attribute any_exception_captured.



42
43
44
# File 'lib/pry-rescue.rb', line 42

def any_exception_captured
  @any_exception_captured
end

Class Method Details

.enter_exception_context(exception) ⇒ Object

Start a Pry session in the context of the exception.

Parameters:

  • exception (Exception)

    The exception raised



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

def enter_exception_context(exception)
  @any_exception_captured = true
  @exception_context_depth ||= 0
  @exception_context_depth += 1

  exception = exception.instance_variable_get(:@rescue_cause) if phantom_load_raise?(exception)
  bindings = exception.instance_variable_get(:@rescue_bindings)

  bindings = without_bindings_below_raise(bindings)
  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),
          :initial_frame => initial_frame(bindings)
    else
      Pry.start bindings.first, :hooks => pry_hooks(exception)
    end
  end
ensure
  @exception_context_depth -= 1
end

.exit_callbacksObject



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

def exit_callbacks
  @exit_callbacks ||= []
end

.in_exception_context?Boolean

Is the user currently inside pry rescue?

Returns:

  • (Boolean)


93
94
95
# File 'lib/pry-rescue.rb', line 93

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

.load(script, ensure_repl = false) ⇒ Object

Load a script wrapped in Pry::rescue{ }

Parameters:

  • script (String)

    The name of the script



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

def load(script, ensure_repl = false)
  require File.expand_path('../pry-rescue/kernel_exit_hooks.rb', __FILE__) if ensure_repl
  Pry::rescue do
    begin
      TOPLEVEL_BINDING.eval File.read(script), script, 1
    rescue SyntaxError => exception
      puts "#{exception}\n"
    end
  end
end

.load_rake(task) ⇒ Object



83
84
85
86
87
88
89
# File 'lib/pry-rescue.rb', line 83

def load_rake(task)
  require 'rake'
  Pry::rescue do
    load "#{Dir.pwd}/Rakefile"
    Rake::Task[task].invoke
  end
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

.run_exit_callbacksObject



6
7
8
9
10
11
12
13
14
# File 'lib/pry-rescue/kernel_exit_hooks.rb', line 6

def run_exit_callbacks
  Pry::rescue do
    exit_callbacks.dup.each(&:call)
  end
  unless any_exception_captured
    print "\n"
    TOPLEVEL_BINDING.pry
  end
end