github.com/hms58/moby@v1.13.1/client/errors.go (about)

     1  package client
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/docker/docker/api/types/versions"
     7  	"github.com/pkg/errors"
     8  )
     9  
    10  // errConnectionFailed implements an error returned when connection failed.
    11  type errConnectionFailed struct {
    12  	host string
    13  }
    14  
    15  // Error returns a string representation of an errConnectionFailed
    16  func (err errConnectionFailed) Error() string {
    17  	if err.host == "" {
    18  		return "Cannot connect to the Docker daemon. Is the docker daemon running on this host?"
    19  	}
    20  	return fmt.Sprintf("Cannot connect to the Docker daemon at %s. Is the docker daemon running?", err.host)
    21  }
    22  
    23  // IsErrConnectionFailed returns true if the error is caused by connection failed.
    24  func IsErrConnectionFailed(err error) bool {
    25  	_, ok := errors.Cause(err).(errConnectionFailed)
    26  	return ok
    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  type notFound interface {
    35  	error
    36  	NotFound() bool // Is the error a NotFound error
    37  }
    38  
    39  // IsErrNotFound returns true if the error is caused with an
    40  // object (image, container, network, volume, …) is not found in the docker host.
    41  func IsErrNotFound(err error) bool {
    42  	te, ok := err.(notFound)
    43  	return ok && te.NotFound()
    44  }
    45  
    46  // imageNotFoundError implements an error returned when an image is not in the docker host.
    47  type imageNotFoundError struct {
    48  	imageID string
    49  }
    50  
    51  // NotFound indicates that this error type is of NotFound
    52  func (e imageNotFoundError) NotFound() bool {
    53  	return true
    54  }
    55  
    56  // Error returns a string representation of an imageNotFoundError
    57  func (e imageNotFoundError) Error() string {
    58  	return fmt.Sprintf("Error: No such image: %s", e.imageID)
    59  }
    60  
    61  // IsErrImageNotFound returns true if the error is caused
    62  // when an image is not found in the docker host.
    63  func IsErrImageNotFound(err error) bool {
    64  	return IsErrNotFound(err)
    65  }
    66  
    67  // containerNotFoundError implements an error returned when a container is not in the docker host.
    68  type containerNotFoundError struct {
    69  	containerID string
    70  }
    71  
    72  // NotFound indicates that this error type is of NotFound
    73  func (e containerNotFoundError) NotFound() bool {
    74  	return true
    75  }
    76  
    77  // Error returns a string representation of a containerNotFoundError
    78  func (e containerNotFoundError) Error() string {
    79  	return fmt.Sprintf("Error: No such container: %s", e.containerID)
    80  }
    81  
    82  // IsErrContainerNotFound returns true if the error is caused
    83  // when a container is not found in the docker host.
    84  func IsErrContainerNotFound(err error) bool {
    85  	return IsErrNotFound(err)
    86  }
    87  
    88  // networkNotFoundError implements an error returned when a network is not in the docker host.
    89  type networkNotFoundError struct {
    90  	networkID string
    91  }
    92  
    93  // NotFound indicates that this error type is of NotFound
    94  func (e networkNotFoundError) NotFound() bool {
    95  	return true
    96  }
    97  
    98  // Error returns a string representation of a networkNotFoundError
    99  func (e networkNotFoundError) Error() string {
   100  	return fmt.Sprintf("Error: No such network: %s", e.networkID)
   101  }
   102  
   103  // IsErrNetworkNotFound returns true if the error is caused
   104  // when a network is not found in the docker host.
   105  func IsErrNetworkNotFound(err error) bool {
   106  	return IsErrNotFound(err)
   107  }
   108  
   109  // volumeNotFoundError implements an error returned when a volume is not in the docker host.
   110  type volumeNotFoundError struct {
   111  	volumeID string
   112  }
   113  
   114  // NotFound indicates that this error type is of NotFound
   115  func (e volumeNotFoundError) NotFound() bool {
   116  	return true
   117  }
   118  
   119  // Error returns a string representation of a volumeNotFoundError
   120  func (e volumeNotFoundError) Error() string {
   121  	return fmt.Sprintf("Error: No such volume: %s", e.volumeID)
   122  }
   123  
   124  // IsErrVolumeNotFound returns true if the error is caused
   125  // when a volume is not found in the docker host.
   126  func IsErrVolumeNotFound(err error) bool {
   127  	return IsErrNotFound(err)
   128  }
   129  
   130  // unauthorizedError represents an authorization error in a remote registry.
   131  type unauthorizedError struct {
   132  	cause error
   133  }
   134  
   135  // Error returns a string representation of an unauthorizedError
   136  func (u unauthorizedError) Error() string {
   137  	return u.cause.Error()
   138  }
   139  
   140  // IsErrUnauthorized returns true if the error is caused
   141  // when a remote registry authentication fails
   142  func IsErrUnauthorized(err error) bool {
   143  	_, ok := err.(unauthorizedError)
   144  	return ok
   145  }
   146  
   147  // nodeNotFoundError implements an error returned when a node is not found.
   148  type nodeNotFoundError struct {
   149  	nodeID string
   150  }
   151  
   152  // Error returns a string representation of a nodeNotFoundError
   153  func (e nodeNotFoundError) Error() string {
   154  	return fmt.Sprintf("Error: No such node: %s", e.nodeID)
   155  }
   156  
   157  // NotFound indicates that this error type is of NotFound
   158  func (e nodeNotFoundError) NotFound() bool {
   159  	return true
   160  }
   161  
   162  // IsErrNodeNotFound returns true if the error is caused
   163  // when a node is not found.
   164  func IsErrNodeNotFound(err error) bool {
   165  	_, ok := err.(nodeNotFoundError)
   166  	return ok
   167  }
   168  
   169  // serviceNotFoundError implements an error returned when a service is not found.
   170  type serviceNotFoundError struct {
   171  	serviceID string
   172  }
   173  
   174  // Error returns a string representation of a serviceNotFoundError
   175  func (e serviceNotFoundError) Error() string {
   176  	return fmt.Sprintf("Error: No such service: %s", e.serviceID)
   177  }
   178  
   179  // NotFound indicates that this error type is of NotFound
   180  func (e serviceNotFoundError) NotFound() bool {
   181  	return true
   182  }
   183  
   184  // IsErrServiceNotFound returns true if the error is caused
   185  // when a service is not found.
   186  func IsErrServiceNotFound(err error) bool {
   187  	_, ok := err.(serviceNotFoundError)
   188  	return ok
   189  }
   190  
   191  // taskNotFoundError implements an error returned when a task is not found.
   192  type taskNotFoundError struct {
   193  	taskID string
   194  }
   195  
   196  // Error returns a string representation of a taskNotFoundError
   197  func (e taskNotFoundError) Error() string {
   198  	return fmt.Sprintf("Error: No such task: %s", e.taskID)
   199  }
   200  
   201  // NotFound indicates that this error type is of NotFound
   202  func (e taskNotFoundError) NotFound() bool {
   203  	return true
   204  }
   205  
   206  // IsErrTaskNotFound returns true if the error is caused
   207  // when a task is not found.
   208  func IsErrTaskNotFound(err error) bool {
   209  	_, ok := err.(taskNotFoundError)
   210  	return ok
   211  }
   212  
   213  type pluginPermissionDenied struct {
   214  	name string
   215  }
   216  
   217  func (e pluginPermissionDenied) Error() string {
   218  	return "Permission denied while installing plugin " + e.name
   219  }
   220  
   221  // IsErrPluginPermissionDenied returns true if the error is caused
   222  // when a user denies a plugin's permissions
   223  func IsErrPluginPermissionDenied(err error) bool {
   224  	_, ok := err.(pluginPermissionDenied)
   225  	return ok
   226  }
   227  
   228  // NewVersionError returns an error if the APIVersion required
   229  // if less than the current supported version
   230  func (cli *Client) NewVersionError(APIrequired, feature string) error {
   231  	if versions.LessThan(cli.version, APIrequired) {
   232  		return fmt.Errorf("%q requires API version %s, but the Docker server is version %s", feature, APIrequired, cli.version)
   233  	}
   234  	return nil
   235  }
   236  
   237  // secretNotFoundError implements an error returned when a secret is not found.
   238  type secretNotFoundError struct {
   239  	name string
   240  }
   241  
   242  // Error returns a string representation of a secretNotFoundError
   243  func (e secretNotFoundError) Error() string {
   244  	return fmt.Sprintf("Error: no such secret: %s", e.name)
   245  }
   246  
   247  // NoFound indicates that this error type is of NotFound
   248  func (e secretNotFoundError) NotFound() bool {
   249  	return true
   250  }
   251  
   252  // IsErrSecretNotFound returns true if the error is caused
   253  // when a secret is not found.
   254  func IsErrSecretNotFound(err error) bool {
   255  	_, ok := err.(secretNotFoundError)
   256  	return ok
   257  }
   258  
   259  // pluginNotFoundError implements an error returned when a plugin is not in the docker host.
   260  type pluginNotFoundError struct {
   261  	name string
   262  }
   263  
   264  // NotFound indicates that this error type is of NotFound
   265  func (e pluginNotFoundError) NotFound() bool {
   266  	return true
   267  }
   268  
   269  // Error returns a string representation of a pluginNotFoundError
   270  func (e pluginNotFoundError) Error() string {
   271  	return fmt.Sprintf("Error: No such plugin: %s", e.name)
   272  }
   273  
   274  // IsErrPluginNotFound returns true if the error is caused
   275  // when a plugin is not found in the docker host.
   276  func IsErrPluginNotFound(err error) bool {
   277  	return IsErrNotFound(err)
   278  }