Make liquid error rendering optional.

Although the author of the liquid template wants to see these errors, they
probably don't want the visitor to see the liquid errors.  Probably the
best fallback when rendering the page for visitors is to render the empty
string for tags with errors.
This commit is contained in:
Dylan Thacker-Smith
2015-05-25 11:24:53 -04:00
parent 070639daba
commit 36200ff704
3 changed files with 13 additions and 3 deletions
+3 -2
View File
@@ -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 attr_accessor :exception_handler, :render_errors
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,6 +22,7 @@ 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
@@ -69,7 +70,7 @@ module Liquid
errors.push(e) errors.push(e)
raise if exception_handler && exception_handler.call(e) raise if exception_handler && exception_handler.call(e)
Liquid::Error.render(e) render_errors ? Liquid::Error.render(e) : ''
end end
def invoke(method, *args) def invoke(method, *args)
+3 -1
View File
@@ -17,7 +17,7 @@ module Liquid
locale: I18n.new locale: I18n.new
} }
attr_accessor :root attr_accessor :root, :render_errors
attr_reader :resource_limits attr_reader :resource_limits
@@file_system = BlankFileSystem.new @@file_system = BlankFileSystem.new
@@ -183,6 +183,8 @@ 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
+7
View File
@@ -185,4 +185,11 @@ class ErrorHandlingTest < Minitest::Test
template.render('errors' => ErrorDrop.new) template.render('errors' => ErrorDrop.new)
end end
end end
def test_disabling_error_rendering
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)
assert_equal [ArgumentError], template.errors.map(&:class)
end
end end