improve parse error

This includes the bad multiaddr in the error and should improve UX (see
https://github.com/ipfs/go-ipfs/issues/4190).
This commit is contained in:
Steven Allen 2019-05-16 18:37:21 -07:00
parent e1825f7b50
commit 86a587ec70

View File

@ -7,7 +7,6 @@ import (
) )
func stringToBytes(s string) ([]byte, error) { func stringToBytes(s string) ([]byte, error) {
// consume trailing slashes // consume trailing slashes
s = strings.TrimRight(s, "/") s = strings.TrimRight(s, "/")
@ -15,7 +14,7 @@ func stringToBytes(s string) ([]byte, error) {
sp := strings.Split(s, "/") sp := strings.Split(s, "/")
if sp[0] != "" { if sp[0] != "" {
return nil, fmt.Errorf("invalid multiaddr, must begin with /") return nil, fmt.Errorf("failed to parse multiaddr %q: must begin with /", s)
} }
// consume first empty elem // consume first empty elem
@ -25,7 +24,7 @@ func stringToBytes(s string) ([]byte, error) {
name := sp[0] name := sp[0]
p := ProtocolWithName(name) p := ProtocolWithName(name)
if p.Code == 0 { if p.Code == 0 {
return nil, fmt.Errorf("no protocol with name %s", sp[0]) return nil, fmt.Errorf("failed to parse multiaddr %q: unknown protocol %s", s, sp[0])
} }
_, _ = b.Write(CodeToVarint(p.Code)) _, _ = b.Write(CodeToVarint(p.Code))
sp = sp[1:] sp = sp[1:]
@ -35,7 +34,7 @@ func stringToBytes(s string) ([]byte, error) {
} }
if len(sp) < 1 { if len(sp) < 1 {
return nil, fmt.Errorf("protocol requires address, none given: %s", name) return nil, fmt.Errorf("failed to parse multiaddr %q: unexpected end of multiaddr", s)
} }
if p.Path { if p.Path {
@ -46,7 +45,7 @@ func stringToBytes(s string) ([]byte, error) {
a, err := p.Transcoder.StringToBytes(sp[0]) a, err := p.Transcoder.StringToBytes(sp[0])
if err != nil { if err != nil {
return nil, fmt.Errorf("failed to parse %s: %s %s", p.Name, sp[0], err) return nil, fmt.Errorf("failed to parse multiaddr %q: invalid value %q for protocol %s: %s", s, sp[0], p.Name, err)
} }
if p.Size < 0 { // varint size. if p.Size < 0 { // varint size.
_, _ = b.Write(CodeToVarint(len(a))) _, _ = b.Write(CodeToVarint(len(a)))