From ecd92f1412a17685025043f386812c6a41f0b3a7 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Wed, 29 Jun 2016 00:39:30 +0100 Subject: [PATCH] Improve decode errors --- base58.go | 19 +++++++++++++++++-- base58_test.go | 3 ++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/base58.go b/base58.go index 860f59c..3d9f4f2 100644 --- a/base58.go +++ b/base58.go @@ -2,6 +2,7 @@ package base58 import ( "errors" + "fmt" "strings" ) @@ -46,8 +47,7 @@ func DecodeWithAlphabet(str string, alphabet string) (int, error) { char := string(str[i-1]) index := strings.Index(alphabet, char) if index == -1 { - errorMsg := "\"" + str + "\" is not a valid base58 string." - return -1, errors.New(errorMsg) + return -1, decodeError(str, alphabet) } num += multi * index multi = multi * base @@ -55,3 +55,18 @@ func DecodeWithAlphabet(str string, alphabet string) (int, error) { return num, nil } + +func decodeError(str string, alphabet string) error { + var msg string + + if alphabet == Alphabet { + msg = fmt.Sprintf("\"%s\" is not a valid base58 string.", str) + } else { + msg = fmt.Sprintf( + "\"%s\" is not a valid input for alphabet \"%s\".", + str, alphabet, + ) + } + + return errors.New(msg) +} diff --git a/base58_test.go b/base58_test.go index 2116f23..6a19f22 100644 --- a/base58_test.go +++ b/base58_test.go @@ -244,7 +244,8 @@ func (s *Base58Suite) TestDecodeWithAlphabet() { func (s *Base58Suite) TestDecodeWithAlphabetError() { assert := assert.New(s.T()) - errMsg := "\"AaBbCc\" is not a valid base58 string." + errMsg := "\"AaBbCc\" is not a valid input for alphabet " + + "\"abcdefghjklmnpqrstuvwxyz\"." result, err := DecodeWithAlphabet("AaBbCc", "abcdefghjklmnpqrstuvwxyz")