2014-03-17 06:27:43 -04:00
|
|
|
#
|
|
|
|
# {Pry::LastException} is a proxy class who wraps an Exception object for
|
|
|
|
# {Pry#last_exception}. it extends the exception object with methods that
|
|
|
|
# help pry commands be useful.
|
|
|
|
#
|
|
|
|
# the original exception object is not modified and method calls are forwarded
|
|
|
|
# to the wrapped exception object.
|
|
|
|
#
|
2019-02-26 19:22:32 -05:00
|
|
|
class Pry
|
|
|
|
class LastException < BasicObject
|
|
|
|
attr_accessor :bt_index
|
|
|
|
|
2019-03-24 12:06:30 -04:00
|
|
|
def initialize(exception)
|
|
|
|
@exception = exception
|
2019-02-26 19:22:32 -05:00
|
|
|
@bt_index = 0
|
|
|
|
@file, @line = bt_source_location_for(0)
|
|
|
|
end
|
2014-03-09 20:28:12 -04:00
|
|
|
|
2019-02-26 19:22:32 -05:00
|
|
|
def method_missing(name, *args, &block)
|
2019-03-24 12:06:30 -04:00
|
|
|
if @exception.respond_to?(name)
|
|
|
|
@exception.public_send(name, *args, &block)
|
2019-02-26 19:22:32 -05:00
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
2014-03-09 20:28:12 -04:00
|
|
|
end
|
|
|
|
|
2019-02-26 19:22:32 -05:00
|
|
|
def respond_to_missing?(name, include_all = false)
|
2019-03-24 12:06:30 -04:00
|
|
|
@exception.respond_to?(name, include_all)
|
2019-02-26 19:22:32 -05:00
|
|
|
end
|
2014-03-09 20:28:12 -04:00
|
|
|
|
2019-02-26 19:22:32 -05:00
|
|
|
#
|
|
|
|
# @return [String]
|
|
|
|
# returns the path to a file for the current backtrace. see {#bt_index}.
|
|
|
|
#
|
2019-03-02 09:39:17 -05:00
|
|
|
attr_reader :file
|
2014-03-17 06:27:43 -04:00
|
|
|
|
2019-02-26 19:22:32 -05:00
|
|
|
#
|
|
|
|
# @return [Fixnum]
|
|
|
|
# returns the line for the current backtrace. see {#bt_index}.
|
|
|
|
#
|
2019-03-02 09:39:17 -05:00
|
|
|
attr_reader :line
|
2014-03-17 06:27:43 -04:00
|
|
|
|
2019-02-26 19:22:32 -05:00
|
|
|
# @return [Exception]
|
|
|
|
# returns the wrapped exception
|
|
|
|
#
|
|
|
|
def wrapped_exception
|
2019-03-24 12:06:30 -04:00
|
|
|
@exception
|
2019-02-26 19:22:32 -05:00
|
|
|
end
|
2014-03-09 20:28:12 -04:00
|
|
|
|
2019-02-26 19:22:32 -05:00
|
|
|
def bt_source_location_for(index)
|
|
|
|
backtrace[index] =~ /(.*):(\d+)/
|
2019-03-23 06:12:22 -04:00
|
|
|
[::Regexp.last_match(1), ::Regexp.last_match(2).to_i]
|
2019-02-26 19:22:32 -05:00
|
|
|
end
|
2014-03-09 20:28:12 -04:00
|
|
|
|
2019-02-26 19:22:32 -05:00
|
|
|
def inc_bt_index
|
|
|
|
@bt_index = (@bt_index + 1) % backtrace.size
|
|
|
|
end
|
2014-03-09 20:28:12 -04:00
|
|
|
end
|
|
|
|
end
|