Skip to content

Commit

Permalink
Better errors
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Broadhurst <[email protected]>
  • Loading branch information
peterbroadhurst committed Aug 27, 2024
1 parent b759d22 commit 2fe278d
Show file tree
Hide file tree
Showing 6 changed files with 7 additions and 7 deletions.
1 change: 1 addition & 0 deletions internal/signermsgs/en_error_messges.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,5 @@ var (
MsgInvalidNumberString = ffe("FF22088", "Invalid integer string '%s'")
MsgInvalidIntPrecisionLoss = ffe("FF22089", "String %s cannot be converted to integer without losing precision")
MsgInvalidUint64PrecisionLoss = ffe("FF22090", "String %s cannot be converted to a uint64 without losing precision")
MsgInvalidJSONTypeForBigInt = ffe("FF22091", "JSON parsed '%T' cannot be converted to an integer")
)
2 changes: 1 addition & 1 deletion pkg/ethtypes/hexinteger.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func (h HexInteger) MarshalJSON() ([]byte, error) {
}

func (h *HexInteger) UnmarshalJSON(b []byte) error {
bi, err := UnmarshalBigInt(b)
bi, err := UnmarshalBigInt(context.Background(), b)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ethtypes/hexinteger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestHexIntegerBadType(t *testing.T) {
}`

err := json.Unmarshal([]byte(testData), &testStruct)
assert.Regexp(t, "unable to parse integer", err)
assert.Regexp(t, "FF22091", err)
}

func TestHexIntegerBadJSON(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/ethtypes/hexuint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (h HexUint64) MarshalJSON() ([]byte, error) {
}

func (h *HexUint64) UnmarshalJSON(b []byte) error {
bi, err := UnmarshalBigInt(b)
bi, err := UnmarshalBigInt(context.Background(), b)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ethtypes/hexuint64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestHexUint64BadType(t *testing.T) {
}`

err := json.Unmarshal([]byte(testData), &testStruct)
assert.Regexp(t, "unable to parse integer", err)
assert.Regexp(t, "FF22091", err)
}

func TestHexUint64BadJSON(t *testing.T) {
Expand Down
5 changes: 2 additions & 3 deletions pkg/ethtypes/integer_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"math/big"

"github.com/hyperledger/firefly-common/pkg/i18n"
Expand Down Expand Up @@ -49,7 +48,7 @@ func BigIntegerFromString(ctx context.Context, s string) (*big.Int, error) {
return i, nil
}

func UnmarshalBigInt(b []byte) (*big.Int, error) {
func UnmarshalBigInt(ctx context.Context, b []byte) (*big.Int, error) {
var i interface{}
d := json.NewDecoder(bytes.NewReader(b))
d.UseNumber()
Expand All @@ -63,6 +62,6 @@ func UnmarshalBigInt(b []byte) (*big.Int, error) {
case string:
return BigIntegerFromString(context.Background(), i)
default:
return nil, fmt.Errorf("unable to parse integer from type %T", i)
return nil, i18n.NewError(ctx, signermsgs.MsgInvalidJSONTypeForBigInt, i)
}
}

0 comments on commit 2fe278d

Please sign in to comment.