github.com/adityamillind98/moby@v23.0.0-rc.4+incompatible/client/errors.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/docker/api/types/versions"
     7  	"github.com/docker/docker/errdefs"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  // errConnectionFailed implements an error returned when connection failed.
    12  type errConnectionFailed struct {
    13  	host string
    14  }
    15  
    16  // Error returns a string representation of an errConnectionFailed
    17  func (err errConnectionFailed) Error() string {
    18  	if err.host == "" {
    19  		return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?"
    20  	}
    21  	return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host)
    22  }
    23  
    24  // IsErrConnectionFailed returns true if the error is caused by connection failed.
    25  func IsErrConnectionFailed(err error) bool {
    26  	return errors.As(err, &errConnectionFailed{})
    27  }
    28  
    29  // ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
    30  func ErrorConnectionFailed(host string) error {
    31  	return errConnectionFailed{host: host}
    32  }
    33  
    34  // Deprecated: use the errdefs.NotFound() interface instead. Kept for backward compatibility
    35  type notFound interface {
    36  	error
    37  	NotFound() bool
    38  }
    39  
    40  // IsErrNotFound returns true if the error is a NotFound error, which is returned
    41  // by the API when some object is not found.
    42  func IsErrNotFound(err error) bool {
    43  	if errdefs.IsNotFound(err) {
    44  		return true
    45  	}
    46  	var e notFound
    47  	return errors.As(err, &e)
    48  }
    49  
    50  type objectNotFoundError struct {
    51  	object string
    52  	id     string
    53  }
    54  
    55  func (e objectNotFoundError) NotFound() {}
    56  
    57  func (e objectNotFoundError) Error() string {
    58  	return fmt.Sprintf("Error: No such %s: %s", e.object, e.id)
    59  }
    60  
    61  // IsErrUnauthorized returns true if the error is caused
    62  // when a remote registry authentication fails
    63  //
    64  // Deprecated: use errdefs.IsUnauthorized
    65  func IsErrUnauthorized(err error) bool {
    66  	return errdefs.IsUnauthorized(err)
    67  }
    68  
    69  type pluginPermissionDenied struct {
    70  	name string
    71  }
    72  
    73  func (e pluginPermissionDenied) Error() string {
    74  	return "Permission denied while installing plugin " + e.name
    75  }
    76  
    77  // IsErrNotImplemented returns true if the error is a NotImplemented error.
    78  // This is returned by the API when a requested feature has not been
    79  // implemented.
    80  //
    81  // Deprecated: use errdefs.IsNotImplemented
    82  func IsErrNotImplemented(err error) bool {
    83  	return errdefs.IsNotImplemented(err)
    84  }
    85  
    86  // NewVersionError returns an error if the APIVersion required
    87  // if less than the current supported version
    88  func (cli *Client) NewVersionError(APIrequired, feature string) error {
    89  	if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
    90  		return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
    91  	}
    92  	return nil
    93  }