Skip to content

Commit

Permalink
cue: apply wrapping to all errors in list
Browse files Browse the repository at this point in the history
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 <cueckoo@gmail.com>
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
  • Loading branch information
mpvl committed Sep 15, 2020
1 parent d2fdbf0 commit 4cce6c4
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 24 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_baddisplay.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
cmp stderr cmd_baddisplay.out

-- cmd_baddisplay.out --
command.baddisplay.display: conflicting values 42 and string (mismatched types int and string)
command.baddisplay.display.text: conflicting values 42 and string (mismatched types int and string)
-- task.cue --
package home
message: "Hello world!"
Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/cmd_dep_cycle.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ cmp stderr expect-stderr3
-- expect-stderr1 --
cyclic dependency in tasks
-- expect-stderr2 --
command.aftercycle: structural cycle
command.aftercycle.t1.$after.$after: structural cycle
-- expect-stderr3 --
-- interlocked-stdout --
v
Expand Down
3 changes: 2 additions & 1 deletion cmd/cue/cmd/testdata/script/cmd_err.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
cmp stderr cmd_badfields.out

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

Expand Down
3 changes: 2 additions & 1 deletion cmd/cue/cmd/testdata/script/eval_e.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ cmp stdout expect-stdout

-- expect-stdout --
-- expect-stderr --
reference "nonExist" not found
reference "nonExist" not found:
--expression:1:1
-- partial.cue --
package exitcode

Expand Down
2 changes: 1 addition & 1 deletion cmd/cue/cmd/testdata/script/eval_expr.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cmp stdout expect-stdout
4
-- expect-stderr --
// b.idx
invalid non-ground value string (must be concrete string)
b.idx: invalid non-ground value string (must be concrete string)
-- partial.cue --
package partial

Expand Down
27 changes: 18 additions & 9 deletions cue/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import (
"cuelang.org/go/internal/core/adt"
)

func (v Value) appendErr(err errors.Error, b *bottom) errors.Error {
return &valueError{
v: v,
err: &adt.Bottom{
Err: errors.Append(err, b.Err),
},
}
}

func (v Value) toErr(b *bottom) (err errors.Error) {
errs := errors.Errors(b.Err)
if len(errs) > 1 {
for _, e := range errs {
bb := *b
bb.Err = e
err = errors.Append(err, &valueError{v: v, err: &bb})
}
return err
}
return &valueError{v: v, err: b}
}

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

func (e *valueError) Position() token.Pos {
if e.err.Err != nil {
return e.err.Err.Position()
}
src := e.err.Source()
if src == nil {
return token.NoPos
Expand All @@ -70,6 +73,12 @@ func (e *valueError) Msg() (string, []interface{}) {
}

func (e *valueError) Path() (a []string) {
if e.err.Err != nil {
a = e.err.Err.Path()
if a != nil {
return a
}
}
return e.v.appendPath(nil)
}

Expand Down
3 changes: 2 additions & 1 deletion internal/filetypes/filetypes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import (
"testing"

"cuelang.org/go/cue/build"
"cuelang.org/go/cue/errors"
"github.com/google/go-cmp/cmp"
"github.com/google/go-cmp/cmp/cmpopts"
)

func check(t *testing.T, want, x interface{}, err error) {
t.Helper()
if err != nil {
x = err.Error()
x = errors.String(err.(errors.Error))
}
if !cmp.Equal(x, want, cmpopts.EquateEmpty()) {
t.Error(cmp.Diff(want, x))
Expand Down
4 changes: 4 additions & 0 deletions pkg/encoding/yaml/manual.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func Marshal(v cue.Value) (string, error) {
if err := v.Validate(); err != nil {
return "", err
}
// TODO: allow adt.Bottom to implement errors.Error so that code and
// messages can be passed.
return "", internal.ErrIncomplete
}
n := v.Syntax(cue.Final(), cue.Concrete(true))
Expand All @@ -55,6 +57,8 @@ func MarshalStream(v cue.Value) (string, error) {
if err := v.Validate(); err != nil {
return "", err
}
// TODO: allow adt.Bottom to implement errors.Error so that code and
// messages can be passed.
return "", internal.ErrIncomplete
}
n := v.Syntax(cue.Final(), cue.Concrete(true))
Expand Down
17 changes: 11 additions & 6 deletions pkg/internal/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,14 @@ func (e *callError) Error() string {

func (c *CallCtxt) errf(src adt.Node, underlying error, format string, args ...interface{}) {
var errs errors.Error
var code adt.ErrorCode
if err, ok := underlying.(bottomer); ok {
errs = err.Bottom().Err
b := err.Bottom()
errs = b.Err
code = b.Code
}
errs = errors.Wrapf(errs, c.ctx.Pos(), format, args...)
c.Err = &callError{&adt.Bottom{Err: errs}}
c.Err = &callError{&adt.Bottom{Code: code, Err: errs}}
}

func (c *CallCtxt) errcf(src adt.Node, code adt.ErrorCode, format string, args ...interface{}) {
Expand All @@ -59,11 +62,13 @@ func wrapCallErr(c *CallCtxt, b *adt.Bottom) *adt.Bottom {
pos = src.Pos()
}
}
const msg = "error in call to %s"
return &adt.Bottom{
Code: b.Code,
Err: errors.Wrapf(b.Err, pos, msg, c.builtin.name(c.ctx)),
var err errors.Error
for _, e := range errors.Errors(b.Err) {
const msg = "error in call to %s"
err = errors.Append(err,
errors.Wrapf(e, pos, msg, c.builtin.name(c.ctx)))
}
return &adt.Bottom{Code: b.Code, Err: err}
}

func (c *CallCtxt) convertError(x interface{}, name string) *adt.Bottom {
Expand Down
9 changes: 6 additions & 3 deletions pkg/list/testdata/gen.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ error in call to list.Slice: negative index
error in call to list.Slice: slice bounds out of range
error in call to list.Take: negative index
0: error in call to list.SortStrings: element 0 of list argument 0: cannot use value 1 (type int) as string
x: error in call to list.Sort: x: conflicting values string and {b:2} (mismatched types string and struct) (and 1 more errors)
x: error in call to list.Sort: conflicting values string and {b:2} (mismatched types string and struct)
y: error in call to list.Sort: conflicting values string and {a:1} (mismatched types string and struct)
t3: cannot use "foo" (type string) as list in argument 1 to list.Avg:
./in.cue:5:14
t14: cannot use "foo" (type string) as int in argument 2 to list.FlattenN:
Expand Down Expand Up @@ -265,7 +266,8 @@ Result:
}
}
t40: (_|_){
// [eval] x: error in call to list.Sort: x: conflicting values string and {b:2} (mismatched types string and struct) (and 1 more errors)
// [eval] x: error in call to list.Sort: conflicting values string and {b:2} (mismatched types string and struct)
// y: error in call to list.Sort: conflicting values string and {a:1} (mismatched types string and struct)
}
t41: (#list){
0: (string){ "a" }
Expand Down Expand Up @@ -300,6 +302,7 @@ Result:
t52: (bool){ true }
t53: (bool){ false }
t54: (_|_){
// [eval] x: error in call to list.Sort: x: conflicting values string and {b:2} (mismatched types string and struct) (and 1 more errors)
// [eval] x: error in call to list.Sort: conflicting values string and {b:2} (mismatched types string and struct)
// y: error in call to list.Sort: conflicting values string and {a:1} (mismatched types string and struct)
}
}

0 comments on commit 4cce6c4

Please sign in to comment.