Exception Details

Exception Details causes instances of Exception to capture a binding at creation-time. It provides convenience methods on Exceptions to inspect all the variables and values from the time of the Exception and adds an informative log string.

Features/benefits:

* Get detail (variables/values) about your Exception circumstances *without* reproducing the problem first.

* Reduced need for debug/logging code to find out what went wrong.

* Minimize ambiguous errors (which variable was nil?).

* A single statement method for a loggable string (avoid repeating exception log string creation)

* Get Pry like debug knowledge from cron job errors and running systems from the log, without being there.

* Pry type information with less overhead/dependencies--binding_of_caller is the only dependency.

Example Usage

begin
    greeting = 'hello'
    @name = nil
    puts greeting + @name
rescue Exception => e
    puts e.details
    puts e.inspect_variables
end


e.details ->

    Exception:
        TypeError: can't convert nil into String
    Variables:
        <String> greeting                   = "hello"
        <NilClass> @name                    = nil

    Backtrace:
        /Users/someguy/apps/exception_details/spec/exception_details_spec.rb:20:in `+'
    /Users/someguy/apps/exception_details/spec/exception_details_spec.rb:20:in `block (3 levels) in <top (required)>'
    /Users/someguy/.rvm/gems/ruby-1.9.3-p374/gems/rspec-core-2.14.3/lib/rspec/core/example.rb:114:in `instance_eval'
    and so forth ...


e.inspect_variables ->

    <String> greeting = "hello"
    <TypeError> e = #<TypeError: can't convert nil into String>
    <NilClass> @name = nil

Or access the variables in the binding yourself...

e.binding_during_exception.eval("puts #{greeting}")

Installation

Add to your Gemfile:

gem 'exception_details'

Execute:

$ bundle

Require it:

require 'exception_details'

Limitations

  • This gem requires [binding_of_caller]:https://github.com/banister/binding_of_caller, so it should only work with MRI 1.9.2, 1.9.3, 2.0 and RBX (Rubinius). Does not work in 1.8.7, but there is a well known (continuation-based) hack to get a Binding#of_caller there. There is some mention about binding of caller supporting jruby, so feel free to try it out.

  • Getting a binding from a NameError seems to be problematic.

  • This gem is still new...

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request