diff --git a/cmd/eval.go b/cmd/eval.go index cd6ccdc811..d76bccbdce 100644 --- a/cmd/eval.go +++ b/cmd/eval.go @@ -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 } diff --git a/cmd/utils.go b/cmd/utils.go index a16d0981f5..179767f3c7 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -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 +}