Skip to content

Commit

Permalink
fix: use wrapError function instead of Join
Browse files Browse the repository at this point in the history
  • Loading branch information
amircybersec committed Nov 11, 2024
1 parent 8f91506 commit 488c9c3
Showing 1 changed file with 15 additions and 3 deletions.
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))}
}
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)
}

0 comments on commit 488c9c3

Please sign in to comment.