forked from osteele/liquid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
73 lines (63 loc) · 1.61 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package parser
import "fmt"
// An Error is a syntax error during template parsing.
type Error interface {
error
Cause() error
Path() string
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 {
// 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()
}
}
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
}
func (e *sourceLocError) Path() string {
return e.Pathname
}
func (e *sourceLocError) LineNumber() int {
return e.LineNo
}
func (e *sourceLocError) Error() string {
line := ""
if e.LineNo > 0 {
line = fmt.Sprintf(" (line %d)", e.LineNo)
}
locative := " in " + e.context
if e.Pathname != "" {
locative = " in " + e.Pathname
}
return fmt.Sprintf("Liquid error%s: %s%s", line, e.message, locative)
}