Skip to content

Commit 33050b4

Browse files
committed
Output a more useful resolve error
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. This commit was moved from ipfs/go-namesys@4e753ad
1 parent 95150d3 commit 33050b4

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

namesys/base.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package namesys
22

33
import (
44
"context"
5+
"fmt"
56
"strings"
67
"time"
78

@@ -36,6 +37,14 @@ func resolve(ctx context.Context, r resolver, name string, options opts.ResolveO
3637
}
3738
}
3839

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: %s is missing DNSLink record (https://docs.ipfs.io/concepts/dnslink/)", ErrResolveFailed, name[i+1:])
47+
}
3948
return p, err
4049
}
4150

namesys/namesys_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package namesys
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"testing"
78
"time"
@@ -25,7 +26,7 @@ type mockResolver struct {
2526
func testResolution(t *testing.T, resolver Resolver, name string, depth uint, expected string, expError error) {
2627
t.Helper()
2728
p, err := resolver.Resolve(context.Background(), name, opts.Depth(depth))
28-
if err != expError {
29+
if !errors.Is(err, expError) {
2930
t.Fatal(fmt.Errorf(
3031
"expected %s with a depth of %d to have a '%s' error, but got '%s'",
3132
name, depth, expError, err))

0 commit comments

Comments
 (0)