mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-12 19:32:48 -06:00
cmd: use regoError to carry compiler errors into CLI machinery
Signed-off-by: Stephan Renatus <stephan@styra.com>
This commit is contained in:
+9
-3
@@ -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
|
||||
}
|
||||
|
||||
+10
-1
@@ -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.
|
||||
|
||||
@@ -10,12 +10,21 @@ import (
|
||||
|
||||
type ExitError struct {
|
||||
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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user