gopkg.in/docker/docker.v20@v20.10.27/volume/service/errors.go (about)

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