volume/store/errors.go
43012fe8
 package store
 
d3eca445
 import (
 	"strings"
 )
43012fe8
 
ebcb7d6b
 const (
43012fe8
 	// errVolumeInUse is a typed error returned when trying to remove a volume that is currently in use by a container
ebcb7d6b
 	errVolumeInUse conflictError = "volume is in use"
43012fe8
 	// errNoSuchVolume is a typed error returned if the requested volume doesn't exist in the volume store
ebcb7d6b
 	errNoSuchVolume notFoundError = "no such volume"
d3eca445
 	// errNameConflict is a typed error returned on create when a volume exists with the given name, but for a different driver
ebcb7d6b
 	errNameConflict conflictError = "volume name must be unique"
43012fe8
 )
 
ebcb7d6b
 type conflictError string
 
 func (e conflictError) Error() string {
 	return string(e)
 }
 func (conflictError) Conflict() {}
 
 type notFoundError string
 
 func (e notFoundError) Error() string {
 	return string(e)
 }
 
 func (notFoundError) NotFound() {}
 
43012fe8
 // OpErr is the error type returned by functions in the store package. It describes
 // the operation, volume name, and error.
 type OpErr struct {
 	// Err is the error that occurred during the operation.
 	Err error
 	// Op is the operation which caused the error, such as "create", or "list".
 	Op string
 	// Name is the name of the resource being requested for this op, typically the volume name or the driver name.
 	Name string
d3eca445
 	// Refs is the list of references associated with the resource.
 	Refs []string
43012fe8
 }
 
927b334e
 // Error satisfies the built-in error interface type.
43012fe8
 func (e *OpErr) Error() string {
 	if e == nil {
 		return "<nil>"
 	}
 	s := e.Op
 	if e.Name != "" {
 		s = s + " " + e.Name
 	}
 
 	s = s + ": " + e.Err.Error()
d3eca445
 	if len(e.Refs) > 0 {
 		s = s + " - " + "[" + strings.Join(e.Refs, ", ") + "]"
 	}
43012fe8
 	return s
 }
 
ebcb7d6b
 // Cause returns the error the caused this error
 func (e *OpErr) Cause() error {
 	return e.Err
 }
 
43012fe8
 // IsInUse returns a boolean indicating whether the error indicates that a
 // volume is in use
 func IsInUse(err error) bool {
 	return isErr(err, errVolumeInUse)
 }
 
 // IsNotExist returns a boolean indicating whether the error indicates that the volume does not exist
 func IsNotExist(err error) bool {
 	return isErr(err, errNoSuchVolume)
 }
 
d3eca445
 // IsNameConflict returns a boolean indicating whether the error indicates that a
 // volume name is already taken
 func IsNameConflict(err error) bool {
 	return isErr(err, errNameConflict)
 }
 
ebcb7d6b
 type causal interface {
 	Cause() error
 }
 
43012fe8
 func isErr(err error, expected error) bool {
 	switch pe := err.(type) {
 	case nil:
 		return false
ebcb7d6b
 	case causal:
 		return isErr(pe.Cause(), expected)
43012fe8
 	}
 	return err == expected
 }