Spec-Zone.ru › Ruby 3.3

модуль ErrorHighlight

Константы

VERSION

Публичные методы класса

formatter() Показать исходный код
# File lib/error_highlight/formatter.rb, line 16
def self.formatter
  Ractor.current[:__error_highlight_formatter__] || DefaultFormatter
end
formatter=(formatter) Показать исходный код
# File lib/error_highlight/formatter.rb, line 20
def self.formatter=(formatter)
  Ractor.current[:__error_highlight_formatter__] = formatter
end
spot(obj, **opts) Показать исходный код
# File lib/error_highlight/base.rb, line 33
def self.spot(obj, **opts)
  case obj
  when Exception
    exc = obj
    loc = opts[:backtrace_location]
    opts = { point_type: opts.fetch(:point_type, :name) }

    unless loc
      case exc
      when TypeError, ArgumentError
        opts[:point_type] = :args
      end

      locs = exc.backtrace_locations
      return nil unless locs

      loc = locs.first
      return nil unless loc

      opts[:name] = exc.name if NameError === obj
    end

    return nil unless Thread::Backtrace::Location === loc

    node = RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true)

    Spotter.new(node, **opts).spot

  when RubyVM::AbstractSyntaxTree::Node
    Spotter.new(obj, **opts).spot

  else
    raise TypeError, "Exception is expected"
  end

rescue SyntaxError,
       SystemCallError, # file not found or something
       ArgumentError # eval'ed code

  return nil
end

Определите фрагмент кода, в котором произошла данная ошибка.

Параметры:

point_type: :name | :args

:name (default) points the method/variable name that the exception occurred.
:args points the arguments of the method call that the exception occurred.

backtrace_location: Thread::Backtrace::Location

It locates the code fragment of the given backtrace_location.
By default, it uses the first frame of backtrace_locations of the given exception.

Возвращаемое значение:

{
  first_lineno: Integer,
  first_column: Integer,
  last_lineno: Integer,
  last_column: Integer,
  snippet: String,
  script_lines: [String],
} | nil

Ограничения:

В настоящее время ErrorHighlight.spot поддерживает только фрагмент кода из одной строки. Поэтому, если возвращаемое значение не пустое, first_lineno и last_lineno будут иметь одинаковые значения. Если фрагмент кода, относящийся к ошибке, занимает несколько строк (например, Array#[] массива +ary+), метод вернёт пустое значение. Это ограничение может быть снято в будущем.

Ruby Core © 1993–2022 Yukihiro Matsumoto
Licensed under the Ruby License.
Ruby Standard Library © contributors
Licensed under their own licenses.

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API