mirror of
https://github.com/holos-run/holos.git
synced 2026-03-21 09:45:00 +00:00
This patch prints out the cue file and line numbers when a cue error
contains multiple go errors to unwrap.
For example:
```
❯ holos render --cluster-name=k2 ~/workspace/holos-run/holos/docs/examples/platforms/reference/clusters/workload/...
3:31PM ERR could not execute version=0.46.0 err="could not decode: content: error in call to encoding/yaml.MarshalStream: incomplete value string (and 1 more errors)" loc=builder.go:212
content: error in call to encoding/yaml.MarshalStream: incomplete value string:
/home/jeff/workspace/holos-run/holos/docs/examples/schema.cue:199:11
/home/jeff/workspace/holos-run/holos/docs/examples/cue.mod/gen/external-secrets.io/externalsecret/v1beta1/types_gen.cue:83:14
```
43 lines
1.1 KiB
Go
43 lines
1.1 KiB
Go
package cli
|
|
|
|
import (
|
|
"context"
|
|
"cuelang.org/go/cue/errors"
|
|
"fmt"
|
|
"github.com/holos-run/holos/pkg/holos"
|
|
"github.com/holos-run/holos/pkg/wrapper"
|
|
"log/slog"
|
|
)
|
|
|
|
// MakeMain makes a main function for the cli or tests.
|
|
func MakeMain(options ...holos.Option) func() int {
|
|
return func() (exitCode int) {
|
|
cfg := holos.New(options...)
|
|
slog.SetDefault(cfg.Logger())
|
|
ctx := context.Background()
|
|
if err := New(cfg).ExecuteContext(ctx); err != nil {
|
|
return handleError(ctx, err, cfg)
|
|
}
|
|
return 0
|
|
}
|
|
}
|
|
|
|
// handleError is the top level error handler that unwraps and logs errors.
|
|
func handleError(ctx context.Context, err error, hc *holos.Config) (exitCode int) {
|
|
log := hc.NewTopLevelLogger()
|
|
var cueErr errors.Error
|
|
var errAt *wrapper.ErrorAt
|
|
const msg = "could not execute"
|
|
if errors.As(err, &errAt) {
|
|
log.ErrorContext(ctx, msg, "err", errAt.Unwrap(), "loc", errAt.Source.Loc())
|
|
} else {
|
|
log.ErrorContext(ctx, msg, "err", err)
|
|
}
|
|
// cue errors are bundled up as a list and refer to multiple files / lines.
|
|
if errors.As(err, &cueErr) {
|
|
msg := errors.Details(cueErr, nil)
|
|
_, _ = fmt.Fprint(hc.Stderr(), msg)
|
|
}
|
|
return 1
|
|
}
|