github.com/walkingsparrow/docker@v1.4.2-0.20151218153551-b708a2249bfa/volume/store/errors.go (about)

     1  package store
     2  
     3  import "errors"
     4  
     5  var (
     6  	// errVolumeInUse is a typed error returned when trying to remove a volume that is currently in use by a container
     7  	errVolumeInUse = errors.New("volume is in use")
     8  	// errNoSuchVolume is a typed error returned if the requested volume doesn't exist in the volume store
     9  	errNoSuchVolume = errors.New("no such volume")
    10  	// errInvalidName is a typed error returned when creating a volume with a name that is not valid on the platform
    11  	errInvalidName = errors.New("volume name is not valid on this platform")
    12  )
    13  
    14  // OpErr is the error type returned by functions in the store package. It describes
    15  // the operation, volume name, and error.
    16  type OpErr struct {
    17  	// Err is the error that occurred during the operation.
    18  	Err error
    19  	// Op is the operation which caused the error, such as "create", or "list".
    20  	Op string
    21  	// Name is the name of the resource being requested for this op, typically the volume name or the driver name.
    22  	Name string
    23  }
    24  
    25  // Error satisfies the built-in error interface type.
    26  func (e *OpErr) Error() string {
    27  	if e == nil {
    28  		return "<nil>"
    29  	}
    30  	s := e.Op
    31  	if e.Name != "" {
    32  		s = s + " " + e.Name
    33  	}
    34  
    35  	s = s + ": " + e.Err.Error()
    36  	return s
    37  }
    38  
    39  // IsInUse returns a boolean indicating whether the error indicates that a
    40  // volume is in use
    41  func IsInUse(err error) bool {
    42  	return isErr(err, errVolumeInUse)
    43  }
    44  
    45  // IsNotExist returns a boolean indicating whether the error indicates that the volume does not exist
    46  func IsNotExist(err error) bool {
    47  	return isErr(err, errNoSuchVolume)
    48  }
    49  
    50  func isErr(err error, expected error) bool {
    51  	switch pe := err.(type) {
    52  	case nil:
    53  		return false
    54  	case *OpErr:
    55  		err = pe.Err
    56  	}
    57  	return err == expected
    58  }