github.com/rhatdan/docker@v0.7.7-0.20180119204836-47a0dcbcd20a/client/errors.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"net/http"
     7  
     8  	"github.com/docker/docker/api/types/versions"
     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  	_, ok := errors.Cause(err).(errConnectionFailed)
    28  	return ok
    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  	return errConnectionFailed{host: host}
    34  }
    35  
    36  type notFound interface {
    37  	error
    38  	NotFound() bool // Is the error a NotFound error
    39  }
    40  
    41  // IsErrNotFound returns true if the error is a NotFound error, which is returned
    42  // by the API when some object is not found.
    43  func IsErrNotFound(err error) bool {
    44  	te, ok := err.(notFound)
    45  	return ok && te.NotFound()
    46  }
    47  
    48  type objectNotFoundError struct {
    49  	object string
    50  	id     string
    51  }
    52  
    53  func (e objectNotFoundError) NotFound() bool {
    54  	return true
    55  }
    56  
    57  func (e objectNotFoundError) Error() string {
    58  	return fmt.Sprintf("Error: No such %s: %s", e.object, e.id)
    59  }
    60  
    61  func wrapResponseError(err error, resp serverResponse, object, id string) error {
    62  	switch {
    63  	case err == nil:
    64  		return nil
    65  	case resp.statusCode == http.StatusNotFound:
    66  		return objectNotFoundError{object: object, id: id}
    67  	case resp.statusCode == http.StatusNotImplemented:
    68  		return notImplementedError{message: err.Error()}
    69  	default:
    70  		return err
    71  	}
    72  }
    73  
    74  // unauthorizedError represents an authorization error in a remote registry.
    75  type unauthorizedError struct {
    76  	cause error
    77  }
    78  
    79  // Error returns a string representation of an unauthorizedError
    80  func (u unauthorizedError) Error() string {
    81  	return u.cause.Error()
    82  }
    83  
    84  // IsErrUnauthorized returns true if the error is caused
    85  // when a remote registry authentication fails
    86  func IsErrUnauthorized(err error) bool {
    87  	_, ok := err.(unauthorizedError)
    88  	return ok
    89  }
    90  
    91  type pluginPermissionDenied struct {
    92  	name string
    93  }
    94  
    95  func (e pluginPermissionDenied) Error() string {
    96  	return "Permission denied while installing plugin " + e.name
    97  }
    98  
    99  // IsErrPluginPermissionDenied returns true if the error is caused
   100  // when a user denies a plugin's permissions
   101  func IsErrPluginPermissionDenied(err error) bool {
   102  	_, ok := err.(pluginPermissionDenied)
   103  	return ok
   104  }
   105  
   106  type notImplementedError struct {
   107  	message string
   108  }
   109  
   110  func (e notImplementedError) Error() string {
   111  	return e.message
   112  }
   113  
   114  func (e notImplementedError) NotImplemented() bool {
   115  	return true
   116  }
   117  
   118  // IsErrNotImplemented returns true if the error is a NotImplemented error.
   119  // This is returned by the API when a requested feature has not been
   120  // implemented.
   121  func IsErrNotImplemented(err error) bool {
   122  	te, ok := err.(notImplementedError)
   123  	return ok && te.NotImplemented()
   124  }
   125  
   126  // NewVersionError returns an error if the APIVersion required
   127  // if less than the current supported version
   128  func (cli *Client) NewVersionError(APIrequired, feature string) error {
   129  	if cli.version != "" && versions.LessThan(cli.version, APIrequired) {
   130  		return fmt.Errorf("%q requires API version %s, but the Docker daemon API version is %s", feature, APIrequired, cli.version)
   131  	}
   132  	return nil
   133  }