Files
Johan Fylling 7bb6dbe36b Preparing for v1 API
Moving (most) source to v1 root package to prepare for v0/v1 API separation.

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
2024-12-12 15:09:03 +01:00

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
}