Skip to content

Commit 48dd05c

Browse files
committed
Cleanup errors
1 parent 2aa1a69 commit 48dd05c

File tree

6 files changed

+34
-33
lines changed

6 files changed

+34
-33
lines changed

base83/base83.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package base83
22

3+
import "fmt"
4+
35
const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz#$%*+,-.:;=?@[]^_{|}~"
46

57
// lookup table for ASCII->base83 mapping
@@ -27,7 +29,7 @@ func Decode(str string) (val int, err error) {
2729
for i := 0; i < len(str); i++ {
2830
idx := charLookup[str[i]]
2931
if idx == -1 {
30-
return 0, invalidError(rune(str[i]), i)
32+
return 0, fmt.Errorf("%w: invalid character %q at index %d", ErrInvalidInput, rune(str[i]), i)
3133
}
3234
val = val*83 + idx
3335
}

base83/error.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
package base83
22

3-
import "errors"
3+
import (
4+
"errors"
5+
)
46

5-
// ErrInvalidInput is returned when the given input to decode is not valid base83.
6-
var ErrInvalidInput = errors.New("base83: invalid input")
7-
8-
func invalidError(r rune, i int) error {
9-
// No stdlib support for wrapped errors, so return as-is pre-1.13
10-
return ErrInvalidInput
11-
}
7+
var (
8+
// ErrInvalidInput is returned when the given input to decode is not valid base83.
9+
ErrInvalidInput = errors.New("base83: invalid input")
10+
)

decode.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blurhash
22

33
import (
4+
"fmt"
45
"image"
56
"image/color"
67
"image/draw"
@@ -12,7 +13,7 @@ import (
1213
// Components returns the X and Y components of a blurhash.
1314
func Components(hash string) (x, y int, err error) {
1415
if len(hash) < 6 {
15-
return 0, 0, ErrInvalidHash
16+
return 0, 0, fmt.Errorf("%w: hash too short", ErrInvalidHash)
1617
}
1718

1819
sizeFlag, err := base83.Decode(string(hash[0]))
@@ -26,7 +27,7 @@ func Components(hash string) (x, y int, err error) {
2627
expectedLength := 4 + 2*x*y
2728
actualLength := len(hash)
2829
if expectedLength != actualLength {
29-
return 0, 0, lengthError(expectedLength, actualLength)
30+
return 0, 0, fmt.Errorf("%w: length mismatch: expected %d, got %d", ErrInvalidHash, expectedLength, actualLength)
3031
}
3132

3233
return x, y, nil
@@ -54,7 +55,7 @@ func NewDecoder() *Decoder {
5455
// Internal buffers are reused across calls when possible.
5556
func (d *Decoder) Decode(hash string, width, height, punch int) (image.Image, error) {
5657
if width <= 0 || height <= 0 {
57-
return nil, ErrInvalidDimensions
58+
return nil, fmt.Errorf("%w: had width=%d, height=%d", ErrInvalidDimensions, width, height)
5859
}
5960
newImg := image.NewNRGBA(image.Rect(0, 0, width, height))
6061
if err := d.DecodeDraw(newImg, hash, float64(punch)); err != nil {

decode_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blurhash_test
22

33
import (
4+
"errors"
45
"image"
56
"image/png"
67
"io"
@@ -82,7 +83,7 @@ func TestComponentsInvalidHash(t *testing.T) {
8283
shortHashes := []string{"", "A", "ABCDE"}
8384
for _, hash := range shortHashes {
8485
_, _, err := blurhash.Components(hash)
85-
if err != blurhash.ErrInvalidHash {
86+
if !errors.Is(err, blurhash.ErrInvalidHash) {
8687
t.Errorf("short hash %q should return ErrInvalidHash, got %v", hash, err)
8788
}
8889
}
@@ -94,7 +95,7 @@ func TestComponentsInvalidHash(t *testing.T) {
9495
if err == nil {
9596
t.Fatal("invalid character should return error")
9697
}
97-
if err != base83.ErrInvalidInput {
98+
if !errors.Is(err, base83.ErrInvalidInput) {
9899
t.Errorf("expected invalid base83 error, got %v", err)
99100
}
100101
})
@@ -103,13 +104,13 @@ func TestComponentsInvalidHash(t *testing.T) {
103104
// '9' encodes 1x2 components (sizeFlag=9), expecting 4+2*1*2=8 chars
104105
// but we provide only 6 chars
105106
_, _, err := blurhash.Components("900000")
106-
if err != blurhash.ErrInvalidHash {
107+
if !errors.Is(err, blurhash.ErrInvalidHash) {
107108
t.Errorf("wrong length should return ErrInvalidHash, got %v", err)
108109
}
109110

110111
// Valid 1x1 hash is 6 chars, but provide 8
111112
_, _, err = blurhash.Components("00000000")
112-
if err != blurhash.ErrInvalidHash {
113+
if !errors.Is(err, blurhash.ErrInvalidHash) {
113114
t.Errorf("wrong length should return ErrInvalidHash, got %v", err)
114115
}
115116
})
@@ -173,7 +174,7 @@ func TestDecodeInvalidDimensions(t *testing.T) {
173174
for _, tt := range tests {
174175
t.Run(tt.name, func(t *testing.T) {
175176
_, err := blurhash.Decode(testFixtures[0].hash, tt.width, tt.height, 1)
176-
if err != blurhash.ErrInvalidDimensions {
177+
if !errors.Is(err, blurhash.ErrInvalidDimensions) {
177178
t.Errorf("invalid dimensions should return ErrInvalidDimensions, got %v", err)
178179
}
179180
})

encode.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package blurhash
22

33
import (
4+
"fmt"
45
"image"
56
"image/draw"
67
"math"
@@ -39,7 +40,7 @@ func NewEncoder() *Encoder {
3940
func (e *Encoder) Encode(xComponents, yComponents int, img image.Image) (string, error) {
4041
if xComponents < minComponents || xComponents > maxComponents ||
4142
yComponents < minComponents || yComponents > maxComponents {
42-
return "", ErrInvalidComponents
43+
return "", fmt.Errorf("%w: had x=%d, y=%d", ErrInvalidComponents, xComponents, yComponents)
4344
}
4445

4546
bounds := img.Bounds()

error.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,14 @@
11
package blurhash
22

3-
import "errors"
4-
5-
// ErrInvalidComponents is returned when components passed to Encode are invalid.
6-
var ErrInvalidComponents = errors.New("blurhash: must have between 1 and 9 components")
7-
8-
// ErrInvalidHash is returned when the library encounters a hash it can't recognise.
9-
var ErrInvalidHash = errors.New("blurhash: invalid hash")
10-
11-
// ErrInvalidDimensions is returned when width or height is invalid.
12-
var ErrInvalidDimensions = errors.New("blurhash: width and height must be positive")
13-
14-
func lengthError(expectedLength, actualLength int) error {
15-
// No stdlib support for wrapped errors, so return as-is pre-1.13
16-
return ErrInvalidHash
17-
}
3+
import (
4+
"errors"
5+
)
6+
7+
var (
8+
// ErrInvalidComponents is returned when components passed to Encode are invalid.
9+
ErrInvalidComponents = errors.New("blurhash: must have between 1 and 9 components")
10+
// ErrInvalidHash is returned when the library encounters a hash it can't recognise.
11+
ErrInvalidHash = errors.New("blurhash: invalid hash")
12+
// ErrInvalidDimensions is returned when width or height is invalid.
13+
ErrInvalidDimensions = errors.New("blurhash: width and height must be positive")
14+
)

0 commit comments

Comments
 (0)