mirror of
https://github.com/kemko/liquid.git
synced 2026-01-04 17:25:41 +03:00
Merge pull request #576 from Shopify/flexible-exception-handler
Allow the exception handler to convert exceptions to hide error messges
This commit is contained in:
@@ -13,7 +13,7 @@ module Liquid
|
||||
# context['bob'] #=> nil class Context
|
||||
class Context
|
||||
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)
|
||||
@environments = [environments].flatten
|
||||
@@ -22,12 +22,11 @@ module Liquid
|
||||
@errors = []
|
||||
@resource_limits = resource_limits || ResourceLimits.new(Template.default_resource_limits)
|
||||
squash_instance_assigns_with_environments
|
||||
@render_errors = true
|
||||
|
||||
@this_stack_used = false
|
||||
|
||||
if rethrow_errors
|
||||
self.exception_handler = ->(e) { true }
|
||||
self.exception_handler = ->(e) { raise }
|
||||
end
|
||||
|
||||
@interrupts = []
|
||||
@@ -68,9 +67,22 @@ module Liquid
|
||||
e.set_line_number_from_token(token)
|
||||
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)
|
||||
raise if exception_handler && exception_handler.call(e)
|
||||
render_errors ? Liquid::Error.render(e) : ''
|
||||
output || Liquid::Error.render(e)
|
||||
end
|
||||
|
||||
def invoke(method, *args)
|
||||
|
||||
@@ -17,7 +17,7 @@ module Liquid
|
||||
locale: I18n.new
|
||||
}
|
||||
|
||||
attr_accessor :root, :render_errors
|
||||
attr_accessor :root
|
||||
attr_reader :resource_limits
|
||||
|
||||
@@file_system = BlankFileSystem.new
|
||||
@@ -168,7 +168,7 @@ module Liquid
|
||||
c = args.shift
|
||||
|
||||
if @rethrow_errors
|
||||
c.exception_handler = ->(e) { true }
|
||||
c.exception_handler = ->(e) { raise }
|
||||
end
|
||||
|
||||
c
|
||||
@@ -183,8 +183,6 @@ module Liquid
|
||||
raise ArgumentError, "Expected Hash or Liquid::Context as parameter"
|
||||
end
|
||||
|
||||
context.render_errors = self.render_errors unless self.render_errors.nil?
|
||||
|
||||
case args.last
|
||||
when Hash
|
||||
options = args.pop
|
||||
|
||||
@@ -186,10 +186,21 @@ class ErrorHandlingTest < Minitest::Test
|
||||
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.render_errors = false
|
||||
assert_equal 'This is an argument error: ', template.render('errors' => ErrorDrop.new)
|
||||
output = template.render({ 'errors' => ErrorDrop.new }, exception_handler: ->(e) { '' })
|
||||
assert_equal 'This is an argument error: ', output
|
||||
assert_equal [ArgumentError], template.errors.map(&:class)
|
||||
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 }}', line_numbers: true)
|
||||
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 (line 1): internal', output
|
||||
assert_equal [InternalError], template.errors.map(&:class)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -102,6 +102,10 @@ class ErrorDrop < Liquid::Drop
|
||||
raise Liquid::SyntaxError, 'syntax error'
|
||||
end
|
||||
|
||||
def runtime_error
|
||||
raise RuntimeError, 'runtime error'
|
||||
end
|
||||
|
||||
def exception
|
||||
raise Exception, 'exception'
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user