cmd: use regoError to carry compiler errors into CLI machinery

Signed-off-by: Stephan Renatus <stephan@styra.com>
This commit is contained in:
Stephan Renatus
2023-12-18 14:17:34 +01:00
parent 36bae2aac6
commit e3f6be6c22
2 changed files with 20 additions and 5 deletions
+9 -3
View File
@@ -175,12 +175,18 @@ const (
defaultPrettyLimit = 80
)
type regoError struct{}
type regoError struct {
wrapped error
}
func (regoError) Error() string {
return "rego"
}
func (r regoError) Unwrap() error {
return r.wrapped
}
func init() {
params := newEvalCommandParams()
@@ -304,7 +310,7 @@ access.
if _, ok := err.(regoError); !ok {
fmt.Fprintln(os.Stderr, err)
}
return newExitError(2)
return newExitErrorWrap(2, err)
}
if (params.fail && !defined) || (params.failDefined && defined) {
@@ -448,7 +454,7 @@ func eval(args []string, params evalCommandParams, w io.Writer) (bool, error) {
// If the rego package returned an error, return a special error here so
// that the command doesn't print the same error twice. The error will
// have been printed above by the presentation package.
return false, regoError{}
return false, regoError{wrapped: result.Errors}
} else if len(result.Result) == 0 {
return false, nil
}
+11 -2
View File
@@ -1,4 +1,4 @@
// Copyright 2023 The OPA Authors. All rights reserved.
// Copyright 2025 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
@@ -9,13 +9,22 @@ import (
)
type ExitError struct {
Exit int
Exit int
wrapped error
}
func newExitError(exit int) error {
return &ExitError{Exit: exit}
}
func newExitErrorWrap(exit int, err error) error {
return &ExitError{Exit: exit, wrapped: err}
}
func (c *ExitError) Error() string {
return fmt.Sprintf("exit %d", c.Exit)
}
func (c *ExitError) Unwrap() error {
return c.wrapped
}