mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-19 02:40:41 -07:00
Allow the exception handler to convert exceptions to hide error messages.
This commit is contained in:
+17
-5
@@ -13,7 +13,7 @@ module Liquid
|
|||||||
# context['bob'] #=> nil class Context
|
# context['bob'] #=> nil class Context
|
||||||
class Context
|
class Context
|
||||||
attr_reader :scopes, :errors, :registers, :environments, :resource_limits
|
attr_reader :scopes, :errors, :registers, :environments, :resource_limits
|
||||||
attr_accessor :exception_handler, :render_errors
|
attr_accessor :exception_handler
|
||||||
|
|
||||||
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil)
|
def initialize(environments = {}, outer_scope = {}, registers = {}, rethrow_errors = false, resource_limits = nil)
|
||||||
@environments = [environments].flatten
|
@environments = [environments].flatten
|
||||||
@@ -22,12 +22,11 @@ module Liquid
|
|||||||
@errors = []
|
@errors = []
|
||||||
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
|
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
|
||||||
squash_instance_assigns_with_environments
|
squash_instance_assigns_with_environments
|
||||||
@render_errors = true
|
|
||||||
|
|
||||||
@this_stack_used = false
|
@this_stack_used = false
|
||||||
|
|
||||||
if rethrow_errors
|
if rethrow_errors
|
||||||
self.exception_handler = ->(e) { true }
|
self.exception_handler = ->(e) { raise }
|
||||||
end
|
end
|
||||||
|
|
||||||
@interrupts = []
|
@interrupts = []
|
||||||
@@ -68,9 +67,22 @@ module Liquid
|
|||||||
e.set_line_number_from_token(token)
|
e.set_line_number_from_token(token)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
output = nil
|
||||||
|
|
||||||
|
if exception_handler
|
||||||
|
result = exception_handler.call(e)
|
||||||
|
case result
|
||||||
|
when Exception
|
||||||
|
e = result
|
||||||
|
e.set_line_number_from_token(token) if e.is_a?(Liquid::Error)
|
||||||
|
when String
|
||||||
|
output = result
|
||||||
|
else
|
||||||
|
raise if result
|
||||||
|
end
|
||||||
|
end
|
||||||
errors.push(e)
|
errors.push(e)
|
||||||
raise if exception_handler && exception_handler.call(e)
|
output || Liquid::Error.render(e)
|
||||||
render_errors ? Liquid::Error.render(e) : ''
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def invoke(method, *args)
|
def invoke(method, *args)
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ module Liquid
|
|||||||
locale: I18n.new
|
locale: I18n.new
|
||||||
}
|
}
|
||||||
|
|
||||||
attr_accessor :root, :render_errors
|
attr_accessor :root
|
||||||
attr_reader :resource_limits
|
attr_reader :resource_limits
|
||||||
|
|
||||||
@@file_system = BlankFileSystem.new
|
@@file_system = BlankFileSystem.new
|
||||||
@@ -168,7 +168,7 @@ module Liquid
|
|||||||
c = args.shift
|
c = args.shift
|
||||||
|
|
||||||
if @rethrow_errors
|
if @rethrow_errors
|
||||||
c.exception_handler = ->(e) { true }
|
c.exception_handler = ->(e) { raise }
|
||||||
end
|
end
|
||||||
|
|
||||||
c
|
c
|
||||||
@@ -183,8 +183,6 @@ module Liquid
|
|||||||
raise ArgumentError, "Expected Hash or Liquid::Context as parameter"
|
raise ArgumentError, "Expected Hash or Liquid::Context as parameter"
|
||||||
end
|
end
|
||||||
|
|
||||||
context.render_errors = self.render_errors unless self.render_errors.nil?
|
|
||||||
|
|
||||||
case args.last
|
case args.last
|
||||||
when Hash
|
when Hash
|
||||||
options = args.pop
|
options = args.pop
|
||||||
|
|||||||
@@ -186,10 +186,21 @@ class ErrorHandlingTest < Minitest::Test
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_disabling_error_rendering
|
def test_exception_handler_with_string_result
|
||||||
template = Liquid::Template.parse('This is an argument error: {{ errors.argument_error }}')
|
template = Liquid::Template.parse('This is an argument error: {{ errors.argument_error }}')
|
||||||
template.render_errors = false
|
output = template.render({ 'errors' => ErrorDrop.new }, exception_handler: ->(e) { '' })
|
||||||
assert_equal 'This is an argument error: ', template.render('errors' => ErrorDrop.new)
|
assert_equal 'This is an argument error: ', output
|
||||||
assert_equal [ArgumentError], template.errors.map(&:class)
|
assert_equal [ArgumentError], template.errors.map(&:class)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class InternalError < Liquid::Error
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_exception_handler_with_exception_result
|
||||||
|
template = Liquid::Template.parse('This is a runtime error: {{ errors.runtime_error }}')
|
||||||
|
handler = ->(e) { e.is_a?(Liquid::Error) ? e : InternalError.new('internal') }
|
||||||
|
output = template.render({ 'errors' => ErrorDrop.new }, exception_handler: handler)
|
||||||
|
assert_equal 'This is a runtime error: Liquid error: internal', output
|
||||||
|
assert_equal [InternalError], template.errors.map(&:class)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -102,6 +102,10 @@ class ErrorDrop < Liquid::Drop
|
|||||||
raise Liquid::SyntaxError, 'syntax error'
|
raise Liquid::SyntaxError, 'syntax error'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def runtime_error
|
||||||
|
raise RuntimeError, 'runtime error'
|
||||||
|
end
|
||||||
|
|
||||||
def exception
|
def exception
|
||||||
raise Exception, 'exception'
|
raise Exception, 'exception'
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user