github.com/skanehira/moby@v17.12.1-ce-rc2+incompatible/volume/store/errors.go (about)

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