is.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. ErrNotImplemented,
  17. ErrUnknown:
  18. return e
  19. case causer:
  20. return getImplementer(e.Cause())
  21. default:
  22. return err
  23. }
  24. }
  25. // IsNotFound returns if the passed in error is a ErrNotFound
  26. func IsNotFound(err error) bool {
  27. _, ok := getImplementer(err).(ErrNotFound)
  28. return ok
  29. }
  30. // IsInvalidParameter returns if the passed in error is an ErrInvalidParameter
  31. func IsInvalidParameter(err error) bool {
  32. _, ok := getImplementer(err).(ErrInvalidParameter)
  33. return ok
  34. }
  35. // IsConflict returns if the passed in error is a ErrConflict
  36. func IsConflict(err error) bool {
  37. _, ok := getImplementer(err).(ErrConflict)
  38. return ok
  39. }
  40. // IsUnauthorized returns if the the passed in error is an ErrUnauthorized
  41. func IsUnauthorized(err error) bool {
  42. _, ok := getImplementer(err).(ErrUnauthorized)
  43. return ok
  44. }
  45. // IsUnavailable returns if the passed in error is an ErrUnavailable
  46. func IsUnavailable(err error) bool {
  47. _, ok := getImplementer(err).(ErrUnavailable)
  48. return ok
  49. }
  50. // IsForbidden returns if the passed in error is a ErrForbidden
  51. func IsForbidden(err error) bool {
  52. _, ok := getImplementer(err).(ErrForbidden)
  53. return ok
  54. }
  55. // IsSystem returns if the passed in error is a ErrSystem
  56. func IsSystem(err error) bool {
  57. _, ok := getImplementer(err).(ErrSystem)
  58. return ok
  59. }
  60. // IsNotModified returns if the passed in error is a NotModified error
  61. func IsNotModified(err error) bool {
  62. _, ok := getImplementer(err).(ErrNotModified)
  63. return ok
  64. }
  65. // IsNotImplemented returns if the passed in error is a ErrNotImplemented
  66. func IsNotImplemented(err error) bool {
  67. _, ok := getImplementer(err).(ErrNotImplemented)
  68. return ok
  69. }
  70. // IsUnknown returns if the passed in error is an ErrUnknown
  71. func IsUnknown(err error) bool {
  72. _, ok := getImplementer(err).(ErrUnknown)
  73. return ok
  74. }