Files
releases/rego/errors.go
T
Stephan Renatus f2db31dd1c CLI: display error details for ErrorDetail errors in eval output (#3739)
Before, errors displayed through the presentation package's prettyError()
method would never show their details. The expectation in the code was
that any error that has details would include them in its own Error()
string method.

When error types return their details using an interface, such as

    type ErrorDetail interface {
        Lines() []string
    }

and their Error() method only return a short error message, then that
detail would never make it to the user in any of the non-JSON output
variants.

Before:

    $ opa eval -t wasm '1+1'
    {
      "errors": [
        {
          "message": "engine not found",
          "details": "WebAssembly runtime not supported in this build.\n----------------------------------------------------------------------------------\nPlease download an OPA binary with Wasm enabled from\nhttps://www.openpolicyagent.org/docs/latest/#running-opa\nor build it yourself (with Wasm enabled).\n----------------------------------------------------------------------------------\n"
        }
      ]
    }
    $ opa -fpretty -t wasm '1+1'
    1 error occurred: engine not found

Now:

    $ opa eval -t wasm '1+1'
    {
      "errors": [
        {
          "message": "engine not found",
          "details": "WebAssembly runtime not supported in this build.\n----------------------------------------------------------------------------------\nPlease download an OPA binary with Wasm enabled from\nhttps://www.openpolicyagent.org/docs/latest/#running-opa\nor build it yourself (with Wasm enabled).\n----------------------------------------------------------------------------------"
        }
      ]
    }
    $ opa eval -fpretty -t wasm '1+1'
    1 error occurred: engine not found
    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).
    ----------------------------------------------------------------------------------

This also surfaces the error details in REPL sessions:

    $ opa run
    OPA 0.32.0-dev (commit 3da95f9c-dirty, built at 2021-08-16T11:24:46Z)

    Run 'help' to see a list of commands and check for updates.

    > target wasm
    > true
    1 error occurred: engine not found
    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).
    ----------------------------------------------------------------------------------
    >

Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
2021-08-18 19:31:53 +02:00

25 lines
600 B
Go

package rego
// HaltError is an error type to return from a custom function implementation
// that will abort the evaluation process (analogous to topdown.Halt).
type HaltError struct {
err error
}
// Error delegates to the wrapped error
func (h *HaltError) Error() string {
return h.err.Error()
}
// NewHaltError wraps an error such that the evaluation process will stop
// when it occurs.
func NewHaltError(err error) error {
return &HaltError{err: err}
}
// ErrorDetails interface is satisfied by an error that provides further
// details.
type ErrorDetails interface {
Lines() []string
}