From b7ce9444e64858a216e4d05c8de32a73ad238362 Mon Sep 17 00:00:00 2001 From: thiagodeev Date: Wed, 6 Nov 2024 15:29:29 -0300 Subject: [PATCH] Fixes failing tests and improves Error() resp --- account/account_test.go | 7 +++++-- rpc/call_test.go | 7 +++++-- rpc/contract_test.go | 7 +++++-- rpc/errors.go | 9 +++++---- rpc/write_test.go | 10 ++++++---- utils/data.go | 14 -------------- 6 files changed, 26 insertions(+), 28 deletions(-) diff --git a/account/account_test.go b/account/account_test.go index db01893c..439e4ea7 100644 --- a/account/account_test.go +++ b/account/account_test.go @@ -1067,7 +1067,7 @@ func TestWaitForTransactionReceipt(t *testing.T) { type testSetType struct { Timeout int Hash *felt.Felt - ExpectedErr error + ExpectedErr *rpc.RPCError ExpectedReceipt rpc.TransactionReceipt } testSet := map[string][]testSetType{ @@ -1087,7 +1087,10 @@ func TestWaitForTransactionReceipt(t *testing.T) { resp, err := acnt.WaitForTransactionReceipt(ctx, test.Hash, 1*time.Second) if test.ExpectedErr != nil { - require.Equal(t, test.ExpectedErr.Error(), err.Error()) + rpcErr, ok := err.(*rpc.RPCError) + require.True(t, ok) + require.Equal(t, test.ExpectedErr.Code, rpcErr.Code) + require.Equal(t, test.ExpectedErr.Message, rpcErr.Message) } else { require.Equal(t, test.ExpectedReceipt.ExecutionStatus, (*resp).ExecutionStatus) } diff --git a/rpc/call_test.go b/rpc/call_test.go index a1823ec2..94f601b2 100644 --- a/rpc/call_test.go +++ b/rpc/call_test.go @@ -30,7 +30,7 @@ func TestCall(t *testing.T) { FunctionCall FunctionCall BlockID BlockID ExpectedPatternResult *felt.Felt - ExpectedError error + ExpectedError *RPCError } testSet := map[string][]testSetType{ "devnet": { @@ -111,7 +111,10 @@ func TestCall(t *testing.T) { require := require.New(t) output, err := testConfig.provider.Call(context.Background(), FunctionCall(test.FunctionCall), test.BlockID) if test.ExpectedError != nil { - require.EqualError(test.ExpectedError, err.Error()) + rpcErr, ok := err.(*RPCError) + require.True(ok) + require.Equal(test.ExpectedError.Code, rpcErr.Code) + require.Equal(test.ExpectedError.Message, rpcErr.Message) } else { require.NoError(err) require.NotEmpty(output, "should return an output") diff --git a/rpc/contract_test.go b/rpc/contract_test.go index 7c0a23c2..029bfce9 100644 --- a/rpc/contract_test.go +++ b/rpc/contract_test.go @@ -404,7 +404,7 @@ func TestEstimateMessageFee(t *testing.T) { MsgFromL1 BlockID ExpectedFeeEst *FeeEstimation - ExpectedError error + ExpectedError *RPCError } // https://sepolia.voyager.online/message/0x273f4e20fc522098a60099e5872ab3deeb7fb8321a03dadbd866ac90b7268361 @@ -470,7 +470,10 @@ func TestEstimateMessageFee(t *testing.T) { for _, test := range testSet { resp, err := testConfig.provider.EstimateMessageFee(context.Background(), test.MsgFromL1, test.BlockID) if err != nil { - require.EqualError(t, test.ExpectedError, err.Error()) + rpcErr, ok := err.(*RPCError) + require.True(t, ok) + require.Equal(t, test.ExpectedError.Code, rpcErr.Code) + require.Equal(t, test.ExpectedError.Message, rpcErr.Message) } else { require.Exactly(t, test.ExpectedFeeEst, resp) } diff --git a/rpc/errors.go b/rpc/errors.go index 999e81ee..1b4c28b1 100644 --- a/rpc/errors.go +++ b/rpc/errors.go @@ -2,6 +2,7 @@ package rpc import ( "encoding/json" + "fmt" "github.com/NethermindEth/juno/core/felt" ) @@ -76,10 +77,10 @@ type RPCError struct { } func (e RPCError) Error() string { - if e.Data == nil { + if e.Data == nil || e.Data.Message == "" { return e.Message } - return e.Message + e.Data.Message + return e.Message + ": " + e.Data.Message } type RPCData struct { @@ -113,7 +114,7 @@ func (rpcData *RPCData) UnmarshalJSON(data []byte) error { return nil } - return nil + return fmt.Errorf("failed to unmarshal RPCData") } func (rpcData RPCData) MarshalJSON() ([]byte, error) { @@ -295,7 +296,7 @@ func (contractEx *ContractExecutionError) UnmarshalJSON(data []byte) error { return nil } - return nil + return fmt.Errorf("failed to unmarshal ContractExecutionError") } type ContractExecutionErrorStruct struct { diff --git a/rpc/write_test.go b/rpc/write_test.go index 1dcb3b3f..37e88406 100644 --- a/rpc/write_test.go +++ b/rpc/write_test.go @@ -2,7 +2,6 @@ package rpc import ( "context" - "errors" "testing" "github.com/NethermindEth/juno/core/felt" @@ -17,7 +16,7 @@ func TestDeclareTransaction(t *testing.T) { type testSetType struct { DeclareTx BroadcastDeclareTxnType ExpectedResp AddDeclareTransactionResponse - ExpectedError error + ExpectedError *RPCError } testSet := map[string][]testSetType{ "devnet": {}, @@ -40,7 +39,7 @@ func TestDeclareTransaction(t *testing.T) { DeclareTx: BroadcastDeclareTxnV1{}, ExpectedResp: AddDeclareTransactionResponse{ TransactionHash: utils.TestHexToFelt(t, "0x55b094dc5c84c2042e067824f82da90988674314d37e45cb0032aca33d6e0b9")}, - ExpectedError: errors.New("Invalid Params"), + ExpectedError: &RPCError{Code: InvalidParams, Message: "Invalid Params"}, }, }, }[testEnv] @@ -48,7 +47,10 @@ func TestDeclareTransaction(t *testing.T) { for _, test := range testSet { resp, err := testConfig.provider.AddDeclareTransaction(context.Background(), test.DeclareTx) if err != nil { - require.Equal(t, test.ExpectedError.Error(), err.Error()) + rpcErr, ok := err.(*RPCError) + require.True(t, ok) + require.Equal(t, test.ExpectedError.Code, rpcErr.Code) + require.Equal(t, test.ExpectedError.Message, rpcErr.Message) } else { require.Equal(t, (*resp.TransactionHash).String(), (*test.ExpectedResp.TransactionHash).String()) } diff --git a/utils/data.go b/utils/data.go index 495df727..2f94bcef 100644 --- a/utils/data.go +++ b/utils/data.go @@ -2,7 +2,6 @@ package utils import ( "encoding/json" - "fmt" ) func UnwrapJSON(data map[string]interface{}, tag string) (map[string]interface{}, error) { @@ -19,16 +18,3 @@ func UnwrapJSON(data map[string]interface{}, tag string) (map[string]interface{} } return data, nil } - -func GetTypedFieldFromJSONMap[T any](data map[string]json.RawMessage, tag string) (resp T, err error) { - rawResp, ok := data[tag] - if !ok { - return resp, fmt.Errorf("missing '%s' field in json object", tag) - } - - if err := json.Unmarshal(rawResp, &resp); err != nil { - return resp, err - } - - return resp, nil -}