podman

Форк
0
/
errors.go 
87 строк · 2.6 Кб
1
package bindings
2

3
import (
4
	"encoding/json"
5
	"errors"
6
	"fmt"
7
	"io"
8

9
	"github.com/blang/semver/v4"
10
	"github.com/containers/podman/v5/pkg/errorhandling"
11
)
12

13
var (
14
	ErrNotImplemented = errors.New("function not implemented")
15
)
16

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)
20
	}
21
	return unmarshalErrorInto.(error)
22
}
23

24
// Process drains the response body, and processes the HTTP status code
25
// Note: Closing the response.Body is left to the caller
26
func (h APIResponse) Process(unmarshalInto interface{}) error {
27
	return h.ProcessWithError(unmarshalInto, &errorhandling.ErrorModel{})
28
}
29

30
// ProcessWithError drains the response body, and processes the HTTP status code
31
// Note: Closing the response.Body is left to the caller
32
func (h APIResponse) ProcessWithError(unmarshalInto interface{}, unmarshalErrorInto interface{}) error {
33
	data, err := io.ReadAll(h.Response.Body)
34
	if err != nil {
35
		return fmt.Errorf("unable to process API response: %w", err)
36
	}
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)
41
			}
42
			return nil
43
		}
44
		return nil
45
	}
46

47
	if h.IsConflictError() {
48
		return handleError(data, unmarshalErrorInto)
49
	}
50

51
	// TODO should we add a debug here with the response code?
52
	return handleError(data, &errorhandling.ErrorModel{})
53
}
54

55
func CheckResponseCode(inError error) (int, error) {
56
	switch e := inError.(type) {
57
	case *errorhandling.ErrorModel:
58
		return e.Code(), nil
59
	case *errorhandling.PodConflictErrorModel:
60
		return e.Code(), nil
61
	default:
62
		return -1, errors.New("is not type ErrorModel")
63
	}
64
}
65

66
type APIVersionError struct {
67
	endpoint        string
68
	serverVersion   *semver.Version
69
	requiredVersion string
70
}
71

72
// NewAPIVersionError create bindings error when the endpoint on the server is not supported
73
// because the version is to old.
74
//   - endpoint is the name for the endpoint (e.g. /containers/json)
75
//   - version is the server API version
76
//   - requiredVersion is the server version need to use said endpoint
77
func NewAPIVersionError(endpoint string, version *semver.Version, requiredVersion string) *APIVersionError {
78
	return &APIVersionError{
79
		endpoint:        endpoint,
80
		serverVersion:   version,
81
		requiredVersion: requiredVersion,
82
	}
83
}
84

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)
87
}
88

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.