-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
252 lines (220 loc) · 5.49 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
package bynom
import (
"context"
"fmt"
"strconv"
"strings"
)
// ErrExpectationFailed describes what have been expected and what encountered.
type ErrExpectationFailed struct {
Expected interface{} // Which range has been expected.
Have byte // Which byte encountered.
Not bool // Not negates the meaning of Expected.
}
func (e ErrExpectationFailed) Error() string {
var expected string
switch v := e.Expected.(type) {
case fmt.Stringer:
expected = v.String()
case byte:
expected = "'" + string(v) + "'"
default:
expected = fmt.Sprint(v)
}
if e.Not {
return fmt.Sprintf("expectation failed: expected not %s", expected)
}
return fmt.Sprintf("expectation failed: expected %v, have '%s'", expected, string(e.Have))
}
// ErrStateTestFailed notifies that state test against value Assert failed.
type ErrStateTestFailed struct {
Assert int64
}
func (e ErrStateTestFailed) Error() string {
return fmt.Sprintf("state test failed: %b", e.Assert)
}
// ErrRequirementNotMet describes the situation when some required condition not met.
type ErrRequirementNotMet struct {
Expected interface{} // Expected value.
Have interface{} // Value which encountered.
Msg string // Message describing what is wrong.
}
func (e ErrRequirementNotMet) Error() string {
return fmt.Sprintf("requirement not met: %s: expected %v, have %v", e.Msg, e.Expected, e.Have)
}
// ErrParseFailed contains the original error happened and the parse context.
type ErrParseFailed struct {
Err error
StartPos int
EndPos int
Context *ParseContext
Stack []Breadcrumb
}
func (e *ErrParseFailed) Error() string {
var sb strings.Builder
sb.WriteString(e.Err.Error())
sb.WriteString(fmt.Sprintf(", start position: %d, end position: %d", e.StartPos, e.EndPos))
if e.Context != nil {
sb.WriteString(", context: ")
sb.WriteString(e.Context.String())
}
if len(e.Stack) > 0 {
sb.WriteString(", stack:")
for i, b := range e.Stack {
if i == 0 {
sb.WriteByte(' ')
} else {
sb.WriteString(", ")
}
sb.WriteString(b.String())
}
}
return sb.String()
}
func (e *ErrParseFailed) Unwrap() error {
return e.Err
}
func (e *ErrParseFailed) CopyContext(ctx context.Context, p Plate, startPos, endPos, chunkLen int) {
e.Context = new(ParseContext)
var (
ctxLen = endPos - startPos
buf []byte
)
if ctxLen <= chunkLen {
if buf, e.Context.HeadErr = p.ByteSlice(ctx, startPos, endPos); e.Context.HeadErr == nil {
e.Context.Head = make([]byte, len(buf))
copy(e.Context.Head, buf)
}
} else {
e.Context.Parted = true
e.Context.BytesRemain = ctxLen - chunkLen
var leftLen = chunkLen / 2
if buf, e.Context.HeadErr = p.ByteSlice(ctx, startPos, startPos+leftLen); e.Context.HeadErr == nil {
e.Context.Head = make([]byte, len(buf))
copy(e.Context.Head, buf)
}
var rightLen = chunkLen - leftLen
if buf, e.Context.TailErr = p.ByteSlice(ctx, endPos-rightLen, endPos); e.Context.TailErr == nil {
e.Context.Tail = make([]byte, len(buf))
copy(e.Context.Tail, buf)
}
}
}
func (e *ErrParseFailed) UnwrapBreadcrumbs() {
for {
if e.Err == nil {
break
}
if v, ok := e.Err.(*ErrBreadcrumb); ok {
e.Stack = append(e.Stack, v.Breadcrumb)
e.Err = v.Err
} else {
break
}
}
}
type ErrBreadcrumb struct {
Err error
Breadcrumb
}
func (e *ErrBreadcrumb) Error() string {
return e.Breadcrumb.String() + ": " + e.Err.Error()
}
func WrapBreadcrumb(err error, name string, index int) *ErrBreadcrumb {
return &ErrBreadcrumb{
Err: err,
Breadcrumb: Breadcrumb{
Name: name,
Index: index,
StartPos: -1,
EndPos: -1,
},
}
}
func ExtendBreadcrumb(err error, startPos, endPos int) error {
if v, ok := err.(*ErrBreadcrumb); ok {
if startPos >= 0 {
v.StartPos = startPos
}
if endPos >= 0 {
v.EndPos = endPos
}
}
return err
}
type ParseContext struct {
Head []byte
HeadErr error
Tail []byte
TailErr error
BytesRemain int
Parted bool
}
func (pc ParseContext) String() string {
var sb strings.Builder
if pc.Parted {
if pc.HeadErr == nil && pc.TailErr == nil {
sb.WriteByte('\'')
_, _ = sb.Write(pc.Head)
if pc.BytesRemain > 0 {
sb.WriteString("..[")
sb.WriteString(strconv.Itoa(pc.BytesRemain))
sb.WriteString(" bytes]..")
}
_, _ = sb.Write(pc.Tail)
sb.WriteByte('\'')
} else if pc.HeadErr == nil {
sb.WriteByte('\'')
_, _ = sb.Write(pc.Head)
sb.WriteString("..[more bytes]'")
} else if pc.TailErr == nil {
sb.WriteString("'[more bytes]..")
_, _ = sb.Write(pc.Tail)
sb.WriteByte('\'')
} else {
sb.WriteByte('!')
sb.WriteString(pc.HeadErr.Error())
}
} else {
if pc.HeadErr == nil {
sb.WriteByte('\'')
_, _ = sb.Write(pc.Head)
sb.WriteByte('\'')
} else {
sb.WriteByte('!')
sb.WriteString(pc.HeadErr.Error())
}
}
return sb.String()
}
type Breadcrumb struct {
Name string
Index int
StartPos int
EndPos int
}
func (b Breadcrumb) String() string {
var sb strings.Builder
if b.Index >= 0 {
sb.WriteString("[")
sb.WriteString(strconv.Itoa(b.Index))
sb.WriteString("]")
}
sb.WriteString(b.Name)
if b.StartPos >= 0 && b.EndPos >= 0 {
sb.WriteByte('{')
sb.WriteString(strconv.Itoa(b.StartPos))
sb.WriteByte(':')
sb.WriteString(strconv.Itoa(b.EndPos))
sb.WriteByte('}')
} else if b.StartPos >= 0 {
sb.WriteByte('{')
sb.WriteString(strconv.Itoa(b.StartPos))
sb.WriteString(":}")
} else if b.EndPos >= 0 {
sb.WriteString("{:")
sb.WriteString(strconv.Itoa(b.EndPos))
sb.WriteByte('}')
}
return sb.String()
}