Exception: JSONP3::Path::Error

Inherits:
Error
  • Object
show all
Defined in:
lib/json_p3/errors.rb

Overview

Base class for all JSONPath errors.

Direct Known Subclasses

NameError, RecursionError, SyntaxError, TypeError

Constant Summary collapse

FULL_MESSAGE =
((RUBY_VERSION.split(".")&.map(&:to_i) <=> [3, 2, 0]) || -1) < 1

Instance Method Summary collapse

Constructor Details

#initialize(msg, token, query) ⇒ Error

Returns a new instance of Error.



12
13
14
15
16
# File 'lib/json_p3/errors.rb', line 12

def initialize(msg, token, query)
  super(msg)
  @token = token
  @query = query
end

Instance Method Details

#detailed_message(highlight: true, **_kwargs) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/json_p3/errors.rb', line 18

def detailed_message(highlight: true, **_kwargs)
  if @query.strip.empty?
    "empty query"
  else
    value = JSONP3::Path.get_token_value(@token, @query)
    lines = @query[...@token[1]]&.lines or [""] # pleasing the type checker
    lineno = lines.length
    col = lines.last.length
    pad = " " * lineno.to_s.length
    pointer = (" " * col) + ("^" * [value.length, 1].max)
    <<~ENDOFMESSAGE.strip
      #{self.class}: #{message}
      #{pad} -> '#{@query}' #{lineno}:#{col}
      #{pad} |
      #{lineno} | #{@query}
      #{pad} | #{pointer} #{highlight ? "\e[1m#{message}\e[0m" : message}
    ENDOFMESSAGE
  end
end

#full_message(highlight: true, order: :top) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/json_p3/errors.rb', line 38

def full_message(highlight: true, order: :top)
  if FULL_MESSAGE
    # For Ruby < 3.2.0
    "#{super}\n#{detailed_message(highlight: highlight, order: order)}"
  else
    super
  end
end