2018-03-22 17:11:03 -04:00
|
|
|
package service // import "github.com/docker/docker/volume/service"
|
2015-10-19 20:41:22 -04:00
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
import (
|
2018-03-22 17:11:03 -04:00
|
|
|
"fmt"
|
2015-09-23 16:29:14 -04:00
|
|
|
"strings"
|
|
|
|
)
|
2015-10-19 20:41:22 -04:00
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
const (
|
2015-10-19 20:41:22 -04:00
|
|
|
// errVolumeInUse is a typed error returned when trying to remove a volume that is currently in use by a container
|
2017-07-19 10:20:13 -04:00
|
|
|
errVolumeInUse conflictError = "volume is in use"
|
2015-10-19 20:41:22 -04:00
|
|
|
// errNoSuchVolume is a typed error returned if the requested volume doesn't exist in the volume store
|
2017-07-19 10:20:13 -04:00
|
|
|
errNoSuchVolume notFoundError = "no such volume"
|
2015-09-23 16:29:14 -04:00
|
|
|
// errNameConflict is a typed error returned on create when a volume exists with the given name, but for a different driver
|
2017-07-19 10:20:13 -04:00
|
|
|
errNameConflict conflictError = "volume name must be unique"
|
2015-10-19 20:41:22 -04:00
|
|
|
)
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
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() {}
|
|
|
|
|
2015-10-19 20:41:22 -04:00
|
|
|
// 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
|
2015-09-23 16:29:14 -04:00
|
|
|
// Refs is the list of references associated with the resource.
|
|
|
|
Refs []string
|
2015-10-19 20:41:22 -04:00
|
|
|
}
|
|
|
|
|
2015-12-13 11:00:39 -05:00
|
|
|
// Error satisfies the built-in error interface type.
|
2015-10-19 20:41:22 -04:00
|
|
|
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()
|
2015-09-23 16:29:14 -04:00
|
|
|
if len(e.Refs) > 0 {
|
|
|
|
s = s + " - " + "[" + strings.Join(e.Refs, ", ") + "]"
|
|
|
|
}
|
2015-10-19 20:41:22 -04:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
// Cause returns the error the caused this error
|
|
|
|
func (e *OpErr) Cause() error {
|
|
|
|
return e.Err
|
|
|
|
}
|
|
|
|
|
2015-10-19 20:41:22 -04:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2015-09-23 16:29:14 -04:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2017-07-19 10:20:13 -04:00
|
|
|
type causal interface {
|
|
|
|
Cause() error
|
|
|
|
}
|
|
|
|
|
2015-10-19 20:41:22 -04:00
|
|
|
func isErr(err error, expected error) bool {
|
|
|
|
switch pe := err.(type) {
|
|
|
|
case nil:
|
|
|
|
return false
|
2017-07-19 10:20:13 -04:00
|
|
|
case causal:
|
|
|
|
return isErr(pe.Cause(), expected)
|
2015-10-19 20:41:22 -04:00
|
|
|
}
|
|
|
|
return err == expected
|
|
|
|
}
|
2018-03-22 17:11:03 -04:00
|
|
|
|
|
|
|
type invalidFilter struct {
|
|
|
|
filter string
|
|
|
|
value interface{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e invalidFilter) Error() string {
|
|
|
|
msg := "Invalid filter '" + e.filter
|
|
|
|
if e.value != nil {
|
|
|
|
msg += fmt.Sprintf("=%s", e.value)
|
|
|
|
}
|
|
|
|
return msg + "'"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e invalidFilter) InvalidParameter() {}
|