2017-07-12 12:55:44 +02:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
2017-07-19 00:39:38 +02:00
|
|
|
// An Error is a syntax error during template parsing.
|
2017-07-12 12:55:44 +02:00
|
|
|
type Error interface {
|
|
|
|
error
|
|
|
|
Cause() error
|
2017-07-12 13:12:14 +02:00
|
|
|
Path() string
|
2017-07-12 12:55:44 +02:00
|
|
|
LineNumber() int
|
|
|
|
}
|
|
|
|
|
|
|
|
// A Locatable provides source location information for error reporting.
|
|
|
|
type Locatable interface {
|
|
|
|
SourceLocation() SourceLoc
|
|
|
|
SourceText() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Errorf creates a parser.Error.
|
|
|
|
func Errorf(loc Locatable, format string, a ...interface{}) *sourceLocError { // nolint: golint
|
|
|
|
return &sourceLocError{loc.SourceLocation(), loc.SourceText(), fmt.Sprintf(format, a...), nil}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapError wraps its argument in a parser.Error if this argument is not already a parser.Error and is not locatable.
|
|
|
|
func WrapError(err error, loc Locatable) Error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if e, ok := err.(Error); ok {
|
2017-07-14 16:17:34 +02:00
|
|
|
// re-wrap the error, if the inner layer implemented the locatable interface
|
|
|
|
// but didn't actually provide any information
|
|
|
|
if e.Path() != "" || loc.SourceLocation().IsZero() {
|
|
|
|
return e
|
|
|
|
}
|
|
|
|
if e.Cause() != nil {
|
|
|
|
err = e.Cause()
|
|
|
|
}
|
2017-07-12 12:55:44 +02:00
|
|
|
}
|
|
|
|
re := Errorf(loc, "%s", err)
|
|
|
|
re.cause = err
|
|
|
|
return re
|
|
|
|
}
|
|
|
|
|
|
|
|
type sourceLocError struct {
|
|
|
|
SourceLoc
|
|
|
|
context string
|
|
|
|
message string
|
|
|
|
cause error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *sourceLocError) Cause() error {
|
|
|
|
return e.cause
|
|
|
|
}
|
|
|
|
|
2017-07-12 13:12:14 +02:00
|
|
|
func (e *sourceLocError) Path() string {
|
2017-07-12 12:55:44 +02:00
|
|
|
return e.Pathname
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *sourceLocError) LineNumber() int {
|
|
|
|
return e.LineNo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e *sourceLocError) Error() string {
|
2017-07-16 19:49:37 +02:00
|
|
|
line := ""
|
|
|
|
if e.LineNo > 0 {
|
|
|
|
line = fmt.Sprintf(" (line %d)", e.LineNo)
|
|
|
|
}
|
2017-07-12 12:55:44 +02:00
|
|
|
locative := " in " + e.context
|
|
|
|
if e.Pathname != "" {
|
|
|
|
locative = " in " + e.Pathname
|
|
|
|
}
|
2017-07-16 19:49:37 +02:00
|
|
|
return fmt.Sprintf("Liquid error%s: %s%s", line, e.message, locative)
|
2017-07-12 12:55:44 +02:00
|
|
|
}
|