is.go 2.8 KB

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