github.com/dpiddy/docker@v1.12.2-rc1/volume/store/errors.go (about)

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