mirror of
https://github.com/open-policy-agent/opa.git
synced 2026-08-14 04:12:41 -06:00
7bb6dbe36b
Moving (most) source to v1 root package to prepare for v0/v1 API separation. Signed-off-by: Johan Fylling <johan.dev@fylling.se>
54 lines
1.2 KiB
Go
54 lines
1.2 KiB
Go
package bundle
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/open-policy-agent/opa/v1/download"
|
|
)
|
|
|
|
// Errors represents a list of errors that occurred during a bundle load enriched by the bundle name.
|
|
type Errors []Error
|
|
|
|
func (e Errors) Unwrap() []error {
|
|
output := make([]error, len(e))
|
|
for i := range e {
|
|
output[i] = e[i]
|
|
}
|
|
return output
|
|
}
|
|
func (e Errors) Error() string {
|
|
err := errors.Join(e.Unwrap()...)
|
|
return err.Error()
|
|
}
|
|
|
|
type Error struct {
|
|
BundleName string
|
|
Code string
|
|
HTTPCode int
|
|
Message string
|
|
Err error
|
|
}
|
|
|
|
func NewBundleError(bundleName string, cause error) Error {
|
|
var (
|
|
httpError download.HTTPError
|
|
)
|
|
switch {
|
|
case cause == nil:
|
|
return Error{BundleName: bundleName, Code: "", HTTPCode: -1, Message: "", Err: nil}
|
|
case errors.As(cause, &httpError):
|
|
return Error{BundleName: bundleName, Code: errCode, HTTPCode: httpError.StatusCode, Message: httpError.Error(), Err: cause}
|
|
default:
|
|
return Error{BundleName: bundleName, Code: errCode, HTTPCode: -1, Message: cause.Error(), Err: cause}
|
|
}
|
|
}
|
|
|
|
func (e Error) Error() string {
|
|
return fmt.Sprintf("Bundle name: %s, Code: %s, HTTPCode: %d, Message: %s", e.BundleName, errCode, e.HTTPCode, e.Message)
|
|
}
|
|
|
|
func (e Error) Unwrap() error {
|
|
return e.Err
|
|
}
|