mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-18 02:10:41 -07:00
feature: Allow a default exception renderer to be specified (#837)
This could be used to preserve the old default of rendering non-Liquid::Error messages or for providing default behaviour like error reporting which could be missed if the exception renderer needed to be specified on each render.
This commit is contained in:
committed by
GitHub
parent
fae3a2de7b
commit
869dbc7ebf
@@ -27,6 +27,7 @@ module Liquid
|
|||||||
|
|
||||||
@this_stack_used = false
|
@this_stack_used = false
|
||||||
|
|
||||||
|
self.exception_renderer = Template.default_exception_renderer
|
||||||
if rethrow_errors
|
if rethrow_errors
|
||||||
self.exception_renderer = ->(e) { raise }
|
self.exception_renderer = ->(e) { raise }
|
||||||
end
|
end
|
||||||
@@ -78,9 +79,7 @@ module Liquid
|
|||||||
e.template_name ||= template_name
|
e.template_name ||= template_name
|
||||||
e.line_number ||= line_number
|
e.line_number ||= line_number
|
||||||
errors.push(e)
|
errors.push(e)
|
||||||
|
exception_renderer.call(e).to_s
|
||||||
e = exception_renderer.call(e) if exception_renderer
|
|
||||||
e.to_s
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def invoke(method, *args)
|
def invoke(method, *args)
|
||||||
|
|||||||
@@ -69,6 +69,11 @@ module Liquid
|
|||||||
# :error raises an error when tainted output is used
|
# :error raises an error when tainted output is used
|
||||||
attr_writer :taint_mode
|
attr_writer :taint_mode
|
||||||
|
|
||||||
|
attr_accessor :default_exception_renderer
|
||||||
|
Template.default_exception_renderer = lambda do |exception|
|
||||||
|
exception
|
||||||
|
end
|
||||||
|
|
||||||
def file_system
|
def file_system
|
||||||
@@file_system
|
@@file_system
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -211,6 +211,20 @@ class ErrorHandlingTest < Minitest::Test
|
|||||||
assert_equal [Liquid::InternalError], template.errors.map(&:class)
|
assert_equal [Liquid::InternalError], template.errors.map(&:class)
|
||||||
end
|
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
|
def test_exception_renderer_exposing_non_liquid_error
|
||||||
template = Liquid::Template.parse('This is a runtime error: {{ errors.runtime_error }}', line_numbers: true)
|
template = Liquid::Template.parse('This is a runtime error: {{ errors.runtime_error }}', line_numbers: true)
|
||||||
exceptions = []
|
exceptions = []
|
||||||
|
|||||||
Reference in New Issue
Block a user