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
|
defaultPrettyLimit = 80
|
||||||
)
|
)
|
||||||
|
|
||||||
type regoError struct{}
|
type regoError struct {
|
||||||
|
wrapped error
|
||||||
|
}
|
||||||
|
|
||||||
func (regoError) Error() string {
|
func (regoError) Error() string {
|
||||||
return "rego"
|
return "rego"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r regoError) Unwrap() error {
|
||||||
|
return r.wrapped
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
params := newEvalCommandParams()
|
params := newEvalCommandParams()
|
||||||
|
|
||||||
@@ -304,7 +310,7 @@ access.
|
|||||||
if _, ok := err.(regoError); !ok {
|
if _, ok := err.(regoError); !ok {
|
||||||
fmt.Fprintln(os.Stderr, err)
|
fmt.Fprintln(os.Stderr, err)
|
||||||
}
|
}
|
||||||
return newExitError(2)
|
return newExitErrorWrap(2, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (params.fail && !defined) || (params.failDefined && defined) {
|
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
|
// 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
|
// that the command doesn't print the same error twice. The error will
|
||||||
// have been printed above by the presentation package.
|
// have been printed above by the presentation package.
|
||||||
return false, regoError{}
|
return false, regoError{wrapped: result.Errors}
|
||||||
} else if len(result.Result) == 0 {
|
} else if len(result.Result) == 0 {
|
||||||
return false, nil
|
return false, nil
|
||||||
}
|
}
|
||||||
|
|||||||
+11
-2
@@ -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
|
// Use of this source code is governed by an Apache2
|
||||||
// license that can be found in the LICENSE file.
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
@@ -9,13 +9,22 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type ExitError struct {
|
type ExitError struct {
|
||||||
Exit int
|
Exit int
|
||||||
|
wrapped error
|
||||||
}
|
}
|
||||||
|
|
||||||
func newExitError(exit int) error {
|
func newExitError(exit int) error {
|
||||||
return &ExitError{Exit: exit}
|
return &ExitError{Exit: exit}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newExitErrorWrap(exit int, err error) error {
|
||||||
|
return &ExitError{Exit: exit, wrapped: err}
|
||||||
|
}
|
||||||
|
|
||||||
func (c *ExitError) Error() string {
|
func (c *ExitError) Error() string {
|
||||||
return fmt.Sprintf("exit %d", c.Exit)
|
return fmt.Sprintf("exit %d", c.Exit)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *ExitError) Unwrap() error {
|
||||||
|
return c.wrapped
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user