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

     1  package daemon
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"syscall"
     7  
     8  	"github.com/docker/docker/errdefs"
     9  	"github.com/pkg/errors"
    10  	"google.golang.org/grpc"
    11  )
    12  
    13  func errNotRunning(id string) error {
    14  	return errdefs.Conflict(errors.Errorf("Container %s is not running", id))
    15  }
    16  
    17  func containerNotFound(id string) error {
    18  	return objNotFoundError{"container", id}
    19  }
    20  
    21  func volumeNotFound(id string) error {
    22  	return objNotFoundError{"volume", id}
    23  }
    24  
    25  type objNotFoundError struct {
    26  	object string
    27  	id     string
    28  }
    29  
    30  func (e objNotFoundError) Error() string {
    31  	return "No such " + e.object + ": " + e.id
    32  }
    33  
    34  func (e objNotFoundError) NotFound() {}
    35  
    36  func errContainerIsRestarting(containerID string) error {
    37  	cause := errors.Errorf("Container %s is restarting, wait until the container is running", containerID)
    38  	return errdefs.Conflict(cause)
    39  }
    40  
    41  func errExecNotFound(id string) error {
    42  	return objNotFoundError{"exec instance", id}
    43  }
    44  
    45  func errExecPaused(id string) error {
    46  	cause := errors.Errorf("Container %s is paused, unpause the container before exec", id)
    47  	return errdefs.Conflict(cause)
    48  }
    49  
    50  func errNotPaused(id string) error {
    51  	cause := errors.Errorf("Container %s is already paused", id)
    52  	return errdefs.Conflict(cause)
    53  }
    54  
    55  type nameConflictError struct {
    56  	id   string
    57  	name string
    58  }
    59  
    60  func (e nameConflictError) Error() string {
    61  	return fmt.Sprintf("Conflict. The container name %q is already in use by container %q. You have to remove (or rename) that container to be able to reuse that name.", e.name, e.id)
    62  }
    63  
    64  func (nameConflictError) Conflict() {}
    65  
    66  type containerNotModifiedError struct {
    67  	running bool
    68  }
    69  
    70  func (e containerNotModifiedError) Error() string {
    71  	if e.running {
    72  		return "Container is already started"
    73  	}
    74  	return "Container is already stopped"
    75  }
    76  
    77  func (e containerNotModifiedError) NotModified() {}
    78  
    79  type invalidIdentifier string
    80  
    81  func (e invalidIdentifier) Error() string {
    82  	return fmt.Sprintf("invalid name or ID supplied: %q", string(e))
    83  }
    84  
    85  func (invalidIdentifier) InvalidParameter() {}
    86  
    87  type duplicateMountPointError string
    88  
    89  func (e duplicateMountPointError) Error() string {
    90  	return "Duplicate mount point: " + string(e)
    91  }
    92  func (duplicateMountPointError) InvalidParameter() {}
    93  
    94  type containerFileNotFound struct {
    95  	file      string
    96  	container string
    97  }
    98  
    99  func (e containerFileNotFound) Error() string {
   100  	return "Could not find the file " + e.file + " in container " + e.container
   101  }
   102  
   103  func (containerFileNotFound) NotFound() {}
   104  
   105  type invalidFilter struct {
   106  	filter string
   107  	value  interface{}
   108  }
   109  
   110  func (e invalidFilter) Error() string {
   111  	msg := "Invalid filter '" + e.filter
   112  	if e.value != nil {
   113  		msg += fmt.Sprintf("=%s", e.value)
   114  	}
   115  	return msg + "'"
   116  }
   117  
   118  func (e invalidFilter) InvalidParameter() {}
   119  
   120  type startInvalidConfigError string
   121  
   122  func (e startInvalidConfigError) Error() string {
   123  	return string(e)
   124  }
   125  
   126  func (e startInvalidConfigError) InvalidParameter() {} // Is this right???
   127  
   128  func translateContainerdStartErr(cmd string, setExitCode func(int), err error) error {
   129  	errDesc := grpc.ErrorDesc(err)
   130  	contains := func(s1, s2 string) bool {
   131  		return strings.Contains(strings.ToLower(s1), s2)
   132  	}
   133  	var retErr = errdefs.Unknown(errors.New(errDesc))
   134  	// if we receive an internal error from the initial start of a container then lets
   135  	// return it instead of entering the restart loop
   136  	// set to 127 for container cmd not found/does not exist)
   137  	if contains(errDesc, cmd) &&
   138  		(contains(errDesc, "executable file not found") ||
   139  			contains(errDesc, "no such file or directory") ||
   140  			contains(errDesc, "system cannot find the file specified")) {
   141  		setExitCode(127)
   142  		retErr = startInvalidConfigError(errDesc)
   143  	}
   144  	// set to 126 for container cmd can't be invoked errors
   145  	if contains(errDesc, syscall.EACCES.Error()) {
   146  		setExitCode(126)
   147  		retErr = startInvalidConfigError(errDesc)
   148  	}
   149  
   150  	// attempted to mount a file onto a directory, or a directory onto a file, maybe from user specified bind mounts
   151  	if contains(errDesc, syscall.ENOTDIR.Error()) {
   152  		errDesc += ": Are you trying to mount a directory onto a file (or vice-versa)? Check if the specified host path exists and is the expected type"
   153  		setExitCode(127)
   154  		retErr = startInvalidConfigError(errDesc)
   155  	}
   156  
   157  	// TODO: it would be nice to get some better errors from containerd so we can return better errors here
   158  	return retErr
   159  }