is.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package 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. ErrAlreadyExists,
  17. ErrNotImplemented,
  18. ErrCancelled,
  19. ErrDeadline,
  20. ErrDataLoss,
  21. ErrUnknown:
  22. return e
  23. case causer:
  24. return getImplementer(e.Cause())
  25. default:
  26. return err
  27. }
  28. }
  29. // IsNotFound returns if the passed in error is an ErrNotFound
  30. func IsNotFound(err error) bool {
  31. _, ok := getImplementer(err).(ErrNotFound)
  32. return ok
  33. }
  34. // IsInvalidParameter returns if the passed in error is an ErrInvalidParameter
  35. func IsInvalidParameter(err error) bool {
  36. _, ok := getImplementer(err).(ErrInvalidParameter)
  37. return ok
  38. }
  39. // IsConflict returns if the passed in error is an ErrConflict
  40. func IsConflict(err error) bool {
  41. _, ok := getImplementer(err).(ErrConflict)
  42. return ok
  43. }
  44. // IsUnauthorized returns if the the passed in error is an ErrUnauthorized
  45. func IsUnauthorized(err error) bool {
  46. _, ok := getImplementer(err).(ErrUnauthorized)
  47. return ok
  48. }
  49. // IsUnavailable returns if the passed in error is an ErrUnavailable
  50. func IsUnavailable(err error) bool {
  51. _, ok := getImplementer(err).(ErrUnavailable)
  52. return ok
  53. }
  54. // IsForbidden returns if the passed in error is an ErrForbidden
  55. func IsForbidden(err error) bool {
  56. _, ok := getImplementer(err).(ErrForbidden)
  57. return ok
  58. }
  59. // IsSystem returns if the passed in error is an ErrSystem
  60. func IsSystem(err error) bool {
  61. _, ok := getImplementer(err).(ErrSystem)
  62. return ok
  63. }
  64. // IsNotModified returns if the passed in error is a NotModified error
  65. func IsNotModified(err error) bool {
  66. _, ok := getImplementer(err).(ErrNotModified)
  67. return ok
  68. }
  69. // IsAlreadyExists returns if the passed in error is a AlreadyExists error
  70. func IsAlreadyExists(err error) bool {
  71. _, ok := getImplementer(err).(ErrAlreadyExists)
  72. return ok
  73. }
  74. // IsNotImplemented returns if the passed in error is an ErrNotImplemented
  75. func IsNotImplemented(err error) bool {
  76. _, ok := getImplementer(err).(ErrNotImplemented)
  77. return ok
  78. }
  79. // IsUnknown returns if the passed in error is an ErrUnknown
  80. func IsUnknown(err error) bool {
  81. _, ok := getImplementer(err).(ErrUnknown)
  82. return ok
  83. }
  84. // IsCancelled returns if the passed in error is an ErrCancelled
  85. func IsCancelled(err error) bool {
  86. _, ok := getImplementer(err).(ErrCancelled)
  87. return ok
  88. }
  89. // IsDeadline returns if the passed in error is an ErrDeadline
  90. func IsDeadline(err error) bool {
  91. _, ok := getImplementer(err).(ErrDeadline)
  92. return ok
  93. }
  94. // IsDataLoss returns if the passed in error is an ErrDataLoss
  95. func IsDataLoss(err error) bool {
  96. _, ok := getImplementer(err).(ErrDataLoss)
  97. return ok
  98. }