9
"github.com/blang/semver/v4"
10
"github.com/containers/podman/v5/pkg/errorhandling"
14
ErrNotImplemented = errors.New("function not implemented")
17
func handleError(data []byte, unmarshalErrorInto interface{}) error {
18
if err := json.Unmarshal(data, unmarshalErrorInto); err != nil {
19
return fmt.Errorf("unmarshalling error into %#v, data %q: %w", unmarshalErrorInto, string(data), err)
21
return unmarshalErrorInto.(error)
26
func (h APIResponse) Process(unmarshalInto interface{}) error {
27
return h.ProcessWithError(unmarshalInto, &errorhandling.ErrorModel{})
32
func (h APIResponse) ProcessWithError(unmarshalInto interface{}, unmarshalErrorInto interface{}) error {
33
data, err := io.ReadAll(h.Response.Body)
35
return fmt.Errorf("unable to process API response: %w", err)
37
if h.IsSuccess() || h.IsRedirection() {
38
if unmarshalInto != nil {
39
if err := json.Unmarshal(data, unmarshalInto); err != nil {
40
return fmt.Errorf("unmarshalling into %#v, data %q: %w", unmarshalInto, string(data), err)
47
if h.IsConflictError() {
48
return handleError(data, unmarshalErrorInto)
52
return handleError(data, &errorhandling.ErrorModel{})
55
func CheckResponseCode(inError error) (int, error) {
56
switch e := inError.(type) {
57
case *errorhandling.ErrorModel:
59
case *errorhandling.PodConflictErrorModel:
62
return -1, errors.New("is not type ErrorModel")
66
type APIVersionError struct {
68
serverVersion *semver.Version
69
requiredVersion string
77
func NewAPIVersionError(endpoint string, version *semver.Version, requiredVersion string) *APIVersionError {
78
return &APIVersionError{
80
serverVersion: version,
81
requiredVersion: requiredVersion,
85
func (e *APIVersionError) Error() string {
86
return fmt.Sprintf("API server version is %s, need at least %s to call %s", e.serverVersion.String(), e.requiredVersion, e.endpoint)