mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-29 07:05:12 -07:00
Merge pull request #397 from Shopify/bogdan-excetion-handling-for-humans
Excetion handling for humans (2)
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
## 3.0.0 / not yet released / branch "master"
|
## 3.0.0 / not yet released / branch "master"
|
||||||
|
|
||||||
* ...
|
* ...
|
||||||
|
* Add exception_handler feature, see #397 and #254 [Bogdan Gusiev, bogdan and Florian Weingarten, fw42]
|
||||||
* Optimize variable parsing to avoid repeated regex evaluation during template rendering #383 [Jason Hiltz-Laforge, jasonhl]
|
* Optimize variable parsing to avoid repeated regex evaluation during template rendering #383 [Jason Hiltz-Laforge, jasonhl]
|
||||||
* Optimize checking for block interrupts to reduce object allocation #380 [Jason Hiltz-Laforge, jasonhl]
|
* Optimize checking for block interrupts to reduce object allocation #380 [Jason Hiltz-Laforge, jasonhl]
|
||||||
* Properly set context rethrow_errors on render! #349 [Thierry Joyal, tjoyal]
|
* Properly set context rethrow_errors on render! #349 [Thierry Joyal, tjoyal]
|
||||||
|
|||||||
@@ -14,8 +14,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 :rethrow_errors
|
|
||||||
|
|
||||||
SQUARE_BRACKETED = /\A\[(.*)\]\z/m
|
SQUARE_BRACKETED = /\A\[(.*)\]\z/m
|
||||||
|
|
||||||
@@ -24,10 +23,13 @@ module Liquid
|
|||||||
@scopes = [(outer_scope || {})]
|
@scopes = [(outer_scope || {})]
|
||||||
@registers = registers
|
@registers = registers
|
||||||
@errors = []
|
@errors = []
|
||||||
@rethrow_errors = rethrow_errors
|
|
||||||
@resource_limits = (resource_limits || {}).merge!({ :render_score_current => 0, :assign_score_current => 0 })
|
@resource_limits = (resource_limits || {}).merge!({ :render_score_current => 0, :assign_score_current => 0 })
|
||||||
squash_instance_assigns_with_environments
|
squash_instance_assigns_with_environments
|
||||||
|
|
||||||
|
if rethrow_errors
|
||||||
|
self.exception_handler = ->(e) { true }
|
||||||
|
end
|
||||||
|
|
||||||
@interrupts = []
|
@interrupts = []
|
||||||
@filters = []
|
@filters = []
|
||||||
@parsed_variables = Hash.new{ |cache, markup| cache[markup] = variable_parse(markup) }
|
@parsed_variables = Hash.new{ |cache, markup| cache[markup] = variable_parse(markup) }
|
||||||
@@ -91,7 +93,8 @@ module Liquid
|
|||||||
|
|
||||||
def handle_error(e)
|
def handle_error(e)
|
||||||
errors.push(e)
|
errors.push(e)
|
||||||
raise if @rethrow_errors
|
|
||||||
|
raise if exception_handler && exception_handler.call(e)
|
||||||
|
|
||||||
case e
|
case e
|
||||||
when SyntaxError
|
when SyntaxError
|
||||||
@@ -300,5 +303,4 @@ module Liquid
|
|||||||
end
|
end
|
||||||
end # squash_instance_assigns_with_environments
|
end # squash_instance_assigns_with_environments
|
||||||
end # Context
|
end # Context
|
||||||
|
|
||||||
end # Liquid
|
end # Liquid
|
||||||
|
|||||||
@@ -142,7 +142,11 @@ module Liquid
|
|||||||
context = case args.first
|
context = case args.first
|
||||||
when Liquid::Context
|
when Liquid::Context
|
||||||
c = args.shift
|
c = args.shift
|
||||||
c.rethrow_errors = true if @rethrow_errors
|
|
||||||
|
if @rethrow_errors
|
||||||
|
c.exception_handler = ->(e) { true }
|
||||||
|
end
|
||||||
|
|
||||||
c
|
c
|
||||||
when Liquid::Drop
|
when Liquid::Drop
|
||||||
drop = args.shift
|
drop = args.shift
|
||||||
@@ -167,6 +171,9 @@ module Liquid
|
|||||||
context.add_filters(options[:filters])
|
context.add_filters(options[:filters])
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if options[:exception_handler]
|
||||||
|
context.exception_handler = options[:exception_handler]
|
||||||
|
end
|
||||||
when Module
|
when Module
|
||||||
context.add_filters(args.pop)
|
context.add_filters(args.pop)
|
||||||
when Array
|
when Array
|
||||||
|
|||||||
@@ -153,4 +153,18 @@ class TemplateTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
assert_equal 'ruby error in drop', e.message
|
assert_equal 'ruby error in drop', e.message
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_exception_handler_doesnt_reraise_if_it_returns_false
|
||||||
|
exception = nil
|
||||||
|
Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_handler: ->(e) { exception = e; false })
|
||||||
|
assert exception.is_a?(ZeroDivisionError)
|
||||||
|
end
|
||||||
|
|
||||||
|
def test_exception_handler_does_reraise_if_it_returns_true
|
||||||
|
exception = nil
|
||||||
|
assert_raises(ZeroDivisionError) do
|
||||||
|
Template.parse("{{ 1 | divided_by: 0 }}").render({}, exception_handler: ->(e) { exception = e; true })
|
||||||
|
end
|
||||||
|
assert exception.is_a?(ZeroDivisionError)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user