github.com/rish1988/moby@v25.0.2+incompatible/client/errors.go (about)

     1  package client // import "github.com/docker/docker/client"
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	"github.com/docker/docker/api/types/versions"
     8  	"github.com/docker/docker/errdefs"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // errConnectionFailed implements an error returned when connection failed.
    13  type errConnectionFailed struct {
    14  	host string
    15  }
    16  
    17  // Error returns a string representation of an errConnectionFailed
    18  func (err errConnectionFailed) Error() string {
    19  	if err.host == "" {
    20  		return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?"
    21  	}
    22  	return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host)
    23  }
    24  
    25  // IsErrConnectionFailed returns true if the error is caused by connection failed.
    26  func IsErrConnectionFailed(err error) bool {
    27  	return errors.As(err, &errConnectionFailed{})
    28  }
    29  
    30  // ErrorConnectionFailed returns an error with host in the error message when connection to docker daemon failed.
    31  func ErrorConnectionFailed(host string) error {
    32  	return errConnectionFailed{host: host}
    33  }
    34  
    35  // IsErrNotFound returns true if the error is a NotFound error, which is returned
    36  // by the API when some object is not found. It is an alias for [errdefs.IsNotFound].
    37  func IsErrNotFound(err error) bool {
    38  	return errdefs.IsNotFound(err)
    39  }
    40  
    41  type objectNotFoundError struct {
    42  	object string
    43  	id     string
    44  }
    45  
    46  func (e objectNotFoundError) NotFound() {}
    47  
    48  func (e objectNotFoundError) Error() string {
    49  	return fmt.Sprintf("Error: No such %s: %s", e.object, e.id)
    50  }
    51  
    52  // NewVersionError returns an error if the APIVersion required is less than the
    53  // current supported version.
    54  //
    55  // It performs API-version negotiation if the Client is configured with this
    56  // option, otherwise it assumes the latest API version is used.
    57  func (cli *Client) NewVersionError(ctx context.Context, APIrequired, feature string) error {
    58  	// Make sure we negotiated (if the client is configured to do so),
    59  	// as code below contains API-version specific handling of options.
    60  	//
    61  	// Normally, version-negotiation (if enabled) would not happen until
    62  	// the API request is made.
    63  	cli.checkVersion(ctx)
    64  	if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
    65  		return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
    66  	}
    67  	return nil
    68  }