Skip to content

Commit 7911394

Browse files
chore: update error message (#6)
1 parent 36a3cde commit 7911394

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

iter.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ func iteratorFromRange[K, V any](s sdk.KVStore, r Ranger[K], kc KeyEncoder[K], v
139139
case OrderDescending:
140140
iter = s.ReverseIterator(startBytes, endBytes)
141141
default:
142-
panic("unrecognized Order")
142+
panic(fmt.Errorf("unrecognized Order: %v", order))
143143
}
144144

145145
return Iterator[K, V]{

keys.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,16 @@ func (stringKey) Encode(s string) []byte {
3737
func (stringKey) Decode(b []byte) (int, string) {
3838
l := len(b)
3939
if l < 2 {
40-
panic("invalid StringKey bytes")
40+
panic(fmt.Errorf(
41+
"invalid StringKey bytes. StringKey must be at least length 2. %s",
42+
HumanizeBytes(b)))
4143
}
4244
for i, c := range b {
4345
if c == 0 {
4446
return i + 1, string(b[:i])
4547
}
4648
}
47-
panic(fmt.Errorf("string is not null terminated: %s", b))
49+
panic(fmt.Errorf("string is not null terminated: %s %s", b, HumanizeBytes(b)))
4850
}
4951

5052
type uint64Key struct{}
@@ -60,7 +62,7 @@ func (timeKey) Encode(t time.Time) []byte { return sdk.FormatTimeBytes(t) }
6062
func (timeKey) Decode(b []byte) (int, time.Time) {
6163
t, err := sdk.ParseTimeBytes(b)
6264
if err != nil {
63-
panic(err)
65+
panic(fmt.Errorf("%w %s", err, HumanizeBytes(b)))
6466
}
6567
return len(b), t
6668
}
@@ -85,7 +87,7 @@ func (v valAddressKeyEncoder) Decode(b []byte) (int, sdk.ValAddress) {
8587
r, s := StringKeyEncoder.Decode(b)
8688
valAddr, err := sdk.ValAddressFromBech32(s)
8789
if err != nil {
88-
panic(err)
90+
panic(fmt.Errorf("%w %s", err, HumanizeBytes(b)))
8991
}
9092
return r, valAddr
9193
}
@@ -113,7 +115,7 @@ func (consAddressKeyEncoder) Decode(b []byte) (int, sdk.ConsAddress) {
113115
r, s := StringKeyEncoder.Decode(b)
114116
consAddr, err := sdk.ConsAddressFromBech32(s)
115117
if err != nil {
116-
panic(err)
118+
panic(fmt.Errorf("%w %s", err, HumanizeBytes(b)))
117119
}
118120
return r, consAddr
119121
}
@@ -126,15 +128,37 @@ func (sdkDecKeyEncoder) Stringify(key sdk.Dec) string { return key.String() }
126128
func (sdkDecKeyEncoder) Encode(key sdk.Dec) []byte {
127129
bz, err := key.Marshal()
128130
if err != nil {
129-
panic(fmt.Errorf("invalid DecKey: %w", err))
131+
panic(fmt.Errorf("invalid DecKey: %w %s", err, HumanizeBytes(bz)))
130132
}
131133
return bz
132134
}
133135
func (sdkDecKeyEncoder) Decode(b []byte) (int, sdk.Dec) {
134136
var dec sdk.Dec
135137
if err := dec.Unmarshal(b); err != nil {
136-
panic(fmt.Errorf("invalid DecKey bytes: %w", err))
138+
panic(fmt.Errorf("invalid DecKey bytes: %w %s", err, HumanizeBytes(b)))
137139
}
138140

139141
return len(b), dec
140142
}
143+
144+
// HumanizeBytes is a shorthand function for converting a slice of bytes ([]byte)
145+
// into to hexadecimal string with a short descriptor. This function is meant to
146+
// make error messages more readable since the bytes will be reproducable.
147+
//
148+
// For example,
149+
// ```go
150+
// import "encoding/hex"
151+
// import "fmt"
152+
//
153+
// // When logging the hex string...
154+
// bz := []byte("ABC€日本語")
155+
// bytesAsHex := fmt.Sprintf("%x", bz) // 414243e282ace697a5e69cace8aa9e
156+
//
157+
// // Later when debugging using the logs, the original bytes can be found
158+
// // easily using the 'bytesAsHex'
159+
// bz, _ := hex.DecodeString("414243e282ace697a5e69cace8aa9e")
160+
// fmt.Println(string(bz)) // ABC€日本語
161+
// ````
162+
func HumanizeBytes(bz []byte) string {
163+
return fmt.Sprintf("\nbytesAsHex: %x", bz)
164+
}

keyset.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (s setObject) Encode(_ setObject) []byte { return []byte{} }
6868

6969
func (s setObject) Decode(b []byte) setObject {
7070
if !bytes.Equal(b, []byte{}) {
71-
panic(fmt.Sprintf("invalid bytes: %s", b))
71+
panic(fmt.Sprintf("invalid bytes: %s %s", b, HumanizeBytes(b)))
7272
}
7373
return setObject{}
7474
}

values.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package collections
22

33
import (
4+
"fmt"
5+
46
"github.com/cosmos/cosmos-sdk/codec"
57
sdk "github.com/cosmos/cosmos-sdk/types"
68
"github.com/gogo/protobuf/proto"
@@ -45,7 +47,7 @@ type decValue struct{}
4547
func (d decValue) Encode(value sdk.Dec) []byte {
4648
b, err := value.Marshal()
4749
if err != nil {
48-
panic(err)
50+
panic(fmt.Errorf("%w %s", err, HumanizeBytes(b)))
4951
}
5052
return b
5153
}
@@ -54,7 +56,7 @@ func (d decValue) Decode(b []byte) sdk.Dec {
5456
dec := new(sdk.Dec)
5557
err := dec.Unmarshal(b)
5658
if err != nil {
57-
panic(err)
59+
panic(fmt.Errorf("%w %s", err, HumanizeBytes(b)))
5860
}
5961
return *dec
6062
}

0 commit comments

Comments
 (0)