Skip to content
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

Remove unwrapping in error Is methods #2269

Merged
merged 3 commits into from
Aug 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions agent/plugin/error.go
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While we're here, you could update Errors to be Unwrap that returns []error. I don't think any call sites of Errors use anything other than the Error method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package plugin

import (
"errors"
"fmt"
"sort"
"strings"
Expand All @@ -19,14 +18,14 @@ func (e *DeprecatedNameErrors) IsEmpty() bool {
return e == nil || len(e.errs) == 0
}

// Errors returns the contained set of errors in sorted order
func (e *DeprecatedNameErrors) Errors() []DeprecatedNameError {
// Unwrap returns the a slice of errors in a stable order
func (e *DeprecatedNameErrors) Unwrap() []error {
if e == nil {
return nil
}

if e.errs == nil {
return []DeprecatedNameError{}
if len(e.errs) == 0 {
return []error{}
}

errs := make([]DeprecatedNameError, 0, len(e.errs))
Expand All @@ -41,13 +40,18 @@ func (e *DeprecatedNameErrors) Errors() []DeprecatedNameError {
return errs[i].old < errs[j].old
})

return errs
out := make([]error, 0, len(errs))
for i := range errs {
out = append(out, &errs[i])
}
Comment on lines +43 to +46
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the alternative is to make errs have type []error, then type-assert everything in the sort.Slice closure...

Hang on, does anything use the order of e.errs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's nice for the user. I wrote tests for it, so they will break

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. Maybe I didn't 🤷‍♂️. Still think it's nice for the user.


return out
}

// Error returns each contained error on a new line
func (e *DeprecatedNameErrors) Error() string {
builder := strings.Builder{}
for i, err := range e.Errors() {
for i, err := range e.Unwrap() {
_, _ = builder.WriteString(err.Error())
if i < len(e.errs)-1 {
_, _ = builder.WriteRune('\n')
Expand All @@ -73,15 +77,11 @@ func (e *DeprecatedNameErrors) Append(errs ...DeprecatedNameError) *DeprecatedNa
return e
}

// Is returns true if and only if a error that is wrapped in target
// contains the same set of DeprecatedNameError as the receiver.
// Is returns true if and only if a target contains the same set of
// DeprecatedNameError as the receiver.
func (e *DeprecatedNameErrors) Is(target error) bool {
if e == nil {
return target == nil
}

var targetErr *DeprecatedNameErrors
if !errors.As(target, &targetErr) {
targetErr, ok := target.(*DeprecatedNameErrors)
if !ok {
return false
}

Expand Down Expand Up @@ -110,18 +110,10 @@ func NewDeprecatedNameError(oldName, newName string) DeprecatedNameError {
}

func (e *DeprecatedNameError) Error() string {
return fmt.Sprintf(" deprecated: %q\nreplacement: %q\n", e.old, e.new)
return fmt.Sprintf("deprecated: %q\nreplacement: %q\n", e.old, e.new)
}

func (e *DeprecatedNameError) Is(target error) bool {
if e == nil {
return target == nil
}

var targetErr *DeprecatedNameError
if !errors.As(target, &targetErr) {
return false
}

return e.old == targetErr.old && e.new == targetErr.new
terr, ok := target.(*DeprecatedNameError)
return ok && *e == *terr
}
2 changes: 1 addition & 1 deletion internal/job/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -869,7 +869,7 @@ func (e *Executor) executePluginHook(ctx context.Context, name string, checkouts
"You may be able to avoid this by removing consecutive underscore, hyphen, or whitespace",
"characters in your plugin configuration.",
}, " "))
for _, err := range dnerr.Errors() {
for _, err := range dnerr.Unwrap() {
e.shell.Logger.Printf("%s", err.Error())
}
} else if err != nil {
Expand Down