From 488c9c36636a187686717a2462d21af0a56a569a Mon Sep 17 00:00:00 2001 From: amir gh Date: Mon, 11 Nov 2024 14:11:15 -0800 Subject: [PATCH] fix: use wrapError function instead of Join --- dns/resolver.go | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/dns/resolver.go b/dns/resolver.go index 98f9ca11..c35762e7 100644 --- a/dns/resolver.go +++ b/dns/resolver.go @@ -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 @@ -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) +}