helpers.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. package errdefs // import "github.com/docker/docker/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. if err == nil {
  59. return nil
  60. }
  61. return errUnavailable{err}
  62. }
  63. type errForbidden struct{ error }
  64. func (errForbidden) Forbidden() {}
  65. func (e errForbidden) Cause() error {
  66. return e.error
  67. }
  68. // Forbidden is a helper to create an error of the class with the same name from any error type
  69. func Forbidden(err error) error {
  70. if err == nil {
  71. return nil
  72. }
  73. return errForbidden{err}
  74. }
  75. type errSystem struct{ error }
  76. func (errSystem) System() {}
  77. func (e errSystem) Cause() error {
  78. return e.error
  79. }
  80. // System is a helper to create an error of the class with the same name from any error type
  81. func System(err error) error {
  82. if err == nil {
  83. return nil
  84. }
  85. return errSystem{err}
  86. }
  87. type errNotModified struct{ error }
  88. func (errNotModified) NotModified() {}
  89. func (e errNotModified) Cause() error {
  90. return e.error
  91. }
  92. // NotModified is a helper to create an error of the class with the same name from any error type
  93. func NotModified(err error) error {
  94. if err == nil {
  95. return nil
  96. }
  97. return errNotModified{err}
  98. }
  99. type errAlreadyExists struct{ error }
  100. func (errAlreadyExists) AlreadyExists() {}
  101. func (e errAlreadyExists) Cause() error {
  102. return e.error
  103. }
  104. // AlreadyExists is a helper to create an error of the class with the same name from any error type
  105. func AlreadyExists(err error) error {
  106. if err == nil {
  107. return nil
  108. }
  109. return errAlreadyExists{err}
  110. }
  111. type errNotImplemented struct{ error }
  112. func (errNotImplemented) NotImplemented() {}
  113. func (e errNotImplemented) Cause() error {
  114. return e.error
  115. }
  116. // NotImplemented is a helper to create an error of the class with the same name from any error type
  117. func NotImplemented(err error) error {
  118. if err == nil {
  119. return nil
  120. }
  121. return errNotImplemented{err}
  122. }
  123. type errUnknown struct{ error }
  124. func (errUnknown) Unknown() {}
  125. func (e errUnknown) Cause() error {
  126. return e.error
  127. }
  128. // Unknown is a helper to create an error of the class with the same name from any error type
  129. func Unknown(err error) error {
  130. if err == nil {
  131. return nil
  132. }
  133. return errUnknown{err}
  134. }
  135. type errCancelled struct{ error }
  136. func (errCancelled) Cancelled() {}
  137. func (e errCancelled) Cause() error {
  138. return e.error
  139. }
  140. // Cancelled is a helper to create an error of the class with the same name from any error type
  141. func Cancelled(err error) error {
  142. if err == nil {
  143. return nil
  144. }
  145. return errCancelled{err}
  146. }
  147. type errDeadline struct{ error }
  148. func (errDeadline) DeadlineExceeded() {}
  149. func (e errDeadline) Cause() error {
  150. return e.error
  151. }
  152. // Deadline is a helper to create an error of the class with the same name from any error type
  153. func Deadline(err error) error {
  154. if err == nil {
  155. return nil
  156. }
  157. return errDeadline{err}
  158. }
  159. type errDataLoss struct{ error }
  160. func (errDataLoss) DataLoss() {}
  161. func (e errDataLoss) Cause() error {
  162. return e.error
  163. }
  164. // DataLoss is a helper to create an error of the class with the same name from any error type
  165. func DataLoss(err error) error {
  166. if err == nil {
  167. return nil
  168. }
  169. return errDataLoss{err}
  170. }
  171. // FromContext returns the error class from the passed in context
  172. func FromContext(ctx context.Context) error {
  173. e := ctx.Err()
  174. if e == nil {
  175. return nil
  176. }
  177. if e == context.Canceled {
  178. return Cancelled(e)
  179. }
  180. if e == context.DeadlineExceeded {
  181. return Deadline(e)
  182. }
  183. return Unknown(e)
  184. }