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