Skip to content

Commit

Permalink
internal/core/compile: better error message for user-defined errors
Browse files Browse the repository at this point in the history
Fixes #321

Change-Id: I0a04fd56a132a98ae3f143e91c4c185052cee4d7
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7069
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
  • Loading branch information
mpvl committed Sep 15, 2020
1 parent c72ce5d commit df01042
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 37 deletions.
24 changes: 12 additions & 12 deletions cue/testdata/basicrewrite/000_errors.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,38 @@ c: b.a == _|_
d: _|_ != b.a
e: _|_ == _|_
-- out/def --
a: _|_ // from source
b: _|_ // from source
a: _|_ // explicit error (_|_ literal) in source
b: _|_ // explicit error (_|_ literal) in source
c: true
d: false
e: true
-- out/legacy-debug --
<0>{a: _|_(from source), b: _|_(from source), c: true, d: false, e: true}
<0>{a: _|_(explicit error (_|_ literal) in source), b: _|_(explicit error (_|_ literal) in source), c: true, d: false, e: true}
-- out/compile --
--- in.cue
{
a: (_|_(from source) & _|_(from source))
b: (null & _|_(from source))
c: (〈0;b〉.a == _|_(from source))
d: (_|_(from source) != 〈0;b〉.a)
e: (_|_(from source) == _|_(from source))
a: (_|_(explicit error (_|_ literal) in source) & _|_(explicit error (_|_ literal) in source))
b: (null & _|_(explicit error (_|_ literal) in source))
c: (〈0;b〉.a == _|_(explicit error (_|_ literal) in source))
d: (_|_(explicit error (_|_ literal) in source) != 〈0;b〉.a)
e: (_|_(explicit error (_|_ literal) in source) == _|_(explicit error (_|_ literal) in source))
}
-- out/eval --
Errors:
from source:
explicit error (_|_ literal) in source:
./in.cue:1:4
from source:
explicit error (_|_ literal) in source:
./in.cue:2:11

Result:
(_|_){
// [user]
a: (_|_){
// [user] from source:
// [user] explicit error (_|_ literal) in source:
// ./in.cue:1:4
}
b: (_|_){
// [user] from source:
// [user] explicit error (_|_ literal) in source:
// ./in.cue:2:11
}
c: (bool){ true }
Expand Down
4 changes: 2 additions & 2 deletions cue/testdata/comprehensions/issue287.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ if #E["y"] != _|_ {
-- out/compile --
--- in.cue
{
if (〈0;#E〉["x"] != _|_(from source)) {
if (〈0;#E〉["x"] != _|_(explicit error (_|_ literal) in source)) {
#E: {
y: true
}
}
if (〈0;#E〉["y"] != _|_(from source)) {
if (〈0;#E〉["y"] != _|_(explicit error (_|_ literal) in source)) {
z: true
}
#E: {
Expand Down
6 changes: 3 additions & 3 deletions cue/testdata/eval/errunifiy.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ b: "t"

-- out/eval --
Errors:
from source:
explicit error (_|_ literal) in source:
./in.cue:4:4

Result:
(_|_){
// [user]
a: (string){ "t" }
b: (_|_){
// [user] from source:
// [user] explicit error (_|_ literal) in source:
// ./in.cue:4:4
}
}
Expand All @@ -26,6 +26,6 @@ Result:
{
a: or([])
a: "t"
b: _|_(from source)
b: _|_(explicit error (_|_ literal) in source)
b: "t"
}
14 changes: 7 additions & 7 deletions cue/testdata/fulleval/031_comparison_against_bottom.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ i: false
-- out/compile --
--- in.cue
{
a: (_|_(from source) == _|_(from source))
a: (_|_(explicit error (_|_ literal) in source) == _|_(explicit error (_|_ literal) in source))
b: ((〈0;err〉 == 1) & 2)
c: (〈0;err〉 == _|_(from source))
d: (〈0;err〉 != _|_(from source))
c: (〈0;err〉 == _|_(explicit error (_|_ literal) in source))
d: (〈0;err〉 != _|_(explicit error (_|_ literal) in source))
e: ((〈0;err〉 != 1) & 3)
f: (({
a: 1
} & {
a: 2
}) == _|_(from source))
}) == _|_(explicit error (_|_ literal) in source))
g: (({
a: 1
} & {
b: 2
}) == _|_(from source))
h: (_|_(from source) == ({
}) == _|_(explicit error (_|_ literal) in source))
h: (_|_(explicit error (_|_ literal) in source) == ({
a: 1
} & {
a: 2
}))
i: (_|_(from source) == ({
i: (_|_(explicit error (_|_ literal) in source) == ({
a: 1
} & {
b: 2
Expand Down
24 changes: 12 additions & 12 deletions cue/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,8 @@ func TestInt(t *testing.T) {
notInt: true,
}, {
value: "_|_",
err: "from source",
errU: "from source",
err: "explicit error (_|_ literal) in source",
errU: "explicit error (_|_ literal) in source",
notInt: true,
}}
for _, tc := range testCases {
Expand Down Expand Up @@ -485,7 +485,7 @@ func TestError(t *testing.T) {
err string
}{{
value: `_|_`,
err: "from source",
err: "explicit error (_|_ literal) in source",
}, {
value: `"Hello world!"`,
}, {
Expand All @@ -506,7 +506,7 @@ func TestNull(t *testing.T) {
err string
}{{
value: `v: _|_`,
err: "from source",
err: "explicit error (_|_ literal) in source",
}, {
value: `v: "str"`,
err: "cannot use value \"str\" (type string) as null",
Expand All @@ -531,7 +531,7 @@ func TestBool(t *testing.T) {
err string
}{{
value: `_|_`,
err: "from source",
err: "explicit error (_|_ literal) in source",
}, {
value: `"str"`,
err: "cannot use value \"str\" (type string) as bool",
Expand Down Expand Up @@ -563,7 +563,7 @@ func TestList(t *testing.T) {
err string
}{{
value: `_|_`,
err: "from source",
err: "explicit error (_|_ literal) in source",
}, {
value: `"str"`,
err: "cannot use value \"str\" (type string) as list",
Expand Down Expand Up @@ -614,7 +614,7 @@ func TestFields(t *testing.T) {
res: "{reg:4,}",
}, {
value: `_|_`,
err: "from source",
err: "explicit error (_|_ literal) in source",
}, {
value: `"str"`,
err: "cannot use value \"str\" (type string) as struct",
Expand Down Expand Up @@ -1324,7 +1324,7 @@ func TestDecode(t *testing.T) {
err string
}{{
value: `_|_`,
err: "from source",
err: "explicit error (_|_ literal) in source",
}, {
value: `"str"`,
dst: new(string),
Expand Down Expand Up @@ -1607,11 +1607,11 @@ func TestValueLookup(t *testing.T) {
}{{
config: "_|_",
path: strList(""),
str: "from source",
str: "explicit error (_|_ literal) in source",
}, {
config: "_|_",
path: strList("a"),
str: "from source",
str: "explicit error (_|_ literal) in source",
}, {
config: config,
path: strList(),
Expand Down Expand Up @@ -2107,7 +2107,7 @@ func TestMarshalJSON(t *testing.T) {
json: `null`,
}, {
value: `_|_`,
err: "from source",
err: "explicit error (_|_ literal) in source",
}, {
value: `(a.b)
a: {}`,
Expand Down Expand Up @@ -2272,7 +2272,7 @@ func TestWalk(t *testing.T) {
out: `null`,
}, {
value: `_|_`,
out: "_|_(from source)",
out: "_|_(explicit error (_|_ literal) in source)",
}, {
value: `(a.b)
a: {}`,
Expand Down
2 changes: 1 addition & 1 deletion internal/core/compile/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -778,7 +778,7 @@ func (c *compiler) expr(expr ast.Expr) adt.Expr {
return &adt.Bottom{
Src: n,
Code: adt.UserError,
Err: errors.Newf(n.Pos(), "from source"),
Err: errors.Newf(n.Pos(), "explicit error (_|_ literal) in source"),
}

case *ast.BadExpr:
Expand Down

0 comments on commit df01042

Please sign in to comment.