Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLI: display error details for ErrorDetail errors in eval output #3739

Merged
merged 2 commits into from
Aug 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions internal/presentation/presentation.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ func NewOutputErrors(err error) []OutputError {
Message: err.Error(),
err: typedErr,
}}
if d, ok := err.(rego.ErrorWithDetails); ok {
errs[0].Details = d.Details()
if d, ok := err.(rego.ErrorDetails); ok {
details := strings.Join(d.Lines(), "\n")
errs[0].Details = details
}
}
}
Expand All @@ -215,16 +216,22 @@ func (e OutputErrors) Error() string {
return "no error(s)"
}

var prefix string
if len(e) == 1 {
return fmt.Sprintf("1 error occurred: %v", e[0].Error())
prefix = "1 error occurred: "
} else {
prefix = fmt.Sprintf("%d errors occurred:\n", len(e))
}

var s []string
for _, err := range e {
s = append(s, err.Error())
if l, ok := err.Details.(string); ok {
s = append(s, l)
}
}

return fmt.Sprintf("%d errors occurred:\n%s", len(e), strings.Join(s, "\n"))
return prefix + strings.Join(s, "\n")
}

// OutputError provides a common structure for all OPA
Expand Down
10 changes: 3 additions & 7 deletions internal/presentation/presentation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,8 @@ func (t *testErrorWithMarshaller) MarshalJSON() ([]byte, error) {

type testErrorWithDetails struct{}

func (*testErrorWithDetails) Error() string { return "something went wrong" }
func (*testErrorWithDetails) Details() string {
return `oh
so
wrong`
}
func (*testErrorWithDetails) Error() string { return "something went wrong" }
func (*testErrorWithDetails) Lines() []string { return []string{"oh", "so", "wrong"} }

func validateJSONOutput(t *testing.T, testErr error, expected string) {
t.Helper()
Expand Down Expand Up @@ -487,7 +483,7 @@ func TestRaw(t *testing.T) {
output: Output{
Errors: NewOutputErrors(&testErrorWithDetails{}),
},
want: "1 error occurred: something went wrong\n",
want: "1 error occurred: something went wrong\noh\nso\nwrong\n",
},
}

Expand Down
17 changes: 9 additions & 8 deletions internal/rego/opa/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ var ErrEngineNotFound error = &errEngineNotFound{}
type errEngineNotFound struct{}

func (*errEngineNotFound) Error() string { return "engine not found" }
func (*errEngineNotFound) Details() string {
return `WebAssembly runtime not supported in this build.
----------------------------------------------------------------------------------
Please download an OPA binary with Wasm enabled from
https://www.openpolicyagent.org/docs/latest/#running-opa
or build it yourself (with Wasm enabled).
----------------------------------------------------------------------------------
`
func (*errEngineNotFound) Lines() []string {
return []string{
`WebAssembly runtime not supported in this build.`,
`----------------------------------------------------------------------------------`,
`Please download an OPA binary with Wasm enabled from`,
`https://www.openpolicyagent.org/docs/latest/#running-opa`,
`or build it yourself (with Wasm enabled).`,
`----------------------------------------------------------------------------------`,
}
}

// Engine repesents a factory for instances of EvalEngine implementations
Expand Down
6 changes: 3 additions & 3 deletions rego/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ func NewHaltError(err error) error {
return &HaltError{err: err}
}

// ErrorWithDetails interface is satisfied by an error that provides further
// ErrorDetails interface is satisfied by an error that provides further
// details.
type ErrorWithDetails interface {
Details() string
type ErrorDetails interface {
Lines() []string
}