File: error_handling_test.rb

package info (click to toggle)
ruby-liquid 5.4.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,176 kB
  • sloc: ruby: 10,561; makefile: 6
file content (272 lines) | stat: -rw-r--r-- 8,949 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# frozen_string_literal: true

require 'test_helper'

class ErrorHandlingTest < Minitest::Test
  include Liquid

  def test_templates_parsed_with_line_numbers_renders_them_in_errors
    template = <<-LIQUID
      Hello,

      {{ errors.standard_error }} will raise a standard error.

      Bla bla test.

      {{ errors.syntax_error }} will raise a syntax error.

      This is an argument error: {{ errors.argument_error }}

      Bla.
    LIQUID

    expected = <<-TEXT
      Hello,

      Liquid error (line 3): standard error will raise a standard error.

      Bla bla test.

      Liquid syntax error (line 7): syntax error will raise a syntax error.

      This is an argument error: Liquid error (line 9): argument error

      Bla.
    TEXT

    output = Liquid::Template.parse(template, line_numbers: true).render('errors' => ErrorDrop.new)
    assert_equal(expected, output)
  end

  def test_standard_error
    template = Liquid::Template.parse(' {{ errors.standard_error }} ')
    assert_equal(' Liquid error: standard error ', template.render('errors' => ErrorDrop.new))

    assert_equal(1, template.errors.size)
    assert_equal(StandardError, template.errors.first.class)
  end

  def test_syntax
    template = Liquid::Template.parse(' {{ errors.syntax_error }} ')
    assert_equal(' Liquid syntax error: syntax error ', template.render('errors' => ErrorDrop.new))

    assert_equal(1, template.errors.size)
    assert_equal(SyntaxError, template.errors.first.class)
  end

  def test_argument
    template = Liquid::Template.parse(' {{ errors.argument_error }} ')
    assert_equal(' Liquid error: argument error ', template.render('errors' => ErrorDrop.new))

    assert_equal(1, template.errors.size)
    assert_equal(ArgumentError, template.errors.first.class)
  end

  def test_missing_endtag_parse_time_error
    assert_raises(Liquid::SyntaxError) do
      Liquid::Template.parse(' {% for a in b %} ... ')
    end
  end

  def test_unrecognized_operator
    with_error_mode(:strict) do
      assert_raises(SyntaxError) do
        Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ')
      end
    end
  end

  def test_lax_unrecognized_operator
    template = Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ', error_mode: :lax)
    assert_equal(' Liquid error: Unknown operator =! ', template.render)
    assert_equal(1, template.errors.size)
    assert_equal(Liquid::ArgumentError, template.errors.first.class)
  end

  def test_with_line_numbers_adds_numbers_to_parser_errors
    err = assert_raises(SyntaxError) do
      Liquid::Template.parse('
          foobar

          {% "cat" | foobar %}

          bla
        ',
        line_numbers: true)
    end

    assert_match(/Liquid syntax error \(line 4\)/, err.message)
  end

  def test_with_line_numbers_adds_numbers_to_parser_errors_with_whitespace_trim
    err = assert_raises(SyntaxError) do
      Liquid::Template.parse('
          foobar

          {%- "cat" | foobar -%}

          bla
        ',
        line_numbers: true)
    end

    assert_match(/Liquid syntax error \(line 4\)/, err.message)
  end

  def test_parsing_warn_with_line_numbers_adds_numbers_to_lexer_errors
    template = Liquid::Template.parse('
        foobar

        {% if 1 =! 2 %}ok{% endif %}

        bla
            ',
      error_mode: :warn,
      line_numbers: true)

    assert_equal(['Liquid syntax error (line 4): Unexpected character = in "1 =! 2"'],
      template.warnings.map(&:message))
  end

  def test_parsing_strict_with_line_numbers_adds_numbers_to_lexer_errors
    err = assert_raises(SyntaxError) do
      Liquid::Template.parse('
          foobar

          {% if 1 =! 2 %}ok{% endif %}

          bla
                ',
        error_mode: :strict,
        line_numbers: true)
    end

    assert_equal('Liquid syntax error (line 4): Unexpected character = in "1 =! 2"', err.message)
  end

  def test_syntax_errors_in_nested_blocks_have_correct_line_number
    err = assert_raises(SyntaxError) do
      Liquid::Template.parse('
          foobar

          {% if 1 != 2 %}
            {% foo %}
          {% endif %}

          bla
                ',
        line_numbers: true)
    end

    assert_equal("Liquid syntax error (line 5): Unknown tag 'foo'", err.message)
  end

  def test_strict_error_messages
    err = assert_raises(SyntaxError) do
      Liquid::Template.parse(' {% if 1 =! 2 %}ok{% endif %} ', error_mode: :strict)
    end
    assert_equal('Liquid syntax error: Unexpected character = in "1 =! 2"', err.message)

    err = assert_raises(SyntaxError) do
      Liquid::Template.parse('{{%%%}}', error_mode: :strict)
    end
    assert_equal('Liquid syntax error: Unexpected character % in "{{%%%}}"', err.message)
  end

  def test_warnings
    template = Liquid::Template.parse('{% if ~~~ %}{{%%%}}{% else %}{{ hello. }}{% endif %}', error_mode: :warn)
    assert_equal(3, template.warnings.size)
    assert_equal('Unexpected character ~ in "~~~"', template.warnings[0].to_s(false))
    assert_equal('Unexpected character % in "{{%%%}}"', template.warnings[1].to_s(false))
    assert_equal('Expected id but found end_of_string in "{{ hello. }}"', template.warnings[2].to_s(false))
    assert_equal('', template.render)
  end

  def test_warning_line_numbers
    template = Liquid::Template.parse("{% if ~~~ %}\n{{%%%}}{% else %}\n{{ hello. }}{% endif %}", error_mode: :warn, line_numbers: true)
    assert_equal('Liquid syntax error (line 1): Unexpected character ~ in "~~~"', template.warnings[0].message)
    assert_equal('Liquid syntax error (line 2): Unexpected character % in "{{%%%}}"', template.warnings[1].message)
    assert_equal('Liquid syntax error (line 3): Expected id but found end_of_string in "{{ hello. }}"', template.warnings[2].message)
    assert_equal(3, template.warnings.size)
    assert_equal([1, 2, 3], template.warnings.map(&:line_number))
  end

  # Liquid should not catch Exceptions that are not subclasses of StandardError, like Interrupt and NoMemoryError
  def test_exceptions_propagate
    assert_raises(Exception) do
      template = Liquid::Template.parse('{{ errors.exception }}')
      template.render('errors' => ErrorDrop.new)
    end
  end

  def test_default_exception_renderer_with_internal_error
    template = Liquid::Template.parse('This is a runtime error: {{ errors.runtime_error }}', line_numbers: true)

    output = template.render('errors' => ErrorDrop.new)

    assert_equal('This is a runtime error: Liquid error (line 1): internal', output)
    assert_equal([Liquid::InternalError], template.errors.map(&:class))
  end

  def test_setting_default_exception_renderer
    old_exception_renderer = Liquid::Template.default_exception_renderer
    exceptions = []
    Liquid::Template.default_exception_renderer = ->(e) {
      exceptions << e
      ''
    }
    template = Liquid::Template.parse('This is a runtime error: {{ errors.argument_error }}')

    output = template.render('errors' => ErrorDrop.new)

    assert_equal('This is a runtime error: ', output)
    assert_equal([Liquid::ArgumentError], template.errors.map(&:class))
  ensure
    Liquid::Template.default_exception_renderer = old_exception_renderer if old_exception_renderer
  end

  def test_exception_renderer_exposing_non_liquid_error
    template   = Liquid::Template.parse('This is a runtime error: {{ errors.runtime_error }}', line_numbers: true)
    exceptions = []
    handler    = ->(e) {
      exceptions << e
      e.cause
    }

    output = template.render({ 'errors' => ErrorDrop.new }, exception_renderer: handler)

    assert_equal('This is a runtime error: runtime error', output)
    assert_equal([Liquid::InternalError], exceptions.map(&:class))
    assert_equal(exceptions, template.errors)
    assert_equal('#<RuntimeError: runtime error>', exceptions.first.cause.inspect)
  end

  class TestFileSystem
    def read_template_file(_template_path)
      "{{ errors.argument_error }}"
    end
  end

  def test_included_template_name_with_line_numbers
    old_file_system = Liquid::Template.file_system

    begin
      Liquid::Template.file_system = TestFileSystem.new

      template = Liquid::Template.parse("Argument error:\n{% include 'product' %}", line_numbers: true)
      page     = template.render('errors' => ErrorDrop.new)
    ensure
      Liquid::Template.file_system = old_file_system
    end
    assert_equal("Argument error:\nLiquid error (product line 1): argument error", page)
    assert_equal("product", template.errors.first.template_name)
  end

  def test_bug_compatible_silencing_of_errors_in_blank_nodes
    output = Liquid::Template.parse("{% assign x = 0 %}{% if 1 < '2' %}not blank{% assign x = 3 %}{% endif %}{{ x }}").render
    assert_equal("Liquid error: comparison of Integer with String failed0", output)

    output = Liquid::Template.parse("{% assign x = 0 %}{% if 1 < '2' %}{% assign x = 3 %}{% endif %}{{ x }}").render
    assert_equal("0", output)
  end
end