lib/protocol: More descriptive errors on device ID parse failures

This commit is contained in:
Jakob Borg 2017-04-27 14:45:35 +09:00
parent d6fbfc3545
commit e0405de5bf

View File

@ -11,9 +11,8 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/syncthing/syncthing/lib/sha256"
"github.com/calmh/luhn" "github.com/calmh/luhn"
"github.com/syncthing/syncthing/lib/sha256"
) )
const DeviceIDLength = 32 const DeviceIDLength = 32
@ -124,7 +123,7 @@ func (n *DeviceID) UnmarshalText(bs []byte) error {
copy(n[:], dec) copy(n[:], dec)
return nil return nil
default: default:
return errors.New("device ID invalid: incorrect length") return fmt.Errorf("%q: device ID invalid: incorrect length", bs)
} }
} }
@ -145,7 +144,7 @@ func (n *DeviceID) MarshalTo(bs []byte) (int, error) {
func (n *DeviceID) Unmarshal(bs []byte) error { func (n *DeviceID) Unmarshal(bs []byte) error {
// Used by protobuf marshaller. // Used by protobuf marshaller.
if len(bs) < DeviceIDLength { if len(bs) < DeviceIDLength {
return errors.New("not enough data") return fmt.Errorf("%q: not enough data", bs)
} }
copy((*n)[:], bs) copy((*n)[:], bs)
return nil return nil
@ -170,7 +169,7 @@ func luhnify(s string) (string, error) {
func unluhnify(s string) (string, error) { func unluhnify(s string) (string, error) {
if len(s) != 56 { if len(s) != 56 {
return "", fmt.Errorf("unsupported string length %d", len(s)) return "", fmt.Errorf("%q: unsupported string length %d", s, len(s))
} }
res := make([]string, 0, 4) res := make([]string, 0, 4)
@ -181,7 +180,7 @@ func unluhnify(s string) (string, error) {
return "", err return "", err
} }
if g := fmt.Sprintf("%s%c", p, l); g != s[i*14:(i+1)*14] { if g := fmt.Sprintf("%s%c", p, l); g != s[i*14:(i+1)*14] {
return "", errors.New("check digit incorrect") return "", fmt.Errorf("%q: check digit incorrect", s)
} }
res = append(res, p) res = append(res, p)
} }