client/errors.go
4f0d95fa
 package client // import "github.com/docker/docker/client"
7c36a1af
 
 import (
 	"fmt"
81bb9978
 	"net/http"
 
e98e4a71
 	"github.com/docker/docker/api/types/versions"
421b66a4
 	"github.com/docker/docker/errdefs"
d5dc9b8b
 	"github.com/pkg/errors"
7c36a1af
 )
 
d5dc9b8b
 // errConnectionFailed implements an error returned when connection failed.
 type errConnectionFailed struct {
 	host string
 }
 
 // Error returns a string representation of an errConnectionFailed
 func (err errConnectionFailed) Error() string {
 	if err.host == "" {
 		return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?"
 	}
 	return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host)
 }
 
 // IsErrConnectionFailed returns true if the error is caused by connection failed.
 func IsErrConnectionFailed(err error) bool {
 	_, ok := errors.Cause(err).(errConnectionFailed)
 	return ok
 }
7c36a1af
 
 // ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
 func ErrorConnectionFailed(host string) error {
d5dc9b8b
 	return errConnectionFailed{host: host}
7c36a1af
 }
 
053c6f09
 // Deprecated: use the errdefs.NotFound() interface instead. Kept for backward compatibility
7c36a1af
 type notFound interface {
 	error
053c6f09
 	NotFound() bool
7c36a1af
 }
 
54242cd0
 // IsErrNotFound returns true if the error is a NotFound error, which is returned
 // by the API when some object is not found.
7c36a1af
 func IsErrNotFound(err error) bool {
421b66a4
 	if _, ok := err.(notFound); ok {
 		return ok
 	}
 	return errdefs.IsNotFound(err)
7c36a1af
 }
 
81bb9978
 type objectNotFoundError struct {
 	object string
 	id     string
7c36a1af
 }
 
5d8ece52
 func (e objectNotFoundError) NotFound() {}
7c36a1af
 
81bb9978
 func (e objectNotFoundError) Error() string {
 	return fmt.Sprintf("Error: No such %s: %s", e.object, e.id)
 }
 
 func wrapResponseError(err error, resp serverResponse, object, id string) error {
 	switch {
 	case err == nil:
 		return nil
 	case resp.statusCode == http.StatusNotFound:
 		return objectNotFoundError{object: object, id: id}
e7e11bdd
 	case resp.statusCode == http.StatusNotImplemented:
421b66a4
 		return errdefs.NotImplemented(err)
81bb9978
 	default:
 		return err
 	}
7c36a1af
 }
 
 // unauthorizedError represents an authorization error in a remote registry.
 type unauthorizedError struct {
 	cause error
 }
 
 // Error returns a string representation of an unauthorizedError
 func (u unauthorizedError) Error() string {
 	return u.cause.Error()
 }
 
 // IsErrUnauthorized returns true if the error is caused
 // when a remote registry authentication fails
 func IsErrUnauthorized(err error) bool {
421b66a4
 	if _, ok := err.(unauthorizedError); ok {
 		return ok
 	}
 	return errdefs.IsUnauthorized(err)
7c36a1af
 }
 
 type pluginPermissionDenied struct {
 	name string
 }
 
 func (e pluginPermissionDenied) Error() string {
 	return "Permission denied while installing plugin " + e.name
 }
 
 // IsErrPluginPermissionDenied returns true if the error is caused
 // when a user denies a plugin's permissions
 func IsErrPluginPermissionDenied(err error) bool {
 	_, ok := err.(pluginPermissionDenied)
 	return ok
 }
e98e4a71
 
e7e11bdd
 type notImplementedError struct {
 	message string
 }
 
 func (e notImplementedError) Error() string {
 	return e.message
 }
 
 func (e notImplementedError) NotImplemented() bool {
 	return true
 }
 
74060888
 // IsErrNotImplemented returns true if the error is a NotImplemented error.
e7e11bdd
 // This is returned by the API when a requested feature has not been
 // implemented.
74060888
 func IsErrNotImplemented(err error) bool {
421b66a4
 	if _, ok := err.(notImplementedError); ok {
 		return ok
 	}
 	return errdefs.IsNotImplemented(err)
e7e11bdd
 }
 
e98e4a71
 // NewVersionError returns an error if the APIVersion required
 // if less than the current supported version
 func (cli *Client) NewVersionError(APIrequired, feature string) error {
ff2ed185
 	if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
34148978
 		return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
e98e4a71
 	}
 	return nil
 }