Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 42b87bb

Browse files
committed
Properly report DNSLink errors
Only report that there is no DNSLink for a name when there are no DNSLink TXT records available for that name. For all other errors, such as being offline, report the more general "cannot resolve name" error.
1 parent 3d90782 commit 42b87bb

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

base.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ func resolve(ctx context.Context, r resolver, name string, options opts.ResolveO
3737
}
3838
}
3939

40-
if err == ErrResolveFailed {
41-
i := len(name) - 1
42-
for i >= 0 && name[i] != '/' {
43-
i--
44-
}
45-
// Wrap error so that it can be tested if it is a ErrResolveFailed
46-
err = fmt.Errorf("%w: %q is missing a DNSLink record (https://docs.ipfs.io/concepts/dnslink/)", ErrResolveFailed, name[i+1:])
47-
}
4840
return p, err
4941
}
5042

dns.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
8888

8989
go func() {
9090
defer close(out)
91+
var rootResErr, subResErr error
9192
for {
9293
select {
9394
case subRes, ok := <-subChan:
@@ -100,6 +101,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
100101
emitOnceResult(ctx, out, onceResult{value: p, err: err})
101102
return
102103
}
104+
subResErr = subRes.error
103105
case rootRes, ok := <-rootChan:
104106
if !ok {
105107
rootChan = nil
@@ -108,11 +110,26 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
108110
if rootRes.error == nil {
109111
p, err := appendPath(rootRes.path)
110112
emitOnceResult(ctx, out, onceResult{value: p, err: err})
113+
return
111114
}
115+
rootResErr = rootRes.error
112116
case <-ctx.Done():
113117
return
114118
}
115119
if subChan == nil && rootChan == nil {
120+
// If here, then both lookups failed
121+
//
122+
// If both lookup errors were due to no TXT records with a
123+
// dnslink, then output a more specific error message
124+
if rootResErr == ErrResolveFailed && subResErr == ErrResolveFailed {
125+
i := len(name) - 1
126+
for i >= 0 && name[i] != '/' {
127+
i--
128+
}
129+
// Wrap error so that it can be tested if it is a ErrResolveFailed
130+
err := fmt.Errorf("%w: %q is missing a DNSLink record (https://docs.ipfs.io/concepts/dnslink/)", ErrResolveFailed, name[i+1:])
131+
emitOnceResult(ctx, out, onceResult{err: err})
132+
}
116133
return
117134
}
118135
}
@@ -126,7 +143,14 @@ func workDomain(r *DNSResolver, name string, res chan lookupRes) {
126143

127144
txt, err := r.lookupTXT(name)
128145
if err != nil {
129-
// Error is != nil
146+
if dnsErr, ok := err.(*net.DNSError); ok {
147+
// If no TXT records found, return same error as when no text
148+
// records contain dnslink. Otherwise, return the actual error.
149+
if dnsErr.IsNotFound {
150+
err = ErrResolveFailed
151+
}
152+
}
153+
// Could not look up any text records for name
130154
res <- lookupRes{"", err}
131155
return
132156
}
@@ -138,6 +162,8 @@ func workDomain(r *DNSResolver, name string, res chan lookupRes) {
138162
return
139163
}
140164
}
165+
166+
// There were no TXT records with a dnslink
141167
res <- lookupRes{"", ErrResolveFailed}
142168
}
143169

0 commit comments

Comments
 (0)