File: exception_mapper.rb

package info (click to toggle)
ruby-web-console 4.2.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 632 kB
  • sloc: ruby: 1,496; javascript: 497; sh: 19; makefile: 4
file content (56 lines) | stat: -rw-r--r-- 1,319 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

module WebConsole
  class ExceptionMapper
    attr_reader :exc

    def self.follow(exc)
      mappers = [new(exc)]

      while cause = (cause || exc).cause
        mappers << new(cause)
      end

      mappers
    end

    def self.find_binding(mappers, exception_object_id)
      mappers.detect do |exception_mapper|
        exception_mapper.exc.object_id == exception_object_id.to_i
      end || mappers.first
    end

    def initialize(exception)
      @backtrace = exception.backtrace
      @bindings = exception.bindings
      @exc = exception
    end

    def first
      guess_the_first_application_binding || @bindings.first
    end

    def [](index)
      guess_binding_for_index(index) || @bindings[index]
    end

    private

      def guess_binding_for_index(index)
        file, line = @backtrace[index].to_s.split(":")
        line = line.to_i

        @bindings.find do |binding|
          source_location = SourceLocation.new(binding)
          source_location.path == file && source_location.lineno == line
        end
      end

      def guess_the_first_application_binding
        @bindings.find do |binding|
          source_location = SourceLocation.new(binding)
          source_location.path.to_s.start_with?(Rails.root.to_s)
        end
      end
  end
end