prefix for Liquid::Error instances

This commit is contained in:
Florian Weingarten
2014-09-05 14:12:30 +00:00
parent aabbd8f1a1
commit c83e1c7b6d
4 changed files with 44 additions and 19 deletions
+33 -11
View File
@@ -1,22 +1,18 @@
module Liquid
class Error < ::StandardError
attr_accessor :line_number
attr_accessor :markup_context
def self.render(e)
def to_s(with_prefix=true)
str = ""
str << message_prefix if with_prefix
str << super()
if e.is_a?(SyntaxError)
str << "Liquid syntax error"
else
str << "Liquid error"
if markup_context
str << " "
str << markup_context
end
if e.respond_to?(:line_number) && e.line_number
str << " (line #{e.line_number})"
end
str << ": "
str << e.message
str
end
@@ -24,6 +20,32 @@ module Liquid
return unless token.respond_to?(:line_number)
self.line_number = token.line_number
end
def self.render(e)
if e.is_a?(Liquid::Error)
e.to_s
else
"Liquid error: #{e.to_s}"
end
end
private
def message_prefix
str = ""
if is_a?(SyntaxError)
str << "Liquid syntax error"
else
str << "Liquid error"
end
if line_number
str << " (line #{line_number})"
end
str << ": "
str
end
end
class ArgumentError < Error; end
+2 -2
View File
@@ -20,12 +20,12 @@ module Liquid
def strict_parse_with_error_context(markup)
strict_parse(markup)
rescue SyntaxError => e
e.message << markup_context(markup)
e.markup_context = markup_context(markup)
raise e
end
def markup_context(markup)
" in \"#{markup.strip}\""
"in \"#{markup.strip}\""
end
end
end
+1 -1
View File
@@ -30,7 +30,7 @@ module Liquid
end
def markup_context(markup)
" in \"{{#{markup}}}\""
"in \"{{#{markup}}}\""
end
def lax_parse(markup)