@@ -37,14 +37,16 @@ func (stringKey) Encode(s string) []byte {
37
37
func (stringKey ) Decode (b []byte ) (int , string ) {
38
38
l := len (b )
39
39
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 )))
41
43
}
42
44
for i , c := range b {
43
45
if c == 0 {
44
46
return i + 1 , string (b [:i ])
45
47
}
46
48
}
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 ) ))
48
50
}
49
51
50
52
type uint64Key struct {}
@@ -60,7 +62,7 @@ func (timeKey) Encode(t time.Time) []byte { return sdk.FormatTimeBytes(t) }
60
62
func (timeKey ) Decode (b []byte ) (int , time.Time ) {
61
63
t , err := sdk .ParseTimeBytes (b )
62
64
if err != nil {
63
- panic (err )
65
+ panic (fmt . Errorf ( "%w %s" , err , HumanizeBytes ( b )) )
64
66
}
65
67
return len (b ), t
66
68
}
@@ -85,7 +87,7 @@ func (v valAddressKeyEncoder) Decode(b []byte) (int, sdk.ValAddress) {
85
87
r , s := StringKeyEncoder .Decode (b )
86
88
valAddr , err := sdk .ValAddressFromBech32 (s )
87
89
if err != nil {
88
- panic (err )
90
+ panic (fmt . Errorf ( "%w %s" , err , HumanizeBytes ( b )) )
89
91
}
90
92
return r , valAddr
91
93
}
@@ -113,7 +115,7 @@ func (consAddressKeyEncoder) Decode(b []byte) (int, sdk.ConsAddress) {
113
115
r , s := StringKeyEncoder .Decode (b )
114
116
consAddr , err := sdk .ConsAddressFromBech32 (s )
115
117
if err != nil {
116
- panic (err )
118
+ panic (fmt . Errorf ( "%w %s" , err , HumanizeBytes ( b )) )
117
119
}
118
120
return r , consAddr
119
121
}
@@ -126,15 +128,37 @@ func (sdkDecKeyEncoder) Stringify(key sdk.Dec) string { return key.String() }
126
128
func (sdkDecKeyEncoder ) Encode (key sdk.Dec ) []byte {
127
129
bz , err := key .Marshal ()
128
130
if err != nil {
129
- panic (fmt .Errorf ("invalid DecKey: %w" , err ))
131
+ panic (fmt .Errorf ("invalid DecKey: %w %s " , err , HumanizeBytes ( bz ) ))
130
132
}
131
133
return bz
132
134
}
133
135
func (sdkDecKeyEncoder ) Decode (b []byte ) (int , sdk.Dec ) {
134
136
var dec sdk.Dec
135
137
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 ) ))
137
139
}
138
140
139
141
return len (b ), dec
140
142
}
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 ("\n bytesAsHex: %x" , bz )
164
+ }
0 commit comments