github.com/google/cadvisor@v0.49.1/container/containerd/errdefs/errors.go (about)

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