diff options
author | Janos Guljas <janos@resenje.org> | 2017-12-19 06:07:48 +0800 |
---|---|---|
committer | Janos Guljas <janos@resenje.org> | 2017-12-19 06:07:48 +0800 |
commit | 0d6a735a72340130acd6b7e536dad5d8bee40d84 (patch) | |
tree | 0dbe9c9c1ca2cf130a790c9e6080e8515965dbf5 /swarm/api/api.go | |
parent | c0a4d9e1e64a09a19484c8c12e24505d9bacbd57 (diff) | |
download | dexon-0d6a735a72340130acd6b7e536dad5d8bee40d84.tar.gz dexon-0d6a735a72340130acd6b7e536dad5d8bee40d84.tar.zst dexon-0d6a735a72340130acd6b7e536dad5d8bee40d84.zip |
swarm/api: implement NoResolverError with information about TLD
MultiResolver needs to provide information about TLD that has
no resolver configured for.
Diffstat (limited to 'swarm/api/api.go')
-rw-r--r-- | swarm/api/api.go | 26 |
1 files changed, 20 insertions, 6 deletions
diff --git a/swarm/api/api.go b/swarm/api/api.go index 2d9db022e..c143d8a3f 100644 --- a/swarm/api/api.go +++ b/swarm/api/api.go @@ -17,7 +17,6 @@ package api import ( - "errors" "fmt" "io" "net/http" @@ -42,9 +41,22 @@ type Resolver interface { Resolve(string) (common.Hash, error) } -// errNoResolver is returned by MultiResolver.Resolve if no resolver +// NoResolverError is returned by MultiResolver.Resolve if no resolver // can be found for the address. -var errNoResolver = errors.New("no resolver") +type NoResolverError struct { + TLD string +} + +func NewNoResolverError(tld string) *NoResolverError { + return &NoResolverError{TLD: tld} +} + +func (e *NoResolverError) Error() string { + if e.TLD == "" { + return "no ENS resolver" + } + return fmt.Sprintf("no ENS endpoint configured to resolve .%s TLD names", e.TLD) +} // MultiResolver is used to resolve URL addresses based on their TLDs. // Each TLD can have multiple resolvers, and the resoluton from the @@ -84,14 +96,16 @@ func NewMultiResolver(opts ...MultiResolverOption) (m *MultiResolver) { // will be returned. func (m MultiResolver) Resolve(addr string) (h common.Hash, err error) { rs := m.resolvers[""] - if ext := path.Ext(addr); ext != "" { - rstld, ok := m.resolvers[ext[1:]] + tld := path.Ext(addr) + if tld != "" { + tld = tld[1:] + rstld, ok := m.resolvers[tld] if ok { rs = rstld } } if rs == nil { - return h, errNoResolver + return h, NewNoResolverError(tld) } for _, r := range rs { h, err = r.Resolve(addr) |