diff options
author | Lewis Marshall <lewis@lmars.net> | 2017-05-13 08:02:25 +0800 |
---|---|---|
committer | Felix Lange <fjl@users.noreply.github.com> | 2017-05-13 08:02:25 +0800 |
commit | b0d0fafd68f526ceed98e59a423b6470f2327a21 (patch) | |
tree | 48e0f7286e0b2811e6e1bc5f319abea82012dcee /swarm/api/api.go | |
parent | 90c7155ef41900babc5fd736224127f048e5c883 (diff) | |
download | dexon-b0d0fafd68f526ceed98e59a423b6470f2327a21.tar.gz dexon-b0d0fafd68f526ceed98e59a423b6470f2327a21.tar.zst dexon-b0d0fafd68f526ceed98e59a423b6470f2327a21.zip |
swarm/api: fix error reporting in api.Resolve (#14464)
Previously, resolve errors were being swallowed and the returned error
was a generic "not a content hash" which isn't helpful.
This updates the Resolve function to fail fast rather than only
returning an error at the end, and also adds test coverage.
Diffstat (limited to 'swarm/api/api.go')
-rw-r--r-- | swarm/api/api.go | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/swarm/api/api.go b/swarm/api/api.go index 26a9445d5..803265a3e 100644 --- a/swarm/api/api.go +++ b/swarm/api/api.go @@ -25,12 +25,13 @@ import ( "sync" "bytes" - "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/log" - "github.com/ethereum/go-ethereum/swarm/storage" "mime" "path/filepath" "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/log" + "github.com/ethereum/go-ethereum/swarm/storage" ) var ( @@ -84,27 +85,33 @@ type ErrResolve error func (self *Api) Resolve(uri *URI) (storage.Key, error) { log.Trace(fmt.Sprintf("Resolving : %v", uri.Addr)) - var err error - if !uri.Immutable() { - if self.dns != nil { - resolved, err := self.dns.Resolve(uri.Addr) - if err == nil { - return resolved[:], nil - } - } else { - err = fmt.Errorf("no DNS to resolve name") + // if the URI is immutable, check if the address is a hash + isHash := hashMatcher.MatchString(uri.Addr) + if uri.Immutable() { + if !isHash { + return nil, fmt.Errorf("immutable address not a content hash: %q", uri.Addr) } + return common.Hex2Bytes(uri.Addr), nil } - if hashMatcher.MatchString(uri.Addr) { - return storage.Key(common.Hex2Bytes(uri.Addr)), nil + + // if DNS is not configured, check if the address is a hash + if self.dns == nil { + if !isHash { + return nil, fmt.Errorf("no DNS to resolve name: %q", uri.Addr) + } + return common.Hex2Bytes(uri.Addr), nil } - if err != nil { - return nil, fmt.Errorf("'%s' does not resolve: %v but is not a content hash", uri.Addr, err) + + // try and resolve the address + resolved, err := self.dns.Resolve(uri.Addr) + if err == nil { + return resolved[:], nil + } else if !isHash { + return nil, err } - return nil, fmt.Errorf("'%s' is not a content hash", uri.Addr) + return common.Hex2Bytes(uri.Addr), nil } - // Put provides singleton manifest creation on top of dpa store func (self *Api) Put(content, contentType string) (storage.Key, error) { r := strings.NewReader(content) |