Use containerd errdefs for error checks

Signed-off-by: Derek McGowan <derek@mcg.dev>
This commit is contained in:
Derek McGowan
2024-05-29 17:07:14 -07:00
parent 2c5f8c51c5
commit f1bb44aeee

View File

@@ -3,119 +3,74 @@ package errdefs
import ( import (
"context" "context"
"errors" "errors"
cerrdefs "github.com/containerd/errdefs"
) )
type causer interface {
Cause() error
}
type wrapErr interface {
Unwrap() error
}
func getImplementer(err error) error {
switch e := err.(type) {
case
ErrNotFound,
ErrInvalidParameter,
ErrConflict,
ErrUnauthorized,
ErrUnavailable,
ErrForbidden,
ErrSystem,
ErrNotModified,
ErrNotImplemented,
ErrCancelled,
ErrDeadline,
ErrDataLoss,
ErrUnknown:
return err
case causer:
return getImplementer(e.Cause())
case wrapErr:
return getImplementer(e.Unwrap())
default:
return err
}
}
// IsNotFound returns if the passed in error is an [ErrNotFound], // IsNotFound returns if the passed in error is an [ErrNotFound],
func IsNotFound(err error) bool { //
_, ok := getImplementer(err).(ErrNotFound) // Deprecated: use containerd [cerrdefs.IsNotFound]
return ok var IsNotFound = cerrdefs.IsNotFound
}
// IsInvalidParameter returns if the passed in error is an [ErrInvalidParameter]. // IsInvalidParameter returns if the passed in error is an [ErrInvalidParameter].
func IsInvalidParameter(err error) bool { //
_, ok := getImplementer(err).(ErrInvalidParameter) // Deprecated: use containerd [cerrdefs.IsInvalidArgument]
return ok var IsInvalidParameter = cerrdefs.IsInvalidArgument
}
// IsConflict returns if the passed in error is an [ErrConflict]. // IsConflict returns if the passed in error is an [ErrConflict].
func IsConflict(err error) bool { //
_, ok := getImplementer(err).(ErrConflict) // Deprecated: use containerd [cerrdefs.IsConflict]
return ok var IsConflict = cerrdefs.IsConflict
}
// IsUnauthorized returns if the passed in error is an [ErrUnauthorized]. // IsUnauthorized returns if the passed in error is an [ErrUnauthorized].
func IsUnauthorized(err error) bool { //
_, ok := getImplementer(err).(ErrUnauthorized) // Deprecated: use containerd [cerrdefs.IsUnauthorized]
return ok var IsUnauthorized = cerrdefs.IsUnauthorized
}
// IsUnavailable returns if the passed in error is an [ErrUnavailable]. // IsUnavailable returns if the passed in error is an [ErrUnavailable].
func IsUnavailable(err error) bool { //
_, ok := getImplementer(err).(ErrUnavailable) // Deprecated: use containerd [cerrdefs.IsUnavailable]
return ok var IsUnavailable = cerrdefs.IsUnavailable
}
// IsForbidden returns if the passed in error is an [ErrForbidden]. // IsForbidden returns if the passed in error is an [ErrForbidden].
func IsForbidden(err error) bool { //
_, ok := getImplementer(err).(ErrForbidden) // Deprecated: use containerd [cerrdefs.IsPermissionDenied]
return ok var IsForbidden = cerrdefs.IsPermissionDenied
}
// IsSystem returns if the passed in error is an [ErrSystem]. // IsSystem returns if the passed in error is an [ErrSystem].
func IsSystem(err error) bool { //
_, ok := getImplementer(err).(ErrSystem) // Deprecated: use containerd [cerrdefs.IsInternal]
return ok var IsSystem = cerrdefs.IsInternal
}
// IsNotModified returns if the passed in error is an [ErrNotModified]. // IsNotModified returns if the passed in error is an [ErrNotModified].
func IsNotModified(err error) bool { //
_, ok := getImplementer(err).(ErrNotModified) // Deprecated: use containerd [cerrdefs.IsNotModified]
return ok var IsNotModified = cerrdefs.IsNotModified
}
// IsNotImplemented returns if the passed in error is an [ErrNotImplemented]. // IsNotImplemented returns if the passed in error is an [ErrNotImplemented].
func IsNotImplemented(err error) bool { //
_, ok := getImplementer(err).(ErrNotImplemented) // Deprecated: use containerd [cerrdefs.IsNotImplemented]
return ok var IsNotImplemented = cerrdefs.IsNotImplemented
}
// IsUnknown returns if the passed in error is an [ErrUnknown]. // IsUnknown returns if the passed in error is an [ErrUnknown].
func IsUnknown(err error) bool { //
_, ok := getImplementer(err).(ErrUnknown) // Deprecated: use containerd [cerrdefs.IsUnknown]
return ok var IsUnknown = cerrdefs.IsUnknown
}
// IsCancelled returns if the passed in error is an [ErrCancelled]. // IsCancelled returns if the passed in error is an [ErrCancelled].
func IsCancelled(err error) bool { //
_, ok := getImplementer(err).(ErrCancelled) // Deprecated: use containerd [cerrdefs.IsCanceled]
return ok var IsCancelled = cerrdefs.IsCanceled
}
// IsDeadline returns if the passed in error is an [ErrDeadline]. // IsDeadline returns if the passed in error is an [ErrDeadline].
func IsDeadline(err error) bool { //
_, ok := getImplementer(err).(ErrDeadline) // Deprecated: use containerd [cerrdefs.IsDeadlineExceeded]
return ok var IsDeadline = cerrdefs.IsDeadlineExceeded
}
// IsDataLoss returns if the passed in error is an [ErrDataLoss]. // IsDataLoss returns if the passed in error is an [ErrDataLoss].
func IsDataLoss(err error) bool { //
_, ok := getImplementer(err).(ErrDataLoss) // Deprecated: use containerd [cerrdefs.IsDataLoss]
return ok var IsDataLoss = cerrdefs.IsDataLoss
}
// IsContext returns if the passed in error is due to context cancellation or deadline exceeded. // IsContext returns if the passed in error is due to context cancellation or deadline exceeded.
func IsContext(err error) bool { func IsContext(err error) bool {