Sha256: af1358b2f57582688bfec34efa11add71830965888b9224528b35679a8fa5ff7

Contents?: true

Size: 1.29 KB

Versions: 11

Compression:

Stored size: 1.29 KB

Contents

# 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

Version data entries

11 entries across 11 versions & 3 rubygems

Version Path
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/web-console-4.2.1/lib/web_console/exception_mapper.rb
web-console-4.2.1 lib/web_console/exception_mapper.rb
web-console-4.2.0 lib/web_console/exception_mapper.rb
web-console-4.1.0 lib/web_console/exception_mapper.rb
web-console-4.0.4 lib/web_console/exception_mapper.rb
web-console-4.0.3 lib/web_console/exception_mapper.rb
web-console-4.0.2 lib/web_console/exception_mapper.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/web-console-4.0.1/lib/web_console/exception_mapper.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/web-console-4.0.1/lib/web_console/exception_mapper.rb
web-console-4.0.1 lib/web_console/exception_mapper.rb
web-console-4.0.0 lib/web_console/exception_mapper.rb