Allow the exception handler to convert exceptions to hide error messages.

This commit is contained in:
Dylan Thacker-Smith
2015-05-27 18:59:51 -04:00
parent 89c6e605f8
commit 8d5e71f856
4 changed files with 37 additions and 12 deletions
+17 -5
View File
@@ -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)
+2 -4
View File
@@ -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