-
Notifications
You must be signed in to change notification settings - Fork 285
/
errors.go
324 lines (285 loc) · 7.29 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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Copyright 2020 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package adt
// This file contains error encodings.
//
//
// *Bottom:
// - an adt.Value
// - always belongs to a single vertex.
// - does NOT implement error
// - marks error code used for control flow
//
// errors.Error
// - CUE default error
// - implements error
// - tracks error locations
// - has error message details
// - supports multiple errors
//
import (
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
cueformat "cuelang.org/go/cue/format"
"cuelang.org/go/cue/token"
)
// ErrorCode indicates the type of error. The type of error may influence
// control flow. No other aspects of an error may influence control flow.
type ErrorCode int
const (
// An EvalError is a fatal evaluation error.
EvalError ErrorCode = iota
// A UserError is a fatal error originating from the user.
UserError
// StructuralCycleError means a structural cycle was found. Structural
// cycles are permanent errors, but they are not passed up recursively,
// as a unification of a value with a structural cycle with one that
// doesn't may still give a useful result.
StructuralCycleError
// IncompleteError means an evaluation could not complete because of
// insufficient information that may still be added later.
IncompleteError
// A CycleError indicates a reference error. It is considered to be
// an incomplete error, as reference errors may be broken by providing
// a concrete value.
CycleError
)
func (c ErrorCode) String() string {
switch c {
case EvalError:
return "eval"
case UserError:
return "user"
case StructuralCycleError:
return "structural cycle"
case IncompleteError:
return "incomplete"
case CycleError:
return "cycle"
}
return "unknown"
}
// Bottom represents an error or bottom symbol.
//
// Although a Bottom node holds control data, it should not be created until the
// control information already resulted in an error.
type Bottom struct {
Src ast.Node
Err errors.Error
Code ErrorCode
HasRecursive bool
ChildError bool // Err is the error of the child
NotExists bool // This error originated from a failed lookup.
// Value holds the computed value so far in case
Value Value
}
func (x *Bottom) Source() ast.Node { return x.Src }
func (x *Bottom) Kind() Kind { return BottomKind }
func (x *Bottom) Specialize(k Kind) Value { return x } // XXX remove
func (b *Bottom) IsIncomplete() bool {
if b == nil {
return false
}
return b.Code == IncompleteError || b.Code == CycleError
}
// isLiteralBottom reports whether x is an error originating from a user.
func isLiteralBottom(x Expr) bool {
b, ok := x.(*Bottom)
return ok && b.Code == UserError
}
// isError reports whether v is an error or nil.
func isError(v Value) bool {
if v == nil {
return true
}
_, ok := v.(*Bottom)
return ok
}
// isIncomplete reports whether v is associated with an incomplete error.
func isIncomplete(v *Vertex) bool {
if v == nil {
return true
}
if b, ok := v.BaseValue.(*Bottom); ok {
return b.IsIncomplete()
}
return false
}
// AddChildError updates x to record an error that occurred in one of
// its descendent arcs. The resulting error will record the worst error code of
// the current error or recursive error.
//
// If x is not already an error, the value is recorded in the error for
// reference.
//
func (v *Vertex) AddChildError(recursive *Bottom) {
v.ChildErrors = CombineErrors(nil, v.ChildErrors, recursive)
if recursive.IsIncomplete() {
return
}
x := v.BaseValue
err, _ := x.(*Bottom)
if err == nil {
v.BaseValue = &Bottom{
Code: recursive.Code,
Value: v,
HasRecursive: true,
ChildError: true,
Err: recursive.Err,
}
return
}
err.HasRecursive = true
if err.Code > recursive.Code {
err.Code = recursive.Code
}
v.BaseValue = err
}
// CombineErrors combines two errors that originate at the same Vertex.
func CombineErrors(src ast.Node, x, y Value) *Bottom {
a, _ := Unwrap(x).(*Bottom)
b, _ := Unwrap(y).(*Bottom)
if a == b && isCyclePlaceholder(a) {
return a
}
switch {
case a != nil && b != nil:
case a != nil:
return a
case b != nil:
return b
default:
return nil
}
if a.Code != b.Code {
if a.Code > b.Code {
a, b = b, a
}
if b.Code >= IncompleteError {
return a
}
}
return &Bottom{
Src: src,
Err: errors.Append(a.Err, b.Err),
Code: a.Code,
}
}
// A ValueError is returned as a result of evaluating a value.
type ValueError struct {
r Runtime
v *Vertex
pos token.Pos
auxpos []token.Pos
errors.Message
}
func (v *ValueError) AddPosition(n Node) {
if n == nil {
return
}
if p := pos(n); p != token.NoPos {
for _, q := range v.auxpos {
if p == q {
return
}
}
v.auxpos = append(v.auxpos, p)
}
}
func (v *ValueError) AddClosedPositions(c CloseInfo) {
for s := c.closeInfo; s != nil; s = s.parent {
if loc := s.location; loc != nil {
v.AddPosition(loc)
}
}
}
func (c *OpContext) errNode() *Vertex {
return c.vertex
}
// MarkPositions marks the current position stack.
func (c *OpContext) MarkPositions() int {
return len(c.positions)
}
// ReleasePositions sets the position state to one from a call to MarkPositions.
func (c *OpContext) ReleasePositions(p int) {
c.positions = c.positions[:p]
}
func (c *OpContext) AddPosition(n Node) {
if n != nil {
c.positions = append(c.positions, n)
}
}
func (c *OpContext) Newf(format string, args ...interface{}) *ValueError {
return c.NewPosf(c.pos(), format, args...)
}
func appendNodePositions(a []token.Pos, n Node) []token.Pos {
if p := pos(n); p != token.NoPos {
a = append(a, p)
}
if v, ok := n.(*Vertex); ok {
for _, c := range v.Conjuncts {
a = appendNodePositions(a, c.Elem())
}
}
return a
}
func (c *OpContext) NewPosf(p token.Pos, format string, args ...interface{}) *ValueError {
var a []token.Pos
if len(c.positions) > 0 {
a = make([]token.Pos, 0, len(c.positions))
for _, n := range c.positions {
a = appendNodePositions(a, n)
}
}
for i, arg := range args {
switch x := arg.(type) {
case Node:
a = appendNodePositions(a, x)
args[i] = c.Str(x)
case ast.Node:
b, _ := cueformat.Node(x)
if p := x.Pos(); p != token.NoPos {
a = append(a, p)
}
args[i] = string(b)
case Feature:
args[i] = x.SelectorString(c.Runtime)
}
}
return &ValueError{
r: c.Runtime,
v: c.errNode(),
pos: p,
auxpos: a,
Message: errors.NewMessage(format, args),
}
}
func (e *ValueError) Error() string {
return errors.String(e)
}
func (e *ValueError) Position() token.Pos {
return e.pos
}
func (e *ValueError) InputPositions() (a []token.Pos) {
return e.auxpos
}
func (e *ValueError) Path() (a []string) {
if e.v == nil {
return nil
}
for _, f := range appendPath(nil, e.v) {
a = append(a, f.SelectorString(e.r))
}
return a
}