github.com/jingleWang/moby@v1.13.1/volume/store/errors.go (about)

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