diff options
author | Felix Lange <fjl@twurst.com> | 2017-06-23 16:50:49 +0800 |
---|---|---|
committer | Felix Lange <fjl@twurst.com> | 2017-06-27 19:15:12 +0800 |
commit | 4a741df757cf6b1d1b4c0e8c875c34b650f46167 (patch) | |
tree | 057d995a3847f9f983a4dcdf41cf3bc9fb9b82a2 /common/types.go | |
parent | b664bedcf21f30b54af542e0b4ba4e24c01968fd (diff) | |
download | dexon-4a741df757cf6b1d1b4c0e8c875c34b650f46167.tar dexon-4a741df757cf6b1d1b4c0e8c875c34b650f46167.tar.gz dexon-4a741df757cf6b1d1b4c0e8c875c34b650f46167.tar.bz2 dexon-4a741df757cf6b1d1b4c0e8c875c34b650f46167.tar.lz dexon-4a741df757cf6b1d1b4c0e8c875c34b650f46167.tar.xz dexon-4a741df757cf6b1d1b4c0e8c875c34b650f46167.tar.zst dexon-4a741df757cf6b1d1b4c0e8c875c34b650f46167.zip |
common/hexutil: wrap errors in json.UnmarshalTypeError
This adds type and struct field context to error messages.
Instead of "hex string of odd length" users will now see "json: cannot
unmarshal hex string of odd length into Go struct field SendTxArgs.from
of type common.Address".
Diffstat (limited to 'common/types.go')
-rw-r--r-- | common/types.go | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/common/types.go b/common/types.go index 05288bf46..803726634 100644 --- a/common/types.go +++ b/common/types.go @@ -31,6 +31,11 @@ const ( AddressLength = 20 ) +var ( + hashT = reflect.TypeOf(Hash{}) + addressT = reflect.TypeOf(Address{}) +) + // Hash represents the 32 byte Keccak256 hash of arbitrary data. type Hash [HashLength]byte @@ -72,6 +77,11 @@ func (h *Hash) UnmarshalText(input []byte) error { return hexutil.UnmarshalFixedText("Hash", input, h[:]) } +// UnmarshalJSON parses a hash in hex syntax. +func (h *Hash) UnmarshalJSON(input []byte) error { + return hexutil.UnmarshalFixedJSON(hashT, input, h[:]) +} + // MarshalText returns the hex representation of h. func (h Hash) MarshalText() ([]byte, error) { return hexutil.Bytes(h[:]).MarshalText() @@ -194,6 +204,11 @@ func (a *Address) UnmarshalText(input []byte) error { return hexutil.UnmarshalFixedText("Address", input, a[:]) } +// UnmarshalJSON parses a hash in hex syntax. +func (a *Address) UnmarshalJSON(input []byte) error { + return hexutil.UnmarshalFixedJSON(addressT, input, a[:]) +} + // UnprefixedHash allows marshaling an Address without 0x prefix. type UnprefixedAddress Address |