-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
128 lines (105 loc) · 2.7 KB
/
errors.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
package errors
import (
"errors"
"fmt"
"github.com/alecthomas/participle/v2/lexer"
)
var (
// error used to implement break
breakError = errors.New("break")
// error used to implement continue
continueError = errors.New("continue")
)
type posError struct {
msg string
}
func (e posError) Error() string {
return e.msg
}
// Errorf returns an error containing the lexer.Position and the formatted message.
// IsError with this error will return true.
func Errorf(pos lexer.Position, f string, a ...interface{}) error {
return &posError{msg: fmt.Sprintf(pos.String()+" "+f, a...)}
}
// Error wraps an error with the lexer.Position.
// If IsError(err) returns true then that error is returned.
// If err is nil then nil is returned.
// IsError will return true for a non-nil result from this function.
func Error(pos lexer.Position, err error) error {
// If err is a PosError then return it as it has the position already.
// Also, if err is nil then return nil, so we can use it as a catch-all
// Break and return dummy errors also are unchanged
if err == nil || IsError(err) || IsBreak(err) || IsContinue(err) || IsReturn(err) || IsNoFieldErr(err) {
return err
}
return Errorf(pos, err.Error())
}
// IsError returns true if the error is from Errorf or Error functions.
func IsError(err error) bool {
if err == nil {
return false
}
_, ok := err.(*posError)
return ok
}
func NoField(pos lexer.Position, v interface{}, n string) error {
return &NoFieldError{
msg: fmt.Sprintf("%s %T has no field %q", pos.String(), v, n),
v: v,
n: n,
}
}
type NoFieldError struct {
msg string
v interface{}
n string
}
func (e *NoFieldError) Error() string {
return e.msg
}
func (e *NoFieldError) Value() interface{} {
return e.v
}
func (e *NoFieldError) Name() string {
return e.n
}
func IsNoFieldErr(err error) bool {
if err == nil {
return false
}
_, ok := err.(*NoFieldError)
return ok
}
func GetNoFieldErr(err error) (*NoFieldError, bool) {
if err == nil {
return nil, false
}
e, ok := err.(*NoFieldError)
return e, ok
}
func Break() error {
return breakError
}
// IsBreak returns true if err is from a break instruction being invoked.
func IsBreak(err error) bool { return err == breakError }
func Continue() error {
return continueError
}
// IsContinue returns true if err is from a continue instruction being invoked.
func IsContinue(err error) bool { return err == continueError }
func IsReturn(err error) bool {
_, ok := err.(*ReturnError)
return ok
}
type ReturnError struct {
value interface{}
}
func (r *ReturnError) Error() string {
return "return"
}
func (r *ReturnError) Value() interface{} {
return r.value
}
func NewReturn(v interface{}) error {
return &ReturnError{value: v}
}