summaryrefslogtreecommitdiff
path: root/lib/pry/last_exception.rb
blob: 4cc0f56b94ee869d9012c6f3d77c52cc1ee8be85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# frozen_string_literal: true

#
# {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.
#
class Pry
  class LastException < BasicObject
    attr_accessor :bt_index

    def initialize(exception)
      @exception = exception
      @bt_index = 0
      @file, @line = bt_source_location_for(0)
    end

    def method_missing(name, *args, &block)
      if @exception.respond_to?(name)
        @exception.public_send(name, *args, &block)
      else
        super
      end
    end

    def respond_to_missing?(name, include_all = false)
      @exception.respond_to?(name, include_all)
    end

    #
    # @return [String]
    #  returns the path to a file for the current backtrace. see {#bt_index}.
    #
    attr_reader :file

    #
    # @return [Fixnum]
    #  returns the line for the current backtrace. see {#bt_index}.
    #
    attr_reader :line

    # @return [Exception]
    #   returns the wrapped exception
    #
    def wrapped_exception
      @exception
    end

    def bt_source_location_for(index)
      backtrace[index] =~ /(.*):(\d+)/
      [::Regexp.last_match(1), ::Regexp.last_match(2).to_i]
    end

    def inc_bt_index
      @bt_index = (@bt_index + 1) % backtrace.size
    end
  end
end