errors.go 2.2 KB

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