-
Notifications
You must be signed in to change notification settings - Fork 18.9k
errdefs: implement "IsXXX" utilities using containerd/errdefs #49378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
thaJeztah
wants to merge
2
commits into
moby:master
Choose a base branch
from
thaJeztah:c8d_errdefs
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+35
−65
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,123 +1,73 @@ | ||
| package errdefs | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| ) | ||
|
|
||
| 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 | ||
| } | ||
| } | ||
| import cerrdefs "github.com/containerd/errdefs" | ||
|
|
||
| // IsNotFound returns if the passed in error is an [ErrNotFound], | ||
| func IsNotFound(err error) bool { | ||
| _, ok := getImplementer(err).(ErrNotFound) | ||
| return ok | ||
| return cerrdefs.IsNotFound(err) | ||
| } | ||
|
|
||
| // IsInvalidParameter returns if the passed in error is an [ErrInvalidParameter]. | ||
| func IsInvalidParameter(err error) bool { | ||
| _, ok := getImplementer(err).(ErrInvalidParameter) | ||
| return ok | ||
| return cerrdefs.IsInvalidArgument(err) | ||
| } | ||
|
|
||
| // IsConflict returns if the passed in error is an [ErrConflict]. | ||
| func IsConflict(err error) bool { | ||
| _, ok := getImplementer(err).(ErrConflict) | ||
| return ok | ||
| return cerrdefs.IsConflict(err) | ||
| } | ||
|
|
||
| // IsUnauthorized returns if the passed in error is an [ErrUnauthorized]. | ||
| func IsUnauthorized(err error) bool { | ||
| _, ok := getImplementer(err).(ErrUnauthorized) | ||
| return ok | ||
| return cerrdefs.IsUnauthorized(err) | ||
| } | ||
|
|
||
| // IsUnavailable returns if the passed in error is an [ErrUnavailable]. | ||
| func IsUnavailable(err error) bool { | ||
| _, ok := getImplementer(err).(ErrUnavailable) | ||
| return ok | ||
| return cerrdefs.IsUnavailable(err) | ||
| } | ||
|
|
||
| // IsForbidden returns if the passed in error is an [ErrForbidden]. | ||
| func IsForbidden(err error) bool { | ||
| _, ok := getImplementer(err).(ErrForbidden) | ||
| return ok | ||
| return cerrdefs.IsPermissionDenied(err) | ||
| } | ||
|
|
||
| // IsSystem returns if the passed in error is an [ErrSystem]. | ||
| func IsSystem(err error) bool { | ||
| _, ok := getImplementer(err).(ErrSystem) | ||
| return ok | ||
| return cerrdefs.IsInternal(err) | ||
| } | ||
|
|
||
| // IsNotModified returns if the passed in error is an [ErrNotModified]. | ||
| func IsNotModified(err error) bool { | ||
| _, ok := getImplementer(err).(ErrNotModified) | ||
| return ok | ||
| return cerrdefs.IsNotModified(err) | ||
| } | ||
|
|
||
| // IsNotImplemented returns if the passed in error is an [ErrNotImplemented]. | ||
| func IsNotImplemented(err error) bool { | ||
| _, ok := getImplementer(err).(ErrNotImplemented) | ||
| return ok | ||
| return cerrdefs.IsNotImplemented(err) | ||
| } | ||
|
|
||
| // IsUnknown returns if the passed in error is an [ErrUnknown]. | ||
| func IsUnknown(err error) bool { | ||
| _, ok := getImplementer(err).(ErrUnknown) | ||
| return ok | ||
| return cerrdefs.IsUnknown(err) | ||
| } | ||
|
|
||
| // IsCancelled returns if the passed in error is an [ErrCancelled]. | ||
| func IsCancelled(err error) bool { | ||
| _, ok := getImplementer(err).(ErrCancelled) | ||
| return ok | ||
| return cerrdefs.IsCanceled(err) | ||
| } | ||
|
|
||
| // IsDeadline returns if the passed in error is an [ErrDeadline]. | ||
| func IsDeadline(err error) bool { | ||
| _, ok := getImplementer(err).(ErrDeadline) | ||
| return ok | ||
| return cerrdefs.IsDeadlineExceeded(err) | ||
| } | ||
|
|
||
| // IsDataLoss returns if the passed in error is an [ErrDataLoss]. | ||
| func IsDataLoss(err error) bool { | ||
| _, ok := getImplementer(err).(ErrDataLoss) | ||
| return ok | ||
| return cerrdefs.IsDataLoss(err) | ||
| } | ||
|
|
||
| // IsContext returns if the passed in error is due to context cancellation or deadline exceeded. | ||
| func IsContext(err error) bool { | ||
| return errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) | ||
| return cerrdefs.IsCanceled(err) || cerrdefs.IsDeadlineExceeded(err) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Confirmed; this test passes with our errdefs, but fails with containerd's implementation 🤔