Improve decode errors

This commit is contained in:
2016-06-29 00:39:30 +01:00
parent 6ce87d138a
commit ecd92f1412
2 changed files with 19 additions and 3 deletions

View File

@@ -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)
}

View File

@@ -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")