Exception: Liquid2::LiquidError

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

Overview

The base class for all Liquid errors.

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message, token = nil, template_name: nil) ⇒ LiquidError

Returns a new instance of LiquidError.



10
11
12
13
14
15
# File 'lib/liquid2/errors.rb', line 10

def initialize(message, token = nil, template_name: nil)
  super(message)
  @token = token
  @template_name = template_name
  @source = nil
end

Instance Attribute Details

#sourceObject

Returns the value of attribute source.



6
7
8
# File 'lib/liquid2/errors.rb', line 6

def source
  @source
end

#template_nameObject

Returns the value of attribute template_name.



6
7
8
# File 'lib/liquid2/errors.rb', line 6

def template_name
  @template_name
end

#tokenObject

Returns the value of attribute token.



6
7
8
# File 'lib/liquid2/errors.rb', line 6

def token
  @token
end

Instance Method Details

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



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

def detailed_message(highlight: true, **kwargs)
  return super unless @source.is_a?(String) && @token

  _kind, value, index = @token || raise
  line, col, current_line = error_context(@source || raise, index)

  name_and_position = if @template_name
                        "#{@template_name}:#{line}:#{col}"
                      else
                        "#{current_line.inspect}:#{line}:#{col}"
                      end

  pad = " " * line.to_s.length
  pointer = (" " * col) + ("^" * (value&.length || 1))

  <<~MESSAGE.strip
    #{self.class}: #{message}
    #{pad} -> #{name_and_position}
    #{pad} |
    #{line} | #{current_line}
    #{pad} | #{pointer} #{highlight ? "\e[1m#{message}\e[0m" : message}
  MESSAGE
end

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



41
42
43
44
45
46
47
48
# File 'lib/liquid2/errors.rb', line 41

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