-
Notifications
You must be signed in to change notification settings - Fork 2k
cli-plugins/manager: various fixes and deprecations #6237
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
Changes from all commits
54367b3
549d39a
1cc698c
7146021
d789bac
50963ac
5876b29
513ceee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -23,11 +23,6 @@ func (e *pluginError) Error() string { | |||||||||||
return e.cause.Error() | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Cause satisfies the errors.causer interface for pluginError. | ||||||||||||
func (e *pluginError) Cause() error { | ||||||||||||
return e.cause | ||||||||||||
} | ||||||||||||
|
||||||||||||
// Unwrap provides compatibility for Go 1.13 error chains. | ||||||||||||
func (e *pluginError) Unwrap() error { | ||||||||||||
return e.cause | ||||||||||||
|
@@ -41,14 +36,11 @@ func (e *pluginError) MarshalText() (text []byte, err error) { | |||||||||||
// wrapAsPluginError wraps an error in a pluginError with an | ||||||||||||
// additional message. | ||||||||||||
func wrapAsPluginError(err error, msg string) error { | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function
Suggested change
Copilot uses AI. Check for mistakes. Positive FeedbackNegative Feedback There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope; that's exactly the reason I don't want it; the caller should check if there's an error to wrap. Silently discarding such mistakes leads to subtle bugs. |
||||||||||||
if err == nil { | ||||||||||||
return nil | ||||||||||||
} | ||||||||||||
return &pluginError{cause: fmt.Errorf("%s: %w", msg, err)} | ||||||||||||
} | ||||||||||||
|
||||||||||||
// NewPluginError creates a new pluginError, analogous to | ||||||||||||
// newPluginError creates a new pluginError, analogous to | ||||||||||||
// errors.Errorf. | ||||||||||||
func NewPluginError(msg string, args ...any) error { | ||||||||||||
func newPluginError(msg string, args ...any) error { | ||||||||||||
return &pluginError{cause: fmt.Errorf(msg, args...)} | ||||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package manager | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"testing" | ||
|
||
"gotest.tools/v3/assert" | ||
is "gotest.tools/v3/assert/cmp" | ||
) | ||
|
||
func TestPluginMarshal(t *testing.T) { | ||
const jsonWithError = `{"Name":"some-plugin","Err":"something went wrong"}` | ||
const jsonNoError = `{"Name":"some-plugin"}` | ||
|
||
tests := []struct { | ||
doc string | ||
error error | ||
expected string | ||
}{ | ||
{ | ||
doc: "no error", | ||
expected: jsonNoError, | ||
}, | ||
{ | ||
doc: "regular error", | ||
error: errors.New("something went wrong"), | ||
expected: jsonWithError, | ||
}, | ||
{ | ||
doc: "custom error", | ||
error: newPluginError("something went wrong"), | ||
expected: jsonWithError, | ||
}, | ||
} | ||
for _, tc := range tests { | ||
t.Run(tc.doc, func(t *testing.T) { | ||
actual, err := json.Marshal(&Plugin{Name: "some-plugin", Err: tc.error}) | ||
assert.NilError(t, err) | ||
assert.Check(t, is.Equal(string(actual), tc.expected)) | ||
}) | ||
} | ||
} |
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.
The function
wrapAsPluginError
no longer checks for nil errors, but the function body will panic iferr
is nil when callingfmt.Errorf("%s: %w", msg, err)
. Either add a nil check or ensure all callers handle nil errors before calling this function.Copilot uses AI. Check for mistakes.
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.
I don't see how it would panic. But sure, let me add a test-case to make sure it doesn't