github.com/moby/docker@v26.1.3+incompatible/errdefs/is.go (about)

     1  package errdefs
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  )
     7  
     8  type causer interface {
     9  	Cause() error
    10  }
    11  
    12  type wrapErr interface {
    13  	Unwrap() error
    14  }
    15  
    16  func getImplementer(err error) error {
    17  	switch e := err.(type) {
    18  	case
    19  		ErrNotFound,
    20  		ErrInvalidParameter,
    21  		ErrConflict,
    22  		ErrUnauthorized,
    23  		ErrUnavailable,
    24  		ErrForbidden,
    25  		ErrSystem,
    26  		ErrNotModified,
    27  		ErrNotImplemented,
    28  		ErrCancelled,
    29  		ErrDeadline,
    30  		ErrDataLoss,
    31  		ErrUnknown:
    32  		return err
    33  	case causer:
    34  		return getImplementer(e.Cause())
    35  	case wrapErr:
    36  		return getImplementer(e.Unwrap())
    37  	default:
    38  		return err
    39  	}
    40  }
    41  
    42  // IsNotFound returns if the passed in error is an ErrNotFound
    43  func IsNotFound(err error) bool {
    44  	_, ok := getImplementer(err).(ErrNotFound)
    45  	return ok
    46  }
    47  
    48  // IsInvalidParameter returns if the passed in error is an ErrInvalidParameter
    49  func IsInvalidParameter(err error) bool {
    50  	_, ok := getImplementer(err).(ErrInvalidParameter)
    51  	return ok
    52  }
    53  
    54  // IsConflict returns if the passed in error is an ErrConflict
    55  func IsConflict(err error) bool {
    56  	_, ok := getImplementer(err).(ErrConflict)
    57  	return ok
    58  }
    59  
    60  // IsUnauthorized returns if the passed in error is an ErrUnauthorized
    61  func IsUnauthorized(err error) bool {
    62  	_, ok := getImplementer(err).(ErrUnauthorized)
    63  	return ok
    64  }
    65  
    66  // IsUnavailable returns if the passed in error is an ErrUnavailable
    67  func IsUnavailable(err error) bool {
    68  	_, ok := getImplementer(err).(ErrUnavailable)
    69  	return ok
    70  }
    71  
    72  // IsForbidden returns if the passed in error is an ErrForbidden
    73  func IsForbidden(err error) bool {
    74  	_, ok := getImplementer(err).(ErrForbidden)
    75  	return ok
    76  }
    77  
    78  // IsSystem returns if the passed in error is an ErrSystem
    79  func IsSystem(err error) bool {
    80  	_, ok := getImplementer(err).(ErrSystem)
    81  	return ok
    82  }
    83  
    84  // IsNotModified returns if the passed in error is a NotModified error
    85  func IsNotModified(err error) bool {
    86  	_, ok := getImplementer(err).(ErrNotModified)
    87  	return ok
    88  }
    89  
    90  // IsNotImplemented returns if the passed in error is an ErrNotImplemented
    91  func IsNotImplemented(err error) bool {
    92  	_, ok := getImplementer(err).(ErrNotImplemented)
    93  	return ok
    94  }
    95  
    96  // IsUnknown returns if the passed in error is an ErrUnknown
    97  func IsUnknown(err error) bool {
    98  	_, ok := getImplementer(err).(ErrUnknown)
    99  	return ok
   100  }
   101  
   102  // IsCancelled returns if the passed in error is an ErrCancelled
   103  func IsCancelled(err error) bool {
   104  	_, ok := getImplementer(err).(ErrCancelled)
   105  	return ok
   106  }
   107  
   108  // IsDeadline returns if the passed in error is an ErrDeadline
   109  func IsDeadline(err error) bool {
   110  	_, ok := getImplementer(err).(ErrDeadline)
   111  	return ok
   112  }
   113  
   114  // IsDataLoss returns if the passed in error is an ErrDataLoss
   115  func IsDataLoss(err error) bool {
   116  	_, ok := getImplementer(err).(ErrDataLoss)
   117  	return ok
   118  }
   119  
   120  // IsContext returns if the passed in error is due to context cancellation or deadline exceeded.
   121  func IsContext(err error) bool {
   122  	return errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded)
   123  }