模块 ErrorHighlight
常量
- VERSION
公共类方法
源代码
# File lib/error_highlight/formatter.rb, line 67 def self.formatter Ractor.current[:__error_highlight_formatter__] || DefaultFormatter end
源代码
# File lib/error_highlight/formatter.rb, line 71 def self.formatter=(formatter) Ractor.current[:__error_highlight_formatter__] = formatter end
源代码
# 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 = begin RubyVM::AbstractSyntaxTree.of(loc, keep_script_lines: true) rescue RuntimeError => error # RubyVM::AbstractSyntaxTree.of raises an error with a message that # includes "prism" when the ISEQ was compiled with the prism compiler. # In this case, we'll try to parse again with prism instead. raise unless error.message.include?("prism") prism_find(loc) end Spotter.new(node, **opts).spot when RubyVM::AbstractSyntaxTree::Node, Prism::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
仅支持单行代码片段。因此,如果返回值不为 nil,则 first_lineno 和 last_lineno 将具有相同的值。如果相关的代码片段跨越多行(例如,Array#[]
的 +ary+),该方法将返回 nil。未来可能会移除此限制。
私有类方法
源代码
# File lib/error_highlight/base.rb, line 86 def self.prism_find(location) require "prism" return nil if Prism::VERSION < "1.0.0" absolute_path = location.absolute_path return unless absolute_path node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(location) Prism.parse_file(absolute_path).value.breadth_first_search { |node| node.node_id == node_id } end
接受一个 Thread::Backtrace::Location
对象,并返回一个与源代码中回溯位置对应的 Prism::Node
。