Commit 4e753ad8 authored by gammazero's avatar gammazero Committed by Andrew Gillis

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.
parent c17fc946
...@@ -2,6 +2,7 @@ package namesys ...@@ -2,6 +2,7 @@ package namesys
import ( import (
"context" "context"
"fmt"
"strings" "strings"
"time" "time"
...@@ -36,6 +37,14 @@ func resolve(ctx context.Context, r resolver, name string, options opts.ResolveO ...@@ -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 return p, err
} }
......
...@@ -2,6 +2,7 @@ package namesys ...@@ -2,6 +2,7 @@ package namesys
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"testing" "testing"
"time" "time"
...@@ -25,7 +26,7 @@ type mockResolver struct { ...@@ -25,7 +26,7 @@ type mockResolver struct {
func testResolution(t *testing.T, resolver Resolver, name string, depth uint, expected string, expError error) { func testResolution(t *testing.T, resolver Resolver, name string, depth uint, expected string, expError error) {
t.Helper() t.Helper()
p, err := resolver.Resolve(context.Background(), name, opts.Depth(depth)) p, err := resolver.Resolve(context.Background(), name, opts.Depth(depth))
if err != expError { if !errors.Is(err, expError) {
t.Fatal(fmt.Errorf( t.Fatal(fmt.Errorf(
"expected %s with a depth of %d to have a '%s' error, but got '%s'", "expected %s with a depth of %d to have a '%s' error, but got '%s'",
name, depth, expError, err)) name, depth, expError, err))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment