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

Properly report DNSLink errors #12

Merged
merged 2 commits into from
Apr 21, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 0 additions & 9 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package namesys

import (
"context"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -37,14 +36,6 @@ func resolve(ctx context.Context, r resolver, name string, options opts.ResolveO
}
}

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

Expand Down
28 changes: 27 additions & 1 deletion dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options

go func() {
defer close(out)
var rootResErr, subResErr error
for {
select {
case subRes, ok := <-subChan:
Expand All @@ -100,6 +101,7 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
emitOnceResult(ctx, out, onceResult{value: p, err: err})
return
}
subResErr = subRes.error
case rootRes, ok := <-rootChan:
if !ok {
rootChan = nil
Expand All @@ -108,11 +110,26 @@ func (r *DNSResolver) resolveOnceAsync(ctx context.Context, name string, options
if rootRes.error == nil {
p, err := appendPath(rootRes.path)
emitOnceResult(ctx, out, onceResult{value: p, err: err})
return
}
rootResErr = rootRes.error
case <-ctx.Done():
return
}
if subChan == nil && rootChan == nil {
// If here, then both lookups failed
//
// If both lookup errors were due to no TXT records with a
// dnslink, then output a more specific error message
if rootResErr == ErrResolveFailed && subResErr == ErrResolveFailed {
i := len(name) - 1
for i >= 0 && name[i] != '/' {
i--
}
// Wrap error so that it can be tested if it is a ErrResolveFailed
err := fmt.Errorf("%w: %q is missing a DNSLink record (https://docs.ipfs.io/concepts/dnslink/)", ErrResolveFailed, name[i+1:])
Copy link
Contributor

@aschmahmann aschmahmann Apr 21, 2021

Choose a reason for hiding this comment

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

Does this mean "is missing a DNSLink record, or the host could not be found" (or some phrasing like that)?

It looks like we're also covering dnsErr.IsNotFound which is "host could not be found" https://golang.org/pkg/net/#DNSError

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This error covers both. So, r.lookupTXT(name) returns a host not found error when DNS does not return a TXT record, but in either case (1. Has TXT records, but none contain DNSLink. 2. No TXT records returned) it is correct that name is missing a DNSLink record.

Copy link
Contributor

Choose a reason for hiding this comment

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

If your machine is offline to you also get a host not found?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No. You do not. I determine that by checking the dnsErr.IsNotFound flag here. If you are offline, then IsNotFound is false.

Copy link
Contributor

Choose a reason for hiding this comment

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

If you are offline, then IsNotFound is false.

The DNSError struct documentation is kind of sparse ("host not found" seems like it could mean a bunch of things). If you've tested that IsNotFound doesn't apply when you cannot reach DNS then 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes tested

emitOnceResult(ctx, out, onceResult{err: err})
}
return
}
}
Expand All @@ -126,7 +143,14 @@ func workDomain(r *DNSResolver, name string, res chan lookupRes) {

txt, err := r.lookupTXT(name)
if err != nil {
// Error is != nil
if dnsErr, ok := err.(*net.DNSError); ok {
// If no TXT records found, return same error as when no text
// records contain dnslink. Otherwise, return the actual error.
if dnsErr.IsNotFound {
err = ErrResolveFailed
}
}
// Could not look up any text records for name
res <- lookupRes{"", err}
return
}
Expand All @@ -138,6 +162,8 @@ func workDomain(r *DNSResolver, name string, res chan lookupRes) {
return
}
}

// There were no TXT records with a dnslink
res <- lookupRes{"", ErrResolveFailed}
}

Expand Down