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

fix: use wrapError function instead of Join #328

Closed
wants to merge 1 commit into from
Closed
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
18 changes: 15 additions & 3 deletions dns/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,16 +186,16 @@ func queryDatagram(conn io.ReadWriter, q dnsmessage.Question) (*dnsmessage.Messa
err = nil
}
if err != nil {
return nil, &nestedError{ErrReceive, errors.Join(returnErr, fmt.Errorf("read message failed: %w", err))}
return nil, &nestedError{ErrReceive, wrapError(returnErr, fmt.Errorf("read message failed: %w", err))}
Copy link
Contributor

Choose a reason for hiding this comment

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

The semantic here is broken.
These are not nested errors. They are sibling.
You are instead saying that the error is the last one, with unrelated sub-errors.

We need to define what semantic we want to represent in the UDP test.
Perhaps we only look at the first error? Call Unwrap() []error instead, extract the first one, then do the usual error handling.

If you need to look at the other errors, you need to figure out what that means. Still, that can live in the test code.

}
var msg dnsmessage.Message
if err := msg.Unpack(buf[:n]); err != nil {
returnErr = errors.Join(returnErr, err)
returnErr = wrapError(returnErr, err)
// Ignore invalid packets that fail to parse. It could be injected.
continue
}
if err := checkResponse(id, q, msg.Header, msg.Questions); err != nil {
returnErr = errors.Join(returnErr, err)
returnErr = wrapError(returnErr, err)
continue
}
return &msg, nil
Expand Down Expand Up @@ -391,3 +391,15 @@ func NewHTTPSResolver(sd transport.StreamDialer, resolverAddr string, url string
return &msg, nil
})
}

// wrapError creates a new error that wraps an existing error chain with a new error,
// preserving the ability to unwrap through the entire chain.
func wrapError(base error, wrap error) error {
if base == nil {
return wrap
}
if wrap == nil {
return base
}
return fmt.Errorf("%w: %w", base, wrap)
}
Loading