helpers.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. package errdefs
  2. import "context"
  3. type errNotFound struct{ error }
  4. func (errNotFound) NotFound() {}
  5. func (e errNotFound) Cause() error {
  6. return e.error
  7. }
  8. // NotFound is a helper to create an error of the class with the same name from any error type
  9. func NotFound(err error) error {
  10. if err == nil {
  11. return nil
  12. }
  13. return errNotFound{err}
  14. }
  15. type errInvalidParameter struct{ error }
  16. func (errInvalidParameter) InvalidParameter() {}
  17. func (e errInvalidParameter) Cause() error {
  18. return e.error
  19. }
  20. // InvalidParameter is a helper to create an error of the class with the same name from any error type
  21. func InvalidParameter(err error) error {
  22. if err == nil {
  23. return nil
  24. }
  25. return errInvalidParameter{err}
  26. }
  27. type errConflict struct{ error }
  28. func (errConflict) Conflict() {}
  29. func (e errConflict) Cause() error {
  30. return e.error
  31. }
  32. // Conflict is a helper to create an error of the class with the same name from any error type
  33. func Conflict(err error) error {
  34. if err == nil {
  35. return nil
  36. }
  37. return errConflict{err}
  38. }
  39. type errUnauthorized struct{ error }
  40. func (errUnauthorized) Unauthorized() {}
  41. func (e errUnauthorized) Cause() error {
  42. return e.error
  43. }
  44. // Unauthorized is a helper to create an error of the class with the same name from any error type
  45. func Unauthorized(err error) error {
  46. if err == nil {
  47. return nil
  48. }
  49. return errUnauthorized{err}
  50. }
  51. type errUnavailable struct{ error }
  52. func (errUnavailable) Unavailable() {}
  53. func (e errUnavailable) Cause() error {
  54. return e.error
  55. }
  56. // Unavailable is a helper to create an error of the class with the same name from any error type
  57. func Unavailable(err error) error {
  58. return errUnavailable{err}
  59. }
  60. type errForbidden struct{ error }
  61. func (errForbidden) Forbidden() {}
  62. func (e errForbidden) Cause() error {
  63. return e.error
  64. }
  65. // Forbidden is a helper to create an error of the class with the same name from any error type
  66. func Forbidden(err error) error {
  67. if err == nil {
  68. return nil
  69. }
  70. return errForbidden{err}
  71. }
  72. type errSystem struct{ error }
  73. func (errSystem) System() {}
  74. func (e errSystem) Cause() error {
  75. return e.error
  76. }
  77. // System is a helper to create an error of the class with the same name from any error type
  78. func System(err error) error {
  79. if err == nil {
  80. return nil
  81. }
  82. return errSystem{err}
  83. }
  84. type errNotModified struct{ error }
  85. func (errNotModified) NotModified() {}
  86. func (e errNotModified) Cause() error {
  87. return e.error
  88. }
  89. // NotModified is a helper to create an error of the class with the same name from any error type
  90. func NotModified(err error) error {
  91. if err == nil {
  92. return nil
  93. }
  94. return errNotModified{err}
  95. }
  96. type errAlreadyExists struct{ error }
  97. func (errAlreadyExists) AlreadyExists() {}
  98. func (e errAlreadyExists) Cause() error {
  99. return e.error
  100. }
  101. // AlreadyExists is a helper to create an error of the class with the same name from any error type
  102. func AlreadyExists(err error) error {
  103. if err == nil {
  104. return nil
  105. }
  106. return errAlreadyExists{err}
  107. }
  108. type errNotImplemented struct{ error }
  109. func (errNotImplemented) NotImplemented() {}
  110. func (e errNotImplemented) Cause() error {
  111. return e.error
  112. }
  113. // NotImplemented is a helper to create an error of the class with the same name from any error type
  114. func NotImplemented(err error) error {
  115. if err == nil {
  116. return nil
  117. }
  118. return errNotImplemented{err}
  119. }
  120. type errUnknown struct{ error }
  121. func (errUnknown) Unknown() {}
  122. func (e errUnknown) Cause() error {
  123. return e.error
  124. }
  125. // Unknown is a helper to create an error of the class with the same name from any error type
  126. func Unknown(err error) error {
  127. if err == nil {
  128. return nil
  129. }
  130. return errUnknown{err}
  131. }
  132. type errCancelled struct{ error }
  133. func (errCancelled) Cancelled() {}
  134. func (e errCancelled) Cause() error {
  135. return e.error
  136. }
  137. // Cancelled is a helper to create an error of the class with the same name from any error type
  138. func Cancelled(err error) error {
  139. if err == nil {
  140. return nil
  141. }
  142. return errCancelled{err}
  143. }
  144. type errDeadline struct{ error }
  145. func (errDeadline) DeadlineExceeded() {}
  146. func (e errDeadline) Cause() error {
  147. return e.error
  148. }
  149. // Deadline is a helper to create an error of the class with the same name from any error type
  150. func Deadline(err error) error {
  151. if err == nil {
  152. return nil
  153. }
  154. return errDeadline{err}
  155. }
  156. type errDataLoss struct{ error }
  157. func (errDataLoss) DataLoss() {}
  158. func (e errDataLoss) Cause() error {
  159. return e.error
  160. }
  161. // DataLoss is a helper to create an error of the class with the same name from any error type
  162. func DataLoss(err error) error {
  163. if err == nil {
  164. return nil
  165. }
  166. return errDataLoss{err}
  167. }
  168. // FromContext returns the error class from the passed in context
  169. func FromContext(ctx context.Context) error {
  170. e := ctx.Err()
  171. if e == nil {
  172. return nil
  173. }
  174. if e == context.Canceled {
  175. return Cancelled(e)
  176. }
  177. if e == context.DeadlineExceeded {
  178. return Deadline(e)
  179. }
  180. return Unknown(e)
  181. }