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 errors.Wrap and error.Wrapf 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. 27package errdefs 28 29import ( 30 "context" 31 32 "github.com/pkg/errors" 33) 34 35// Definitions of common error types used throughout containerd. All containerd 36// errors returned by most packages will map into one of these errors classes. 37// Packages should return errors of these types when they want to instruct a 38// client to take a particular action. 39// 40// For the most part, we just try to provide local grpc errors. Most conditions 41// map very well to those defined by grpc. 42var ( 43 ErrUnknown = errors.New("unknown") // used internally to represent a missed mapping. 44 ErrInvalidArgument = errors.New("invalid argument") 45 ErrNotFound = errors.New("not found") 46 ErrAlreadyExists = errors.New("already exists") 47 ErrFailedPrecondition = errors.New("failed precondition") 48 ErrUnavailable = errors.New("unavailable") 49 ErrNotImplemented = errors.New("not implemented") // represents not supported and unimplemented 50) 51 52// IsInvalidArgument returns true if the error is due to an invalid argument 53func IsInvalidArgument(err error) bool { 54 return errors.Cause(err) == ErrInvalidArgument 55} 56 57// IsNotFound returns true if the error is due to a missing object 58func IsNotFound(err error) bool { 59 return errors.Cause(err) == ErrNotFound 60} 61 62// IsAlreadyExists returns true if the error is due to an already existing 63// metadata item 64func IsAlreadyExists(err error) bool { 65 return errors.Cause(err) == ErrAlreadyExists 66} 67 68// IsFailedPrecondition returns true if an operation could not proceed to the 69// lack of a particular condition 70func IsFailedPrecondition(err error) bool { 71 return errors.Cause(err) == ErrFailedPrecondition 72} 73 74// IsUnavailable returns true if the error is due to a resource being unavailable 75func IsUnavailable(err error) bool { 76 return errors.Cause(err) == ErrUnavailable 77} 78 79// IsNotImplemented returns true if the error is due to not being implemented 80func IsNotImplemented(err error) bool { 81 return errors.Cause(err) == ErrNotImplemented 82} 83 84// IsCanceled returns true if the error is due to `context.Canceled`. 85func IsCanceled(err error) bool { 86 return errors.Cause(err) == context.Canceled 87} 88 89// IsDeadlineExceeded returns true if the error is due to 90// `context.DeadlineExceeded`. 91func IsDeadlineExceeded(err error) bool { 92 return errors.Cause(err) == context.DeadlineExceeded 93} 94