Skip to content

Commit

Permalink
improve more error reporting
Browse files Browse the repository at this point in the history
Signed-off-by: Tony Worm <tony@hofstadter.io>
  • Loading branch information
verdverm committed Dec 30, 2021
1 parent df7d629 commit 2127847
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
5 changes: 5 additions & 0 deletions lib/cuetils/print.go
Expand Up @@ -128,6 +128,11 @@ func CueErrorToString(err error) string {
return w.String()
}

func ExpandCueError(err error) error {
s := CueErrorToString(err)
return fmt.Errorf(s)
}

func PrintCueError(err error) {
s := CueErrorToString(err)
fmt.Println(s)
Expand Down
25 changes: 9 additions & 16 deletions lib/cuetils/runtime.go
Expand Up @@ -115,16 +115,17 @@ func (CRT *CueRuntime) load() (err error) {
// fmt.Printf("%d: start\n", i)

if bi.Err != nil {
s := CueErrorToString(bi.Err)
errs = append(errs, fmt.Errorf(s))
es := errors.Errors(bi.Err)
for _, e := range es {
errs = append(errs, e.(error))
}
continue
}

// Build the Instance
V := CRT.CueContext.BuildInstance(bi)
if V.Err() != nil {
es := errors.Errors(V.Err())
// fmt.Println("BUILD ERR", es, I)
for _, e := range es {
errs = append(errs, e.(error))
}
Expand All @@ -133,24 +134,16 @@ func (CRT *CueRuntime) load() (err error) {

CRT.CueValue = V

// Decode? we want to be lazy
/*
err = V.Decode(&CRT.Value)
if err != nil {
errs = append(errs, err)
continue
}
*/

// fmt.Println(i, "decoded", CRT.Value)

}

if len(errs) > 0 {
CRT.CueErrors = errs
s := fmt.Sprintf("Errors while loading Cue entrypoints: %s %v\n", CRT.Workspace, CRT.Entrypoints)
for _, e := range errs {
s += fmt.Sprintf("%v\n", e.Error())
for _, E := range errs {
es := errors.Errors(E)
for _, e := range es {
s += CueErrorToString(e)
}
}
return fmt.Errorf(s)
}
Expand Down
18 changes: 9 additions & 9 deletions lib/datamodel/load.go
Expand Up @@ -112,15 +112,15 @@ func loadDatamodelsAt(entrypoints []string, flgs flags.DatamodelPflagpole) ([]*D

kvs, err := cuetils.GetByAttrKeys(crt.CueValue, "datamodel", nil, nil)
if err != nil {
return dms, err
return dms, cuetils.ExpandCueError(err)
}

for _, kv := range kvs {
// decode and setup datamodel
var dm Datamodel
err = kv.Val.Decode(&dm)
if err != nil {
return dms, err
return dms, cuetils.ExpandCueError(err)
}
dm.Label = kv.Key
dm.Version = "dirty-" + tag // set to current timestamp
Expand All @@ -139,19 +139,19 @@ func loadDatamodelsAt(entrypoints []string, flgs flags.DatamodelPflagpole) ([]*D
)

if err != nil {
return dms, err
return dms, cuetils.ExpandCueError(err)
}

dm.Value = crt.CueContext.CompileString(str)

// go deeper to extract model values
ms := dm.Value.LookupPath(cue.ParsePath("Models"))
if ms.Err() != nil {
return dms, ms.Err()
return dms, cuetils.ExpandCueError(ms.Err())
}
iter, err := ms.Fields()
if err != nil {
return dms, err
return dms, cuetils.ExpandCueError(err)
}

i := 0
Expand Down Expand Up @@ -232,7 +232,7 @@ func loadDatamodelHistory(dm *Datamodel, crt *cuetils.CueRuntime) error {
vers := []*Datamodel{}
iter, err := crt.CueValue.Fields()
if err != nil {
return err
return cuetils.ExpandCueError(err)
}

for iter.Next() {
Expand All @@ -245,7 +245,7 @@ func loadDatamodelHistory(dm *Datamodel, crt *cuetils.CueRuntime) error {
var d Datamodel
err := value.Decode(&d)
if err != nil {
return err
return cuetils.ExpandCueError(err)
}

// set extra values
Expand All @@ -257,11 +257,11 @@ func loadDatamodelHistory(dm *Datamodel, crt *cuetils.CueRuntime) error {
// go deeper to extract model values
ms := d.Value.LookupPath(cue.ParsePath("Models"))
if ms.Err() != nil {
return ms.Err()
return cuetils.ExpandCueError(ms.Err())
}
iter, err := ms.Fields()
if err != nil {
return err
return cuetils.ExpandCueError(err)
}

i := 0
Expand Down

0 comments on commit 2127847

Please sign in to comment.