is.go 2.7 KB

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