errors.go 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright The containerd Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package errdefs defines the common errors used throughout containerd
  14. // packages.
  15. //
  16. // Use with fmt.Errorf to add context to an error.
  17. //
  18. // To detect an error class, use the IsXXX functions to tell whether an error
  19. // is of a certain type.
  20. //
  21. // The functions ToGRPC and FromGRPC can be used to map server-side and
  22. // client-side errors to the correct types.
  23. package errdefs
  24. import (
  25. "context"
  26. "errors"
  27. )
  28. // Definitions of common error types used throughout containerd. All containerd
  29. // errors returned by most packages will map into one of these errors classes.
  30. // Packages should return errors of these types when they want to instruct a
  31. // client to take a particular action.
  32. //
  33. // For the most part, we just try to provide local grpc errors. Most conditions
  34. // map very well to those defined by grpc.
  35. var (
  36. ErrUnknown = errors.New("unknown") // used internally to represent a missed mapping.
  37. ErrInvalidArgument = errors.New("invalid argument")
  38. ErrNotFound = errors.New("not found")
  39. ErrAlreadyExists = errors.New("already exists")
  40. ErrFailedPrecondition = errors.New("failed precondition")
  41. ErrUnavailable = errors.New("unavailable")
  42. ErrNotImplemented = errors.New("not implemented") // represents not supported and unimplemented
  43. )
  44. // IsInvalidArgument returns true if the error is due to an invalid argument
  45. func IsInvalidArgument(err error) bool {
  46. return errors.Is(err, ErrInvalidArgument)
  47. }
  48. // IsNotFound returns true if the error is due to a missing object
  49. func IsNotFound(err error) bool {
  50. return errors.Is(err, ErrNotFound)
  51. }
  52. // IsAlreadyExists returns true if the error is due to an already existing
  53. // metadata item
  54. func IsAlreadyExists(err error) bool {
  55. return errors.Is(err, ErrAlreadyExists)
  56. }
  57. // IsFailedPrecondition returns true if an operation could not proceed to the
  58. // lack of a particular condition
  59. func IsFailedPrecondition(err error) bool {
  60. return errors.Is(err, ErrFailedPrecondition)
  61. }
  62. // IsUnavailable returns true if the error is due to a resource being unavailable
  63. func IsUnavailable(err error) bool {
  64. return errors.Is(err, ErrUnavailable)
  65. }
  66. // IsNotImplemented returns true if the error is due to not being implemented
  67. func IsNotImplemented(err error) bool {
  68. return errors.Is(err, ErrNotImplemented)
  69. }
  70. // IsCanceled returns true if the error is due to `context.Canceled`.
  71. func IsCanceled(err error) bool {
  72. return errors.Is(err, context.Canceled)
  73. }
  74. // IsDeadlineExceeded returns true if the error is due to
  75. // `context.DeadlineExceeded`.
  76. func IsDeadlineExceeded(err error) bool {
  77. return errors.Is(err, context.DeadlineExceeded)
  78. }