-
Notifications
You must be signed in to change notification settings - Fork 394
/
interpretererror.go
139 lines (125 loc) · 3.36 KB
/
interpretererror.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package earthfile2llb
import (
"fmt"
"regexp"
"strconv"
"github.com/earthly/earthly/ast/spec"
"github.com/earthly/earthly/util/stringutil"
"github.com/pkg/errors"
)
var _ error = &InterpreterError{}
// note this regex should be updated in case the error format changes in Errorf
var regex = regexp.MustCompile(`(?P<file_path>.*?):(?P<line>\d+):(?P<column>\d+) (?P<error>.+?)($|\nin\t\t(?P<stack>.+?)$)`)
// InterpreterError is an error of the interpreter, which contains optional references to the original
// source code location.
type InterpreterError struct {
SourceLocation *spec.SourceLocation
TargetID string
text string
cause error
stack string
}
// Errorf creates a new interpreter error.
func Errorf(sl *spec.SourceLocation, targetID, stack string, format string, args ...interface{}) *InterpreterError {
return &InterpreterError{
SourceLocation: sl,
TargetID: targetID,
stack: stack,
text: fmt.Sprintf(format, args...),
}
}
// WrapError wraps another error into a new interpreter error.
func WrapError(cause error, sl *spec.SourceLocation, targetID, stack string, format string, args ...interface{}) *InterpreterError {
return &InterpreterError{
cause: cause,
SourceLocation: sl,
TargetID: targetID,
stack: stack,
text: fmt.Sprintf(format, args...),
}
}
func (ie InterpreterError) Error() string {
var err error
if ie.cause != nil {
err = errors.Wrap(ie.cause, ie.text)
} else {
err = errors.New(ie.text)
}
if ie.SourceLocation == nil {
return err.Error()
}
ret := fmt.Sprintf(
"%s:%d:%d %s",
ie.SourceLocation.File, ie.SourceLocation.StartLine, ie.SourceLocation.StartColumn,
err.Error())
if ie.stack != "" {
ret = fmt.Sprintf("%s\nin\t\t%s", ret, ie.stack)
}
return ret
}
// Cause returns the cause of the error (if any).
func (ie InterpreterError) Cause() error {
return errors.Cause(ie.cause)
}
// Unwrap unwraps the error.
func (ie InterpreterError) Unwrap() error {
return ie.cause
}
// Stack returns the Earthly stack within the error.
func (ie InterpreterError) Stack() string {
return ie.stack
}
// GetInterpreterError finds the first InterpreterError in the wrap chain and returns it.
func GetInterpreterError(err error) (*InterpreterError, bool) {
if err == nil {
return nil, false
}
ie, ok := err.(*InterpreterError)
if ok {
return ie, true
}
unwrapped := errors.Unwrap(err)
if unwrapped != nil {
return GetInterpreterError(unwrapped)
}
return nil, false
}
// FromError attempts to parse the given error's string to an *InterpreterError
func FromError(err error) (*InterpreterError, bool) {
if err == nil {
return nil, false
}
matches, _ := stringutil.NamedGroupMatches(err.Error(), regex)
if len(matches) != 4 && len(matches) != 5 {
return nil, false
}
for k := range matches {
if k != "stack" && len(matches[k]) != 1 {
return nil, false
}
}
filePath := matches["file_path"][0]
line, err := strconv.Atoi(matches["line"][0])
if err != nil {
return nil, false
}
column, err := strconv.Atoi(matches["column"][0])
if err != nil {
return nil, false
}
errMsg := matches["error"][0]
stack := ""
if len(matches["stack"]) == 1 {
stack = matches["stack"][0]
}
return Errorf(
&spec.SourceLocation{
File: filePath,
StartLine: line,
StartColumn: column,
},
"",
stack,
errMsg,
), true
}