Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(errors): Use custom erorrs to allow errors.Is() #19

Merged
merged 2 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gohashtree

import "errors"

var (
// ErrOddChunks is returned when the number of chunks is odd.
ErrOddChunks = errors.New("odd number of chunks")
// ErrNotEnoughDigests is returned when the number of digests is not enough.
ErrNotEnoughDigests = errors.New("not enough digest length")
// ErrChunksNotMultipleOf64 is returned when the chunks are not multiple of 64 bytes.
ErrChunksNotMultipleOf64 = errors.New("chunks not multiple of 64 bytes")
// ErrDigestsNotMultipleOf32 is returned when the digests are not multiple of 32 bytes.
ErrDigestsNotMultipleOf32 = errors.New("digests not multiple of 32 bytes")
)
13 changes: 8 additions & 5 deletions hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ func Hash(digests [][32]byte, chunks [][32]byte) error {
}

if len(chunks)%2 == 1 {
return fmt.Errorf("odd number of chunks")
return ErrOddChunks
}
if len(digests) < len(chunks)/2 {
return fmt.Errorf("not enough digest length, need at least %v, got %v", len(chunks)/2, len(digests))
return fmt.Errorf("%w: need at least %v, got %v", ErrNotEnoughDigests, len(chunks)/2, len(digests))
}
if supportedCPU {
_hash(&digests[0][0], chunks, uint32(len(chunks)/2))
Expand All @@ -64,14 +64,17 @@ func HashByteSlice(digests []byte, chunks []byte) error {
if len(chunks) == 0 {
return nil
}

if len(chunks)%64 != 0 {
return fmt.Errorf("chunks not multiple of 64 bytes")
return ErrChunksNotMultipleOf64
}

if len(digests)%32 != 0 {
return fmt.Errorf("digests not multiple of 32 bytes")
return ErrDigestsNotMultipleOf32
}

if len(digests) < len(chunks)/2 {
return fmt.Errorf("not enough digest length, need at least %d, got %d", len(chunks)/2, len(digests))
return fmt.Errorf("%w: need at least %v, got %v", ErrNotEnoughDigests, len(chunks)/2, len(digests))
}
// We use an unsafe pointer to cast []byte to [][32]byte. The length and
// capacity of the slice need to be divided accordingly by 32.
Expand Down
3 changes: 2 additions & 1 deletion hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ SOFTWARE.
package gohashtree_test

import (
"errors"
"reflect"
"testing"

Expand Down Expand Up @@ -283,7 +284,7 @@ func TestNotAllocatedDigest(t *testing.T) {
chunks := make([][32]byte, 4)
err := gohashtree.Hash(digests, chunks)
expected := "not enough digest length, need at least 2, got 1"
if err.Error() != expected {
if !errors.Is(err, gohashtree.ErrNotEnoughDigests) {
t.Logf("expected error: \"%s\", got: \"%s\"", expected, err)
t.Fail()
}
Expand Down
Loading