mirror of
https://github.com/Shopify/liquid.git
synced 2026-09-12 23:40:45 -07:00
Optional line numbers for liquid errors
This commit is contained in:
+1
-1
@@ -66,11 +66,11 @@ require 'liquid/standardfilters'
|
||||
require 'liquid/condition'
|
||||
require 'liquid/module_ex'
|
||||
require 'liquid/utils'
|
||||
require 'liquid/token'
|
||||
|
||||
# Load all the tags of the standard library
|
||||
#
|
||||
Dir[File.dirname(__FILE__) + '/liquid/tags/*.rb'].each { |f| require f }
|
||||
|
||||
require 'liquid/profiler'
|
||||
require 'liquid/profiler/token'
|
||||
require 'liquid/profiler/hooks'
|
||||
|
||||
+1
-1
@@ -144,7 +144,7 @@ module Liquid
|
||||
rescue MemoryError => e
|
||||
raise e
|
||||
rescue ::StandardError => e
|
||||
output << (context.handle_error(e))
|
||||
output << (context.handle_error(e, token))
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -91,17 +91,12 @@ module Liquid
|
||||
@interrupts.pop
|
||||
end
|
||||
|
||||
def handle_error(e)
|
||||
|
||||
def handle_error(e, token)
|
||||
e = Liquid::Error.error_with_line_number(e, token)
|
||||
errors.push(e)
|
||||
|
||||
raise if exception_handler && exception_handler.call(e)
|
||||
|
||||
case e
|
||||
when SyntaxError
|
||||
"Liquid syntax error: #{e.message}"
|
||||
else
|
||||
"Liquid error: #{e.message}"
|
||||
end
|
||||
Liquid::Error.render(e)
|
||||
end
|
||||
|
||||
def invoke(method, *args)
|
||||
|
||||
+31
-1
@@ -1,5 +1,35 @@
|
||||
module Liquid
|
||||
class Error < ::StandardError; end
|
||||
class Error < ::StandardError
|
||||
attr_accessor :line_number
|
||||
|
||||
def self.render(e)
|
||||
msg = if e.is_a?(Liquid::Error) && e.line_number
|
||||
"#{e.line_number}: #{e.message}"
|
||||
else
|
||||
e.message
|
||||
end
|
||||
|
||||
case e
|
||||
when SyntaxError
|
||||
"Liquid syntax error: #{msg}"
|
||||
else
|
||||
"Liquid error: #{msg}"
|
||||
end
|
||||
end
|
||||
|
||||
def self.error_with_line_number(e, token)
|
||||
if e.is_a?(Liquid::Error)
|
||||
e.set_line_number_from_token(token)
|
||||
end
|
||||
|
||||
e
|
||||
end
|
||||
|
||||
def set_line_number_from_token(token)
|
||||
return unless token.respond_to?(:line_number)
|
||||
self.line_number = token.line_number
|
||||
end
|
||||
end
|
||||
|
||||
class ArgumentError < Error; end
|
||||
class ContextError < Error; end
|
||||
|
||||
@@ -105,6 +105,8 @@ module Liquid
|
||||
def parse(source, options = {})
|
||||
@options = options
|
||||
@profiling = options[:profile]
|
||||
@profiling = options.delete(:profile)
|
||||
@line_numbers = options.delete(:line_numbers) || @profiling
|
||||
@root = Document.parse(tokenize(source), DEFAULT_OPTIONS.merge(options))
|
||||
@warnings = nil
|
||||
self
|
||||
@@ -197,7 +199,7 @@ module Liquid
|
||||
end
|
||||
result.respond_to?(:join) ? result.join : result
|
||||
rescue Liquid::MemoryError => e
|
||||
context.handle_error(e)
|
||||
context.handle_error(e, nil)
|
||||
ensure
|
||||
@errors = context.errors
|
||||
end
|
||||
@@ -224,7 +226,7 @@ module Liquid
|
||||
end
|
||||
|
||||
def calculate_line_numbers(raw_tokens)
|
||||
return raw_tokens unless @profiling
|
||||
return raw_tokens unless @line_numbers
|
||||
|
||||
current_line = 1
|
||||
raw_tokens.map do |token|
|
||||
@@ -248,6 +250,5 @@ module Liquid
|
||||
yield
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
module Liquid
|
||||
class Token < String
|
||||
|
||||
attr_reader :line_number
|
||||
|
||||
def initialize(content, line_number)
|
||||
@@ -11,6 +10,5 @@ module Liquid
|
||||
def raw
|
||||
"<raw>"
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user