|
| 1 | +/* |
| 2 | + Copyright The containerd Authors. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package errdefs defines the common errors used throughout containerd |
| 18 | +// packages. |
| 19 | +// |
| 20 | +// Use with fmt.Errorf to add context to an error. |
| 21 | +// |
| 22 | +// To detect an error class, use the IsXXX functions to tell whether an error |
| 23 | +// is of a certain type. |
| 24 | +// |
| 25 | +// The functions ToGRPC and FromGRPC can be used to map server-side and |
| 26 | +// client-side errors to the correct types. |
| 27 | +package errdefs |
| 28 | + |
| 29 | +import ( |
| 30 | + "github.com/containerd/errdefs" |
| 31 | +) |
| 32 | + |
| 33 | +// Definitions of common error types used throughout containerd. All containerd |
| 34 | +// errors returned by most packages will map into one of these errors classes. |
| 35 | +// Packages should return errors of these types when they want to instruct a |
| 36 | +// client to take a particular action. |
| 37 | +// |
| 38 | +// For the most part, we just try to provide local grpc errors. Most conditions |
| 39 | +// map very well to those defined by grpc. |
| 40 | +var ( |
| 41 | + ErrUnknown = errdefs.ErrUnknown |
| 42 | + ErrInvalidArgument = errdefs.ErrInvalidArgument |
| 43 | + ErrNotFound = errdefs.ErrNotFound |
| 44 | + ErrAlreadyExists = errdefs.ErrAlreadyExists |
| 45 | + ErrFailedPrecondition = errdefs.ErrFailedPrecondition |
| 46 | + ErrUnavailable = errdefs.ErrUnavailable |
| 47 | + ErrNotImplemented = errdefs.ErrNotImplemented |
| 48 | +) |
| 49 | + |
| 50 | +// IsInvalidArgument returns true if the error is due to an invalid argument |
| 51 | +func IsInvalidArgument(err error) bool { |
| 52 | + return errdefs.IsInvalidArgument(err) |
| 53 | +} |
| 54 | + |
| 55 | +// IsNotFound returns true if the error is due to a missing object |
| 56 | +func IsNotFound(err error) bool { |
| 57 | + return errdefs.IsNotFound(err) |
| 58 | +} |
| 59 | + |
| 60 | +// IsAlreadyExists returns true if the error is due to an already existing |
| 61 | +// metadata item |
| 62 | +func IsAlreadyExists(err error) bool { |
| 63 | + return errdefs.IsAlreadyExists(err) |
| 64 | +} |
| 65 | + |
| 66 | +// IsFailedPrecondition returns true if an operation could not proceed to the |
| 67 | +// lack of a particular condition |
| 68 | +func IsFailedPrecondition(err error) bool { |
| 69 | + return errdefs.IsFailedPrecondition(err) |
| 70 | +} |
| 71 | + |
| 72 | +// IsUnavailable returns true if the error is due to a resource being unavailable |
| 73 | +func IsUnavailable(err error) bool { |
| 74 | + return errdefs.IsUnavailable(err) |
| 75 | +} |
| 76 | + |
| 77 | +// IsNotImplemented returns true if the error is due to not being implemented |
| 78 | +func IsNotImplemented(err error) bool { |
| 79 | + return errdefs.IsNotImplemented(err) |
| 80 | +} |
| 81 | + |
| 82 | +// IsCanceled returns true if the error is due to `context.Canceled`. |
| 83 | +func IsCanceled(err error) bool { |
| 84 | + return errdefs.IsCanceled(err) |
| 85 | +} |
| 86 | + |
| 87 | +// IsDeadlineExceeded returns true if the error is due to |
| 88 | +// `context.DeadlineExceeded`. |
| 89 | +func IsDeadlineExceeded(err error) bool { |
| 90 | + return errdefs.IsDeadlineExceeded(err) |
| 91 | +} |
| 92 | + |
| 93 | +// ToGRPC will attempt to map the backend containerd error into a grpc error, |
| 94 | +// using the original error message as a description. |
| 95 | +// |
| 96 | +// Further information may be extracted from certain errors depending on their |
| 97 | +// type. |
| 98 | +// |
| 99 | +// If the error is unmapped, the original error will be returned to be handled |
| 100 | +// by the regular grpc error handling stack. |
| 101 | +func ToGRPC(err error) error { |
| 102 | + return errdefs.ToGRPC(err) |
| 103 | +} |
| 104 | + |
| 105 | +// ToGRPCf maps the error to grpc error codes, assembling the formatting string |
| 106 | +// and combining it with the target error string. |
| 107 | +// |
| 108 | +// This is equivalent to errdefs.ToGRPC(fmt.Errorf("%s: %w", fmt.Sprintf(format, args...), err)) |
| 109 | +func ToGRPCf(err error, format string, args ...interface{}) error { |
| 110 | + return errdefs.ToGRPCf(err, format, args...) |
| 111 | +} |
| 112 | + |
| 113 | +// FromGRPC returns the underlying error from a grpc service based on the grpc error code |
| 114 | +func FromGRPC(err error) error { |
| 115 | + return errdefs.FromGRPC(err) |
| 116 | +} |
0 commit comments