Skip to content
This repository

Enter the context of exceptions

branch: master
README.md

DEPRECATION NOTICE: This project has been deprecated in favor of pry-rescue and is no longer supported.

pry-exception_explorer

(C) John Mair (banisterfiend) 2012

Enter the context of exceptions

pry-exception_explorer is an interactive error console for MRI Ruby 1.9.2+ inspired by the Hammertime gem, which was in turn inspired by consoles found in the Lisp and Smalltalk environments. pry-exception_explorer is a plugin for the Pry REPL.

Using pry-exception_explorer we can automatically pull up a Pry session at the point an exception arises and use Pry to inspect the state there to debug (and fix) the problem. We also get access to the entire call stack of the exception and can walk the stack to interactively examine the state in parent frames (using pry-stack_explorer).

Watch the mini-screencast: http://vimeo.com/36061298

Also look at the plymouth project which utilizes pry-exception_explorer to intercept test failures.

Example:

In the Ruby file:

require 'pry-exception_explorer'

def alpha
  name = "john"
  beta
  puts name
end

def beta
  x = "john"
  gamma(x)
end

def gamma(x)
  raise ArgumentError, "x must be a number!" if !x.is_a?(Numeric)
  puts "2 * x = #{2 * x}"
end

EE.wrap do
  alpha
end

The following session starts up:

Frame number: 0/4
Frame type: method

From: /Users/john/ruby/projects/pry-exception_explorer/examples/example_inline.rb @ line 23 in Object#gamma:

    18:   x = "john"
    19:   gamma(x)
    20: end
    21:
    22: def gamma(x)
 => 23:   raise ArgumentError, "x must be a number!" if !x.is_a?(Numeric)
    24:   puts "2 * x = #{2 * x}"
    25: end
    26:
    27: alpha

[1] (pry) main: 0> x
=> "john"
[2] (pry) main: 0> x = 7
=> 7
[3] (pry) main: 0> continue-exception

Since we fixed the problem (invalid type for x local) we can continue-exception, and have the method continue with the amended x:

PROGRAM OUTPUT:

2 * x = 14

john

Features and limitations

Features

  • Puts you in context of exception.
  • Makes entire call stack accessible (useful for drilling down to precise cause of error).
  • Allows you to 'continue' from exception, recovering from error (continue-exception command)
  • Can now intercept C exceptions (exceptions raised internally such as NoMethodErrors, etc)
  • Let's you assert over state of entire stack when determining whether an exception should be intercepted.
  • Let's you start the session on any stack frame.

Limitations

  • Only works on Ruby 1.9.2+ (including 1.9.3) MRI.

Contact

Problems or questions contact me at github

License

(The MIT License)

Copyright (c) 2012 John Mair (banisterfiend)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Something went wrong with that request. Please try again.