diff --git a/registry.go b/registry.go index 70652c3..39a1cca 100644 --- a/registry.go +++ b/registry.go @@ -5,6 +5,7 @@ import ( "crypto/sha1" "crypto/sha256" "crypto/sha512" + "fmt" "hash" ) @@ -50,11 +51,11 @@ func Register(indicator uint64, hasherFactory func() hash.Hash) { // Other hash implementations can be made available by using the Register function. // The 'go-mulithash/register/*' packages can also be imported to gain more common hash functions. // -// If an error is returned, it will be ErrSumNotSupported. +// If an error is returned, it will match `errors.Is(err, ErrSumNotSupported)`. func GetHasher(indicator uint64) (hash.Hash, error) { factory, exists := registry[indicator] if !exists { - return nil, ErrSumNotSupported // REVIEW: it's unfortunate that this error doesn't say what code was missing. Also "NotSupported" is a bit of a misnomer now. + return nil, fmt.Errorf("unknown multihash code %d (0x%x): %w", indicator, indicator, ErrSumNotSupported) } return factory(), nil } diff --git a/sum.go b/sum.go index b0563f2..53c1805 100644 --- a/sum.go +++ b/sum.go @@ -5,7 +5,7 @@ import ( ) // ErrSumNotSupported is returned when the Sum function code is not implemented -var ErrSumNotSupported = errors.New("Function not implemented. Complain to lib maintainer.") +var ErrSumNotSupported = errors.New("no such hash registered") var ErrLenTooLarge = errors.New("requested length was too large for digest") diff --git a/sum_test.go b/sum_test.go index 0ad3b75..c2e9c1b 100644 --- a/sum_test.go +++ b/sum_test.go @@ -3,6 +3,7 @@ package multihash_test import ( "bytes" "encoding/hex" + "errors" "fmt" "runtime" "testing" @@ -132,8 +133,9 @@ func TestTooLargeLength(t *testing.T) { func TestBasicSum(t *testing.T) { for code, name := range multihash.Codes { _, err := multihash.Sum([]byte("test"), code, -1) - switch err { - case multihash.ErrSumNotSupported, nil: + switch { + case errors.Is(err, multihash.ErrSumNotSupported): + case err == nil: default: t.Errorf("unexpected error for %s: %s", name, err) }