Exception: JSONP3::JSONPathError

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

Overview

Base class for JSONPath exceptions that happen when parsing or evaluating a query.

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) ⇒ JSONPathError

Returns a new instance of JSONPathError.



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

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

Instance Method Details

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



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

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

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



35
36
37
38
39
40
41
42
# File 'lib/json_p3/errors.rb', line 35

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