errors.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package store
  2. import (
  3. "strings"
  4. "github.com/pkg/errors"
  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("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. err = errors.Cause(err)
  59. switch pe := err.(type) {
  60. case nil:
  61. return false
  62. case *OpErr:
  63. err = errors.Cause(pe.Err)
  64. }
  65. return err == expected
  66. }