2018-02-05 21:05:59 +00:00
|
|
|
package client // import "github.com/docker/docker/client"
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
import (
|
2023-09-12 12:08:54 +00:00
|
|
|
"context"
|
2016-09-06 18:46:37 +00:00
|
|
|
"fmt"
|
2017-09-08 16:04:34 +00:00
|
|
|
|
2016-11-03 00:43:32 +00:00
|
|
|
"github.com/docker/docker/api/types/versions"
|
2018-12-31 17:16:08 +00:00
|
|
|
"github.com/docker/docker/errdefs"
|
2016-11-22 02:42:55 +00:00
|
|
|
"github.com/pkg/errors"
|
2016-09-06 18:46:37 +00:00
|
|
|
)
|
|
|
|
|
2016-11-22 02:42:55 +00:00
|
|
|
// errConnectionFailed implements an error returned when connection failed.
|
|
|
|
type errConnectionFailed struct {
|
2024-02-23 14:13:22 +00:00
|
|
|
error
|
2016-11-22 02:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns a string representation of an errConnectionFailed
|
2024-02-23 14:13:22 +00:00
|
|
|
func (e errConnectionFailed) Error() string {
|
|
|
|
return e.error.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e errConnectionFailed) Unwrap() error {
|
|
|
|
return e.error
|
2016-11-22 02:42:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsErrConnectionFailed returns true if the error is caused by connection failed.
|
|
|
|
func IsErrConnectionFailed(err error) bool {
|
2020-04-17 10:01:01 +00:00
|
|
|
return errors.As(err, &errConnectionFailed{})
|
2016-11-22 02:42:55 +00:00
|
|
|
}
|
2016-09-06 18:46:37 +00:00
|
|
|
|
|
|
|
// ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
|
|
|
|
func ErrorConnectionFailed(host string) error {
|
2024-02-23 14:13:22 +00:00
|
|
|
var err error
|
|
|
|
if host == "" {
|
|
|
|
err = fmt.Errorf("Cannot connect to the Docker daemon. Is the docker daemon running on this host?")
|
|
|
|
} else {
|
|
|
|
err = fmt.Errorf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", host)
|
|
|
|
}
|
|
|
|
return errConnectionFailed{error: err}
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-07 16:22:11 +00:00
|
|
|
// IsErrNotFound returns true if the error is a NotFound error, which is returned
|
2023-05-10 11:36:19 +00:00
|
|
|
// by the API when some object is not found. It is an alias for [errdefs.IsNotFound].
|
2016-09-06 18:46:37 +00:00
|
|
|
func IsErrNotFound(err error) bool {
|
2023-05-10 11:36:19 +00:00
|
|
|
return errdefs.IsNotFound(err)
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2017-09-08 16:04:34 +00:00
|
|
|
type objectNotFoundError struct {
|
|
|
|
object string
|
|
|
|
id string
|
2016-09-06 18:46:37 +00:00
|
|
|
}
|
|
|
|
|
2018-12-31 17:18:41 +00:00
|
|
|
func (e objectNotFoundError) NotFound() {}
|
2016-09-06 18:46:37 +00:00
|
|
|
|
2017-09-08 16:04:34 +00:00
|
|
|
func (e objectNotFoundError) Error() string {
|
|
|
|
return fmt.Sprintf("Error: No such %s: %s", e.object, e.id)
|
|
|
|
}
|
|
|
|
|
2023-09-12 12:08:54 +00:00
|
|
|
// NewVersionError returns an error if the APIVersion required is less than the
|
|
|
|
// current supported version.
|
|
|
|
//
|
|
|
|
// It performs API-version negotiation if the Client is configured with this
|
|
|
|
// option, otherwise it assumes the latest API version is used.
|
|
|
|
func (cli *Client) NewVersionError(ctx context.Context, APIrequired, feature string) error {
|
|
|
|
// Make sure we negotiated (if the client is configured to do so),
|
|
|
|
// as code below contains API-version specific handling of options.
|
|
|
|
//
|
|
|
|
// Normally, version-negotiation (if enabled) would not happen until
|
|
|
|
// the API request is made.
|
2024-02-23 11:20:06 +00:00
|
|
|
if err := cli.checkVersion(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-06-30 05:08:42 +00:00
|
|
|
if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
|
2017-01-16 14:35:27 +00:00
|
|
|
return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
|
2016-11-03 00:43:32 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|