• Stars
    star
    850
  • Rank 53,628 (Top 2 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 12 years ago
  • Updated 11 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Start a pry session whenever something goes wrong.

pry-rescue

Super-fast debugging for Ruby. (See Pry to the rescue!) Build status

Introduction

pry-rescue is an implementation of "break on unhandled exception" for Ruby. Whenever an exception is raised, but not rescued, pry-rescue will automatically open Pry for you:

$ rescue examples/example2.rb
From: /home/conrad/0/ruby/pry-rescue/examples/example2.rb @ line 19 Object#beta:

    17: def beta
    18:   y = 30
 => 19:   gamma(1, 2)
    20: end

ArgumentError: wrong number of arguments (2 for 1)
from /home/conrad/0/ruby/pry-rescue/examples/example2.rb:22:in `gamma`
[1] pry(main)>

Installation

You can install pry-rescue with RubyGems as normal, and I strongly recommend you also install pry-stack_explorer. See Known bugs for places that won't work.

gem install pry-rescue pry-stack_explorer

If you're using Bundler, you can add it to your Gemfile in the development group:

group :development do
  gem 'pry-rescue'
  gem 'pry-stack_explorer'
end

Usage

For simple Ruby scripts, just run them with the rescue executable instead of the ruby executable.

rescue <script.rb> [arguments..]

Rails

For Rails, use rescue rails in place of rails, for example:

rescue rails server

If you're using bundle exec the rescue should go after the exec:

bundle exec rescue rails server

Then whenever an unhandled exception happens inside Rails, a Pry console will open on stdout. This is the same terminal that you see the Rails logs on, so if you're using something like pow then you will run into difficulties.

If you are using non-default http servers like Unicorn or Thin, you can also trigger this behavior via (after including pry-rescue in your Gemfile):

PRY_RESCUE_RAILS=1 bundle exec unicorn

You might also be interested in better_errors which opens consoles in your browser on unhandled exceptions, and pry-rails which adds some Rails specific helpers to Pry, and replaces rails console by Pry.

RSpec

If you're using RSpec or respec, you can open a Pry session on every test failure using rescue rspec or rescue respec:

$ rescue rspec
From: /home/conrad/0/ruby/pry-rescue/examples/example_spec.rb @ line 9 :

     6:
     7: describe "Float" do
     8:   it "should be able to add" do
 =>  9:     (0.1 + 0.2).should == 0.3
    10:   end
    11: end

RSpec::Expectations::ExpectationNotMetError: expected: 0.3
     got: 0.30000000000000004 (using ==)
[1] pry(main)>

Unfortunately using edit -c to edit _spec.rb files does not yet reload the code in a way that the try-again command can understand. You can still use try-again if you edit code that is not in spec files.

If you want pry-rescue to always be enabled when you run tests, simply add this line to your test_helper.rb:

require 'pry-rescue/rspec'

Minitest

Add the following to your test_helper.rb or to the top of your test file.

require 'minitest/autorun'
require 'pry-rescue/minitest'

Then, when you have a failure, you can use edit, edit -c, and edit-method, then try-again to re-run the tests.

Rack

If you're using Rack, you should use the middleware instead (though be careful to only include it in development!):

use PryRescue::Rack if ENV["RACK_ENV"] == 'development'

Pry commands

pry-rescue adds two commands to Pry. cd-cause and try-again. In combination with edit --method these can let you fix the problem with your code and verify that the fix worked without restarting your program.

cd-cause

If you've run some code in Pry, and an exception was raised, you can use the cd-cause command:

[1] pry(main)> foo
RuntimeError: two
from a.rb:4:in `rescue in foo`
[2] pry(main)> cd-cause
From: a.rb @ line 4 Object#foo:

    1: def foo
    2:   raise "one"
    3: rescue => e
 => 4:   raise "two"
    5: end

[3] pry(main)>

If that exception was in turn caused by a previous exception you can use cd-cause again to move to the original problem:

[3] pry(main)> cd-cause
From: examples/example.rb @ line 4 Object#test:

    4: def test
 => 5:   raise "foo"
    6: rescue => e
    7:   raise "bar"
    8: end

RuntimeError: foo
from examples/example.rb:5:in `test`
[4] pry(main)>

To get back from cd-cause you can either type <ctrl+d> or cd ...

try-again

Once you've used Pry's edit or command to fix your code, you can issue a try-again command to re-run your code. For Rails and rack, this re-runs the request, for minitest and rspec, it re-runs the current test, for more advanced users this re-runs the Pry::rescue{ } block.

[4] pry(main)> edit --method
[5] pry(main)> whereami
From: examples/example.rb @ line 4 Object#test:

    4: def test
 => 5:   puts "foo"
    6: rescue => e
    7:   raise "bar"
    8: end
[6] pry(main)> try-again
foo

Advanced usage

Block form

If you want more fine-grained control over which parts of your code are rescued, you can also use the block form:

require 'pry-rescue'

def test
  raise "foo"
rescue => e
  raise "bar"
end

Pry.rescue do
  test
end

This will land you in a pry-session:

From: examples/example.rb @ line 4 Object#test:

    4: def test
    5:   raise "foo"
    6: rescue => e
 => 7:   raise "bar"
    8: end

RuntimeError: bar
from examples/example.rb:7:in `rescue in test`
[1] pry(main)>

Rescuing an exception

Finally. If you're doing your own exception handling, you can ask Pry to open on an exception that you've caught. For this to work you must be inside a Pry::rescue{ } block.

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

Pry::rescue{ test }

Peeking

Sometimes bugs in your program don't cause exceptions. Instead your program just gets stuck. Examples include infinite loops, slow network calls, or tests that take a surprisingly long time to run.

In this case it's useful to be able to open a Pry console when you notice that your program is not going anywhere. To do this, send your process a SIGQUIT using <ctrl+\>.

cirwin@localhost:/tmp/pry $ ruby examples/loop.rb
^\
Preparing to peek via pry!
Frame number: 0/4

From: ./examples/loop.rb @ line 10 Object#r
    10: def r
    11:   some_var = 13
    12:   loop do
 => 13:     x = File.readlines('lib/pry-rescue.rb')
    14:   end
    15: end
pry (main)>

Advanced peeking

You can configure which signal pry-rescue listens for by default by exporting the PRY_PEEK environment variable that suits your use-case best:

export PRY_PEEK=""    # don't autopeek at all
export PRY_PEEK=INT   # peek on SIGINT (<ctrl+c>)
export PRY_PEEK=QUIT  # peek on SIGQUIT
export PRY_PEEK=USR1  # peek on SIGUSR1
export PRY_PEEK=USR2  # peek on SIGUSR2
export PRY_PEEK=EXIT  # peek on program exit

If it's only important for one program, then you can also set the environment variable in Ruby before requiring pry-rescue:

ENV['PRY_PEEK'] = '' # disable SIGQUIT handler
require "pry-rescue"

Finally, you can enable peeking into programs that do not include pry-rescue by configuring Ruby to always load one (or several) of these files:

export RUBYOPT=-rpry-rescue/peek/int   # peek on SIGINT (<ctrl-c>)
export RUBYOPT=-rpry-rescue/peek/quit  # peek on SIGQUIT (<ctrl-\>)
export RUBYOPT=-rpry-rescue/peek/usr1  # peek on SIGUSR1
export RUBYOPT=-rpry-rescue/peek/usr2  # peek on SIGUSR2
export RUBYOPT=-rpry-rescue/peek/exit  # peek on program exit

These last examples relies on having pry-rescue in the load path (i.e. at least in the gemset, or Gemfile of the program). If that is not true, you can use absolute paths. The hook files do not require the whole of pry-rescue, nor is any of Pry itself loaded until you trigger the signal.

export RUBYOPT=-r/home/cirwin/src/pry-rescue/lib/pry-rescue/peek/usr2

Known bugs

  • Ruby 2.0, 1.9.3, 1.9.2 – no known bugs
  • Ruby 1.9.1 — not supported
  • Ruby 1.8.7 — occasional incorrect values for self
  • REE 1.8.7 — no known bugs
  • JRuby 1.7 (1.8 mode and 1.9 mode) — no known bugs
  • JRuby 1.6 (1.8 mode and 1.9 mode) — incorrect value for self in NoMethodErrors
  • Rubinius (1.8 mode and 1.9 mode) – does not catch some low-level errors (e.g. ZeroDivisionError)

Meta-fu

Released under the MIT license, see LICENSE.MIT for details. Contributions and bug-reports are welcome.

More Repositories

1

showterm

The rubygem to upload to http://showterm.io
Ruby
600
star
2

aws-name-server

DNS server that lets you look up ec2 instances by instance name
Go
560
star
3

vim-bracketed-paste

Handles bracketed-paste-mode in vim (aka. automatic `:set paste`)
Vim Script
481
star
4

showterm.io

The website for showterm!
JavaScript
354
star
5

async-profile

Asynchronous CPU profiling for node
CoffeeScript
264
star
6

dotgpg

A secure and easy-to-use store for your production secrets
Ruby
162
star
7

jist

Jist just gists
Ruby
160
star
8

git-aliae

Random git stuff....
Shell
81
star
9

console.log

Log to the Javascript console from ruby.
Ruby
79
star
10

font

golang parser for OpenType files.
Go
70
star
11

em-imap

An event machine based IMAP client
Ruby
69
star
12

lspace

Safe operation-local global variables!
Ruby
62
star
13

pry-debundle

Allows you to use gems not in your Gemfile from Pry.
Ruby
50
star
14

gpg-decoder

A port of the awesome ASN.1 Javascript Decoder for GPG messages
JavaScript
44
star
15

ruby-source_map

A Ruby library for interacting with the awesome javascript SourceMaps.
Ruby
40
star
16

trie-ing

The fastest weighted auto-completion trie known to...
JavaScript
33
star
17

unicode-dragon

Eats invalid unicode for breakfast.
JavaScript
25
star
18

interception

Listen to raise in ruby
Ruby
24
star
19

twitter-followers

A tool to download all a user's twitter followers
Go
22
star
20

http_load

hard-core HTTP load tester from http://acme.com/software/http_load/
C
20
star
21

pry-syntax-hacks

Some syntactic "high fructose corn syrup"s for pry.
Ruby
16
star
22

zepto-ghostclick

A Zepto plugin to help avoid ghost clicks
JavaScript
14
star
23

rfc2047-ruby

An RFC 2047 compliant email header parser
Ruby
11
star
24

motion-rubygems

Very very broken support for rubygems in RubyMotion
Ruby
11
star
25

pry-highlight

Highlights strings in pry output
Ruby
9
star
26

cap-runit

Capistrano 3 runit support
Ruby
9
star
27

gotar

A replacement for `go build` that includes static assets
Go
9
star
28

cause

A backport of Exception#cause from Ruby-2.1.0
Ruby
9
star
29

golo

defer go compile errors to runtime
Go
8
star
30

pry-em

Playing with async stuff is as easy as pry!
Ruby
8
star
31

ruby18_source_location

Giving Ruby 1.8.7 a chance to join in the #source_location fun.
Ruby
8
star
32

conradirwin.github.com

My Blog.
HTML
6
star
33

mongoid-rails

Strong parameters integration with mongoid
Ruby
6
star
34

yada_yada

A reimplementation of perl's Yada Yada operator in ruby!
Ruby
6
star
35

pry-capture

The old version of pry-rescue!
Ruby
5
star
36

encoding-codepage

A rubygem that lets you look up encodings by Microsoft® Code Page Identifier
Ruby
5
star
37

thin-attach_socket

Adds Thin::Backend::AttachSocket for running thin behind einhorn
Ruby
4
star
38

vim-comment-object

Perform actions on an entire comment.
Vim Script
4
star
39

wiktionary

A python library for playing with Wiktionary
Python
4
star
40

go-dwarf

DWARF debugging data parser in go
Go
4
star
41

self

Provides debug-access to private methods and instance variables of ruby Objects
Ruby
4
star
42

optimistic_dev

An optimistic developer twitter persona!
Go
4
star
43

secure_equals

Constant time equality for ruby.
Ruby
4
star
44

canvas-animation-loader

GPU-accelerated canvas animations in webpack
JavaScript
3
star
45

rbenv-all

Provides "rbenv all" for running one command against all your rubies
Shell
3
star
46

p_enwikt

git fork of https://fisheye.toolserver.org/browse/enwikt/
Perl
3
star
47

git-fetch-series

A tool to let you download patches from the git mailing list.
Python
3
star
48

c_location

source_location for methods written in C (Method#c_location)
Ruby
3
star
49

autoconfig.mozillamessaging.com

Mirror of http://svn.mozilla.org/mozillamessaging.com/sites/autoconfig.mozillamessaging.com, Mozilla's ISPDB
DIGITAL Command Language
3
star
50

pry-lexer

A pygments lexer for pry sessions!
Python
3
star
51

proxies

Ruby
2
star
52

jslint-node

Run jslint with node
JavaScript
2
star
53

mruby-example

"Hello world!" executable with mruby
Ruby
2
star
54

bisect

Library for maintaining sorted Arrays borrowed from Python
Ruby
2
star
55

go-for-rubyists

Go for rubyists, a tech talk.
JavaScript
2
star
56

amfs

Go
2
star
57

fireplace

Go
2
star
58

howtobuilda.bike

A writeup of building a bike
CSS
2
star
59

Commune

Ruby
2
star
60

libxml2

My fork of git://git.gnome.org/libxml2
C
2
star
61

motion-rubygems-example

An example project that uses motion-rubygems
Ruby
2
star
62

better-gmail

clean up gmail interface
JavaScript
1
star
63

node-zset

Disk backed semi-sorted sets
CoffeeScript
1
star
64

test

1
star
65

bytes

A small utility for dealing with raw bytes
Go
1
star
66

cap3-elb

Capistrano commands for managing your ELB.
Ruby
1
star
67

qrcode-raplet

A raplet to make it easier to phone people
JavaScript
1
star
68

udt

Ideas for CRDTs
Go
1
star
69

mrsa

Mediated RSA in golang
Go
1
star
70

em-monitor

For monitoring the distribution of CPU-spans in your event machine reactor thread.
Ruby
1
star
71

parallel

Structured concurrency for go
Go
1
star
72

browser-action-open

JavaScript
1
star
73

plotocrat

Automatically plot probability distributions
JavaScript
1
star
74

code-point-mapping

Map between javascript string indices and unicode code point offsets effectively
TypeScript
1
star
75

ggg

An experimental self-hosted debugger for go
Go
1
star
76

music

LilyPond
1
star
77

source_map-jsmin

A Source Map enabled pure-ruby version of Doug Crockford's awesome jsmin
JavaScript
1
star
78

goer

Some people like to browse, other's prefer to go directly.
JavaScript
1
star
79

introducing-teacup

The really simple sample app created in http://cirw.in/blog/introducing-teacup
Ruby
1
star
80

beamer-bootstrap

A bootstrap for beamer presentations that include code and images.
C
1
star
81

airpad

An iPad client for the awesome http://airbrake.io/
Objective-C
1
star
82

tracing-bug

Rust
1
star
83

lxterminal-clicky

A terminal emulator you can click on
C
1
star