Exception handling for humans

Ability to pass exception_handler as a block to #render
and provide whatever behavior you want on handling exceptions

https://github.com/Shopify/liquid/pull/254
This commit is contained in:
Florian Weingarten
2014-07-24 14:44:02 +00:00
parent 034a47a6cf
commit 6c6350f18b
4 changed files with 30 additions and 6 deletions
+7 -5
View File
@@ -14,8 +14,7 @@ module Liquid
# context['bob'] #=> nil class Context
class Context
attr_reader :scopes, :errors, :registers, :environments, :resource_limits
attr_accessor :rethrow_errors
attr_accessor :exception_handler
SQUARE_BRACKETED = /\A\[(.*)\]\z/m
@@ -24,10 +23,13 @@ module Liquid
@scopes = [(outer_scope || {})]
@registers = registers
@errors = []
@rethrow_errors = rethrow_errors
@resource_limits = (resource_limits || {}).merge!({ :render_score_current => 0, :assign_score_current => 0 })
squash_instance_assigns_with_environments
if rethrow_errors
self.exception_handler = ->(e) { true }
end
@interrupts = []
@filters = []
@parsed_variables = Hash.new{ |cache, markup| cache[markup] = variable_parse(markup) }
@@ -91,7 +93,8 @@ module Liquid
def handle_error(e)
errors.push(e)
raise if @rethrow_errors
raise if exception_handler && exception_handler.call(e)
case e
when SyntaxError
@@ -300,5 +303,4 @@ module Liquid
end
end # squash_instance_assigns_with_environments
end # Context
end # Liquid
+8 -1
View File
@@ -142,7 +142,11 @@ module Liquid
context = case args.first
when Liquid::Context
c = args.shift
c.rethrow_errors = true if @rethrow_errors
if @rethrow_errors
c.exception_handler = ->(e) { true }
end
c
when Liquid::Drop
drop = args.shift
@@ -167,6 +171,9 @@ module Liquid
context.add_filters(options[:filters])
end
if options[:exception_handler]
context.exception_handler = options[:exception_handler]
end
when Module
context.add_filters(args.pop)
when Array