Skip to content

Commit

Permalink
internal/filetpes: improve error messages
Browse files Browse the repository at this point in the history
Fixes #384

Change-Id: I0cf677ccc1aa4e285cb7600b10235edd7d5e7ed6
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/7803
Reviewed-by: Marcel van Lohuizen <mpvl@golang.org>
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
  • Loading branch information
mpvl committed Nov 28, 2020
1 parent d0dd888 commit ec6f95d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/cue/cmd/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func TestScript(t *testing.T) {
// Usage Comment out t.Skip() and set file to test.
func TestX(t *testing.T) {
t.Skip()
const path = "./testdata/script/eval_context.txt"
const path = "./testdata/script/eval_e.txt"

check := func(err error) {
t.Helper()
Expand Down
10 changes: 10 additions & 0 deletions cmd/cue/cmd/testdata/script/eval_e.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ cue eval -e incomplete
cmp stderr expect/incomplete/stderr
cmp stdout expect/incomplete/stdout

# Issue #384
! cue eval foo.bar
cmp stderr expect/foobar/stderr

-- expect/nonExist/stdout --
-- expect/nonExist/stderr --
Expand All @@ -15,6 +18,11 @@ reference "nonExist" not found:
-- expect/incomplete/stdout --

-- expect/incomplete/stderr --
-- expect/foobar/stdout --

-- expect/foobar/stderr --
unknown file extension .bar
-- input/ --
-- partial.cue --
package exitcode

Expand All @@ -26,6 +34,8 @@ a: 1

incomplete: pkg.Settings

foo: bar: "hello"

-- cue.mod/pkg/foo.com/example/example.cue --
package example

Expand Down
28 changes: 21 additions & 7 deletions internal/filetypes/filetypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func FromFile(b *build.File, mode Mode) (*FileInfo, error) {
}

i := cuegenInstance.Value()
i = i.Unify(i.Lookup("modes", mode.String()))
i, errs := update(nil, i, i, "modes", mode.String())
v := i.LookupDef("FileInfo")
v = v.Fill(b)

Expand All @@ -110,25 +110,34 @@ func FromFile(b *build.File, mode Mode) (*FileInfo, error) {

interpretation, _ := v.Lookup("interpretation").String()
if b.Form != "" {
v = v.Unify(i.Lookup("forms", string(b.Form)))
v, errs = update(errs, v, i, "forms", string(b.Form))
// may leave some encoding-dependent options open in data mode.
} else if interpretation != "" {
// always sets schema form.
v = v.Unify(i.Lookup("interpretations", interpretation))
v, errs = update(errs, v, i, "interpretations", interpretation)
}
if interpretation == "" {
s, err := v.Lookup("encoding").String()
if err != nil {
return nil, err
}
v = v.Unify(i.Lookup("encodings", s))
v, errs = update(errs, v, i, "encodings", s)
}

fi := &FileInfo{}
if err := v.Decode(fi); err != nil {
return nil, err
return nil, errors.Wrapf(err, token.NoPos, "could not parse arguments")
}
return fi, nil
return fi, errs
}

func update(errs errors.Error, v, i cue.Value, field, value string) (cue.Value, errors.Error) {
v = v.Unify(i.Lookup(field, value))
if err := v.Err(); err != nil {
errs = errors.Append(errs,
errors.Newf(token.NoPos, "unknown %s %s", field, value))
}
return v, errs
}

// ParseArgs converts a sequence of command line arguments representing
Expand Down Expand Up @@ -245,6 +254,10 @@ func toFile(i, v cue.Value, filename string) (*build.File, error) {
} else if ext := filepath.Ext(filename); ext != "" {
if x := i.Lookup("extensions", ext); x.Exists() || !hasDefault {
v = v.Unify(x)
if err := v.Err(); err != nil {
return nil, errors.Newf(token.NoPos,
"unknown file extension %s", ext)
}
}
} else if !hasDefault {
return nil, errors.Newf(token.NoPos,
Expand All @@ -254,7 +267,8 @@ func toFile(i, v cue.Value, filename string) (*build.File, error) {

f := &build.File{}
if err := v.Decode(&f); err != nil {
return nil, err
return nil, errors.Wrapf(err, token.NoPos,
"could not determine file type")
}
return f, nil
}
Expand Down

0 comments on commit ec6f95d

Please sign in to comment.