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

Commit

Permalink
Output a more useful resolve error
Browse files Browse the repository at this point in the history
This outputs a missage that blames a specific sife for not having DNSLink record.  For example, the error message looks like:

`could not resolve name: bad.example.net is missing DNSLink record (https://docs.ipfs.io/concepts/dnslink/)`

The "could not resolve name" portion is still present because the returned error wraps the original ErrResolveFailed, allowing code to test if the error is an ErrorResolveFailed error.
  • Loading branch information
gammazero committed Apr 7, 2021
1 parent c17fc94 commit 4e753ad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
9 changes: 9 additions & 0 deletions base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package namesys

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

Expand Down Expand Up @@ -36,6 +37,14 @@ 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: %s is missing DNSLink record (https://docs.ipfs.io/concepts/dnslink/)", ErrResolveFailed, name[i+1:])
}
return p, err
}

Expand Down
3 changes: 2 additions & 1 deletion namesys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package namesys

import (
"context"
"errors"
"fmt"
"testing"
"time"
Expand All @@ -25,7 +26,7 @@ type mockResolver struct {
func testResolution(t *testing.T, resolver Resolver, name string, depth uint, expected string, expError error) {
t.Helper()
p, err := resolver.Resolve(context.Background(), name, opts.Depth(depth))
if err != expError {
if !errors.Is(err, expError) {
t.Fatal(fmt.Errorf(
"expected %s with a depth of %d to have a '%s' error, but got '%s'",
name, depth, expError, err))
Expand Down

0 comments on commit 4e753ad

Please sign in to comment.