mirror of
https://github.com/jimeh/go-base58.git
synced 2026-02-19 08:06:39 +00:00
Improve decode errors
This commit is contained in:
19
base58.go
19
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)
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
|
||||
Reference in New Issue
Block a user