Skip to content

Commit c70fcc9

Browse files
author
Devon Bear
authored
feat(errors): Use custom erorrs to allow errors.Is() (#19)
1 parent f61e0ca commit c70fcc9

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

errors.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package gohashtree
2+
3+
import "errors"
4+
5+
var (
6+
// ErrOddChunks is returned when the number of chunks is odd.
7+
ErrOddChunks = errors.New("odd number of chunks")
8+
// ErrNotEnoughDigests is returned when the number of digests is not enough.
9+
ErrNotEnoughDigests = errors.New("not enough digest length")
10+
// ErrChunksNotMultipleOf64 is returned when the chunks are not multiple of 64 bytes.
11+
ErrChunksNotMultipleOf64 = errors.New("chunks not multiple of 64 bytes")
12+
// ErrDigestsNotMultipleOf32 is returned when the digests are not multiple of 32 bytes.
13+
ErrDigestsNotMultipleOf32 = errors.New("digests not multiple of 32 bytes")
14+
)

hash.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ func Hash(digests [][32]byte, chunks [][32]byte) error {
3838
}
3939

4040
if len(chunks)%2 == 1 {
41-
return fmt.Errorf("odd number of chunks")
41+
return ErrOddChunks
4242
}
4343
if len(digests) < len(chunks)/2 {
44-
return fmt.Errorf("not enough digest length, need at least %v, got %v", len(chunks)/2, len(digests))
44+
return fmt.Errorf("%w: need at least %v, got %v", ErrNotEnoughDigests, len(chunks)/2, len(digests))
4545
}
4646
if supportedCPU {
4747
_hash(&digests[0][0], chunks, uint32(len(chunks)/2))
@@ -64,14 +64,17 @@ func HashByteSlice(digests []byte, chunks []byte) error {
6464
if len(chunks) == 0 {
6565
return nil
6666
}
67+
6768
if len(chunks)%64 != 0 {
68-
return fmt.Errorf("chunks not multiple of 64 bytes")
69+
return ErrChunksNotMultipleOf64
6970
}
71+
7072
if len(digests)%32 != 0 {
71-
return fmt.Errorf("digests not multiple of 32 bytes")
73+
return ErrDigestsNotMultipleOf32
7274
}
75+
7376
if len(digests) < len(chunks)/2 {
74-
return fmt.Errorf("not enough digest length, need at least %d, got %d", len(chunks)/2, len(digests))
77+
return fmt.Errorf("%w: need at least %v, got %v", ErrNotEnoughDigests, len(chunks)/2, len(digests))
7578
}
7679
// We use an unsafe pointer to cast []byte to [][32]byte. The length and
7780
// capacity of the slice need to be divided accordingly by 32.

hash_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ SOFTWARE.
2424
package gohashtree_test
2525

2626
import (
27+
"errors"
2728
"reflect"
2829
"testing"
2930

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

0 commit comments

Comments
 (0)