Skip to content

Commit 4cce6c4

Browse files
committed
cue: apply wrapping to all errors in list
for valueError and callError. This also improves path and position information. Change-Id: I27edc7fef2e38e4017a9ee62c6f82f967c54d80a Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7085 Reviewed-by: CUE cueckoo <[email protected]> Reviewed-by: Marcel van Lohuizen <[email protected]>
1 parent d2fdbf0 commit 4cce6c4

File tree

10 files changed

+48
-24
lines changed

10 files changed

+48
-24
lines changed

cmd/cue/cmd/testdata/script/cmd_baddisplay.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
cmp stderr cmd_baddisplay.out
44

55
-- cmd_baddisplay.out --
6-
command.baddisplay.display: conflicting values 42 and string (mismatched types int and string)
6+
command.baddisplay.display.text: conflicting values 42 and string (mismatched types int and string)
77
-- task.cue --
88
package home
99
message: "Hello world!"

cmd/cue/cmd/testdata/script/cmd_dep_cycle.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ cmp stderr expect-stderr3
99
-- expect-stderr1 --
1010
cyclic dependency in tasks
1111
-- expect-stderr2 --
12-
command.aftercycle: structural cycle
12+
command.aftercycle.t1.$after.$after: structural cycle
1313
-- expect-stderr3 --
1414
-- interlocked-stdout --
1515
v

cmd/cue/cmd/testdata/script/cmd_err.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
cmp stderr cmd_badfields.out
44

55
-- cmd_badfields.out --
6-
command.ref.task.display.filename: non-concrete value string
76
command.ref.task.display.contents: invalid bytes argument for field "contents": non-concrete value (string|bytes):
87
./task_tool.cue:6:8
8+
command.ref.task.display.filename: non-concrete value string:
9+
tool/file:15:16
910
-- task_tool.cue --
1011
package home
1112

cmd/cue/cmd/testdata/script/eval_e.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ cmp stdout expect-stdout
44

55
-- expect-stdout --
66
-- expect-stderr --
7-
reference "nonExist" not found
7+
reference "nonExist" not found:
8+
--expression:1:1
89
-- partial.cue --
910
package exitcode
1011

cmd/cue/cmd/testdata/script/eval_expr.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ cmp stdout expect-stdout
77
4
88
-- expect-stderr --
99
// b.idx
10-
invalid non-ground value string (must be concrete string)
10+
b.idx: invalid non-ground value string (must be concrete string)
1111
-- partial.cue --
1212
package partial
1313

cue/errors.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ import (
2020
"cuelang.org/go/internal/core/adt"
2121
)
2222

23-
func (v Value) appendErr(err errors.Error, b *bottom) errors.Error {
24-
return &valueError{
25-
v: v,
26-
err: &adt.Bottom{
27-
Err: errors.Append(err, b.Err),
28-
},
29-
}
30-
}
31-
3223
func (v Value) toErr(b *bottom) (err errors.Error) {
24+
errs := errors.Errors(b.Err)
25+
if len(errs) > 1 {
26+
for _, e := range errs {
27+
bb := *b
28+
bb.Err = e
29+
err = errors.Append(err, &valueError{v: v, err: &bb})
30+
}
31+
return err
32+
}
3333
return &valueError{v: v, err: b}
3434
}
3535

@@ -48,6 +48,9 @@ func (e *valueError) Error() string {
4848
}
4949

5050
func (e *valueError) Position() token.Pos {
51+
if e.err.Err != nil {
52+
return e.err.Err.Position()
53+
}
5154
src := e.err.Source()
5255
if src == nil {
5356
return token.NoPos
@@ -70,6 +73,12 @@ func (e *valueError) Msg() (string, []interface{}) {
7073
}
7174

7275
func (e *valueError) Path() (a []string) {
76+
if e.err.Err != nil {
77+
a = e.err.Err.Path()
78+
if a != nil {
79+
return a
80+
}
81+
}
7382
return e.v.appendPath(nil)
7483
}
7584

internal/filetypes/filetypes_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,15 @@ import (
1919
"testing"
2020

2121
"cuelang.org/go/cue/build"
22+
"cuelang.org/go/cue/errors"
2223
"github.com/google/go-cmp/cmp"
2324
"github.com/google/go-cmp/cmp/cmpopts"
2425
)
2526

2627
func check(t *testing.T, want, x interface{}, err error) {
2728
t.Helper()
2829
if err != nil {
29-
x = err.Error()
30+
x = errors.String(err.(errors.Error))
3031
}
3132
if !cmp.Equal(x, want, cmpopts.EquateEmpty()) {
3233
t.Error(cmp.Diff(want, x))

pkg/encoding/yaml/manual.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ func Marshal(v cue.Value) (string, error) {
3131
if err := v.Validate(); err != nil {
3232
return "", err
3333
}
34+
// TODO: allow adt.Bottom to implement errors.Error so that code and
35+
// messages can be passed.
3436
return "", internal.ErrIncomplete
3537
}
3638
n := v.Syntax(cue.Final(), cue.Concrete(true))
@@ -55,6 +57,8 @@ func MarshalStream(v cue.Value) (string, error) {
5557
if err := v.Validate(); err != nil {
5658
return "", err
5759
}
60+
// TODO: allow adt.Bottom to implement errors.Error so that code and
61+
// messages can be passed.
5862
return "", internal.ErrIncomplete
5963
}
6064
n := v.Syntax(cue.Final(), cue.Concrete(true))

pkg/internal/errors.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,14 @@ func (e *callError) Error() string {
3939

4040
func (c *CallCtxt) errf(src adt.Node, underlying error, format string, args ...interface{}) {
4141
var errs errors.Error
42+
var code adt.ErrorCode
4243
if err, ok := underlying.(bottomer); ok {
43-
errs = err.Bottom().Err
44+
b := err.Bottom()
45+
errs = b.Err
46+
code = b.Code
4447
}
4548
errs = errors.Wrapf(errs, c.ctx.Pos(), format, args...)
46-
c.Err = &callError{&adt.Bottom{Err: errs}}
49+
c.Err = &callError{&adt.Bottom{Code: code, Err: errs}}
4750
}
4851

4952
func (c *CallCtxt) errcf(src adt.Node, code adt.ErrorCode, format string, args ...interface{}) {
@@ -59,11 +62,13 @@ func wrapCallErr(c *CallCtxt, b *adt.Bottom) *adt.Bottom {
5962
pos = src.Pos()
6063
}
6164
}
62-
const msg = "error in call to %s"
63-
return &adt.Bottom{
64-
Code: b.Code,
65-
Err: errors.Wrapf(b.Err, pos, msg, c.builtin.name(c.ctx)),
65+
var err errors.Error
66+
for _, e := range errors.Errors(b.Err) {
67+
const msg = "error in call to %s"
68+
err = errors.Append(err,
69+
errors.Wrapf(e, pos, msg, c.builtin.name(c.ctx)))
6670
}
71+
return &adt.Bottom{Code: b.Code, Err: err}
6772
}
6873

6974
func (c *CallCtxt) convertError(x interface{}, name string) *adt.Bottom {

pkg/list/testdata/gen.txtar

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ error in call to list.Slice: negative index
7676
error in call to list.Slice: slice bounds out of range
7777
error in call to list.Take: negative index
7878
0: error in call to list.SortStrings: element 0 of list argument 0: cannot use value 1 (type int) as string
79-
x: error in call to list.Sort: x: conflicting values string and {b:2} (mismatched types string and struct) (and 1 more errors)
79+
x: error in call to list.Sort: conflicting values string and {b:2} (mismatched types string and struct)
80+
y: error in call to list.Sort: conflicting values string and {a:1} (mismatched types string and struct)
8081
t3: cannot use "foo" (type string) as list in argument 1 to list.Avg:
8182
./in.cue:5:14
8283
t14: cannot use "foo" (type string) as int in argument 2 to list.FlattenN:
@@ -265,7 +266,8 @@ Result:
265266
}
266267
}
267268
t40: (_|_){
268-
// [eval] x: error in call to list.Sort: x: conflicting values string and {b:2} (mismatched types string and struct) (and 1 more errors)
269+
// [eval] x: error in call to list.Sort: conflicting values string and {b:2} (mismatched types string and struct)
270+
// y: error in call to list.Sort: conflicting values string and {a:1} (mismatched types string and struct)
269271
}
270272
t41: (#list){
271273
0: (string){ "a" }
@@ -300,6 +302,7 @@ Result:
300302
t52: (bool){ true }
301303
t53: (bool){ false }
302304
t54: (_|_){
303-
// [eval] x: error in call to list.Sort: x: conflicting values string and {b:2} (mismatched types string and struct) (and 1 more errors)
305+
// [eval] x: error in call to list.Sort: conflicting values string and {b:2} (mismatched types string and struct)
306+
// y: error in call to list.Sort: conflicting values string and {a:1} (mismatched types string and struct)
304307
}
305308
}

0 commit comments

Comments
 (0)