is.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package errdefs // import "github.com/docker/docker/errdefs"
  2. type causer interface {
  3. Cause() error
  4. }
  5. func getImplementer(err error) error {
  6. switch e := err.(type) {
  7. case
  8. ErrNotFound,
  9. ErrInvalidParameter,
  10. ErrConflict,
  11. ErrUnauthorized,
  12. ErrUnavailable,
  13. ErrForbidden,
  14. ErrSystem,
  15. ErrNotModified,
  16. ErrNotImplemented,
  17. ErrCancelled,
  18. ErrDeadline,
  19. ErrDataLoss,
  20. ErrUnknown:
  21. return err
  22. case causer:
  23. return getImplementer(e.Cause())
  24. default:
  25. return err
  26. }
  27. }
  28. // IsNotFound returns if the passed in error is an ErrNotFound
  29. func IsNotFound(err error) bool {
  30. _, ok := getImplementer(err).(ErrNotFound)
  31. return ok
  32. }
  33. // IsInvalidParameter returns if the passed in error is an ErrInvalidParameter
  34. func IsInvalidParameter(err error) bool {
  35. _, ok := getImplementer(err).(ErrInvalidParameter)
  36. return ok
  37. }
  38. // IsConflict returns if the passed in error is an ErrConflict
  39. func IsConflict(err error) bool {
  40. _, ok := getImplementer(err).(ErrConflict)
  41. return ok
  42. }
  43. // IsUnauthorized returns if the passed in error is an ErrUnauthorized
  44. func IsUnauthorized(err error) bool {
  45. _, ok := getImplementer(err).(ErrUnauthorized)
  46. return ok
  47. }
  48. // IsUnavailable returns if the passed in error is an ErrUnavailable
  49. func IsUnavailable(err error) bool {
  50. _, ok := getImplementer(err).(ErrUnavailable)
  51. return ok
  52. }
  53. // IsForbidden returns if the passed in error is an ErrForbidden
  54. func IsForbidden(err error) bool {
  55. _, ok := getImplementer(err).(ErrForbidden)
  56. return ok
  57. }
  58. // IsSystem returns if the passed in error is an ErrSystem
  59. func IsSystem(err error) bool {
  60. _, ok := getImplementer(err).(ErrSystem)
  61. return ok
  62. }
  63. // IsNotModified returns if the passed in error is a NotModified error
  64. func IsNotModified(err error) bool {
  65. _, ok := getImplementer(err).(ErrNotModified)
  66. return ok
  67. }
  68. // IsNotImplemented returns if the passed in error is an ErrNotImplemented
  69. func IsNotImplemented(err error) bool {
  70. _, ok := getImplementer(err).(ErrNotImplemented)
  71. return ok
  72. }
  73. // IsUnknown returns if the passed in error is an ErrUnknown
  74. func IsUnknown(err error) bool {
  75. _, ok := getImplementer(err).(ErrUnknown)
  76. return ok
  77. }
  78. // IsCancelled returns if the passed in error is an ErrCancelled
  79. func IsCancelled(err error) bool {
  80. _, ok := getImplementer(err).(ErrCancelled)
  81. return ok
  82. }
  83. // IsDeadline returns if the passed in error is an ErrDeadline
  84. func IsDeadline(err error) bool {
  85. _, ok := getImplementer(err).(ErrDeadline)
  86. return ok
  87. }
  88. // IsDataLoss returns if the passed in error is an ErrDataLoss
  89. func IsDataLoss(err error) bool {
  90. _, ok := getImplementer(err).(ErrDataLoss)
  91. return ok
  92. }