Skip to content

Commit

Permalink
implement error codec interface
Browse files Browse the repository at this point in the history
  • Loading branch information
akaladarshi committed Oct 22, 2024
1 parent a092883 commit 0ba5a70
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 45 deletions.
54 changes: 39 additions & 15 deletions api/api_errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"

"github.com/filecoin-project/go-jsonrpc"
"golang.org/x/xerrors"
)

const executionRevertedDefaultMsg = "execution reverted"
Expand Down Expand Up @@ -43,14 +44,15 @@ var (
// should back off and try again later.
ErrF3NotReady = errF3NotReady{}

_ error = (*ErrOutOfGas)(nil)
_ error = (*ErrActorNotFound)(nil)
_ error = (*errF3Disabled)(nil)
_ error = (*errF3ParticipationTicketInvalid)(nil)
_ error = (*errF3ParticipationTicketExpired)(nil)
_ error = (*errF3ParticipationIssuerMismatch)(nil)
_ error = (*errF3NotReady)(nil)
_ error = (*ErrExecutionRevertedWithData)(nil)
_ error = (*ErrOutOfGas)(nil)
_ error = (*ErrActorNotFound)(nil)
_ error = (*errF3Disabled)(nil)
_ error = (*errF3ParticipationTicketInvalid)(nil)
_ error = (*errF3ParticipationTicketExpired)(nil)
_ error = (*errF3ParticipationIssuerMismatch)(nil)
_ error = (*errF3NotReady)(nil)
_ error = (*ErrExecutionRevertedWithData)(nil)
_ jsonrpc.ErrorCodec = (*ErrExecutionRevertedWithData)(nil)
)

func init() {
Expand Down Expand Up @@ -117,20 +119,42 @@ type errF3NotReady struct{}
func (errF3NotReady) Error() string { return "f3 isn't yet ready to participate" }

type ErrExecutionRevertedWithData struct {
message string
data string
Message string
Data string
}

// Error returns the error message.
func (e *ErrExecutionRevertedWithData) Error() string { return e.message }
func (e *ErrExecutionRevertedWithData) Error() string { return e.Message }

// ErrorData returns the error data.
func (e *ErrExecutionRevertedWithData) ErrorData() interface{} { return e.data }
// FromJSONRPCError converts a JSONRPCError to ErrExecutionRevertedWithData.
func (e *ErrExecutionRevertedWithData) FromJSONRPCError(jerr jsonrpc.JSONRPCError) error {
if jerr.Code != EExecutionRevertedWithData {
return nil
}

data, ok := jerr.Data.(string)
if !ok {
return xerrors.Errorf("expected string data in execution reverted error, got %T", jerr.Data)
}

e.Message = jerr.Message
e.Data = data
return nil
}

// ToJSONRPCError converts ErrExecutionRevertedWithData to a JSONRPCError.
func (e *ErrExecutionRevertedWithData) ToJSONRPCError() (jsonrpc.JSONRPCError, error) {
return jsonrpc.JSONRPCError{
Code: EExecutionRevertedWithData,
Message: e.Message,
Data: e.Data,
}, nil
}

// NewErrExecutionRevertedWithData creates a new ErrExecutionRevertedWithData.
func NewErrExecutionRevertedWithData(data string) *ErrExecutionRevertedWithData {
return &ErrExecutionRevertedWithData{
message: executionRevertedDefaultMsg,
data: data,
Message: executionRevertedDefaultMsg,
Data: data,
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ replace github.com/filecoin-project/test-vectors => ./extern/test-vectors // pro

replace github.com/filecoin-project/filecoin-ffi => ./extern/filecoin-ffi // provided via a git submodule

replace github.com/filecoin-project/go-jsonrpc => github.com/virajbhartiya/go-jsonrpc v0.0.0-20241016130229-4e33c9f29a6a
replace github.com/filecoin-project/go-jsonrpc => github.com/virajbhartiya/go-jsonrpc v0.0.0-20241022104347-ef506b1630b0

require (
contrib.go.opencensus.io/exporter/prometheus v0.4.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1279,8 +1279,8 @@ github.com/valyala/fasttemplate v1.0.1 h1:tY9CJiPnMXf1ERmG2EyK7gNUd+c6RKGD0IfU8W
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/viant/assertly v0.4.8/go.mod h1:aGifi++jvCrUaklKEKT0BU95igDNaqkvz+49uaYMPRU=
github.com/viant/toolbox v0.24.0/go.mod h1:OxMCG57V0PXuIP2HNQrtJf2CjqdmbrOx5EkMILuUhzM=
github.com/virajbhartiya/go-jsonrpc v0.0.0-20241016130229-4e33c9f29a6a h1:7iKYazcDTV1Gs3PZ3KM36ecW86eHXwf1hXhjt+obUt8=
github.com/virajbhartiya/go-jsonrpc v0.0.0-20241016130229-4e33c9f29a6a/go.mod h1:lAUpS8BSVtKaA8+/CFUMA5dokMiSM7n0ehf8bHOFdpE=
github.com/virajbhartiya/go-jsonrpc v0.0.0-20241022104347-ef506b1630b0 h1:V+4+u8WmKprL4cvaYhy8S5DzMBp4JAt3qm9ZuBq8r5E=
github.com/virajbhartiya/go-jsonrpc v0.0.0-20241022104347-ef506b1630b0/go.mod h1:lAUpS8BSVtKaA8+/CFUMA5dokMiSM7n0ehf8bHOFdpE=
github.com/warpfork/go-testmark v0.12.1 h1:rMgCpJfwy1sJ50x0M0NgyphxYYPMOODIJHhsXyEHU0s=
github.com/warpfork/go-testmark v0.12.1/go.mod h1:kHwy7wfvGSPh1rQJYKayD4AbtNaeyZdcGi9tNJTaa5Y=
github.com/warpfork/go-wish v0.0.0-20180510122957-5ad1f5abf436/go.mod h1:x6AKhvSSexNrVSrViXSHUEbICjmGXhtgABaHIySUSGw=
Expand Down
38 changes: 11 additions & 27 deletions itests/fevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"os"
"testing"
Expand All @@ -16,7 +15,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-jsonrpc"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
builtintypes "github.com/filecoin-project/go-state-types/builtin"
Expand Down Expand Up @@ -1043,19 +1041,13 @@ func TestFEVMErrorParsing(t *testing.T) {
t.Run(sig, func(t *testing.T) {
entryPoint := kit.CalcFuncSignature(sig)
t.Run("EthCall", func(t *testing.T) {
_, err := e.EthCall(ctx, ethtypes.EthCall{
_, err = e.EthCall(ctx, ethtypes.EthCall{
To: &contractAddrEth,
Data: entryPoint,
}, ethtypes.NewEthBlockNumberOrHashFromPredefined("latest"))
require.Error(t, err)

var dataErr jsonrpc.ErrorWithData
if errors.As(err, &dataErr) {
errData := dataErr.ErrorData()
require.Contains(t, errData, expected, "Error data should contain the expected error")
} else {
t.Fatalf("Expected error to implement jsonrpc.ErrorWithData")
}
require.Contains(t, err.Error(), expected, "Error data should contain the expected error")
})
t.Run("EthEstimateGas", func(t *testing.T) {
gasParams, err := json.Marshal(ethtypes.EthEstimateGasParams{Tx: ethtypes.EthCall{
Expand All @@ -1066,14 +1058,7 @@ func TestFEVMErrorParsing(t *testing.T) {

_, err = e.EthEstimateGas(ctx, gasParams)
require.Error(t, err)

var dataErr jsonrpc.ErrorWithData
if errors.As(err, &dataErr) {
errData := dataErr.ErrorData()
require.Contains(t, errData, expected, "Error data should contain the expected error")
} else {
t.Fatalf("Expected error to implement jsonrpc.ErrorWithData")
}
require.Contains(t, err.Error(), expected, "Error data should contain the expected error")
})
})
}
Expand Down Expand Up @@ -1490,13 +1475,12 @@ func TestEthCall(t *testing.T) {
_, err = client.EthCall(ctx, callParams, ethtypes.NewEthBlockNumberOrHashFromNumber(latestBlock))
require.Error(t, err)

var dataErr jsonrpc.ErrorWithData
ok := errors.As(err, &dataErr)
require.True(t, ok, "Expected error to implement jsonrpc.DataError")
require.Contains(t, dataErr.Error(), "execution reverted", "Expected 'execution reverted' message")
var dataErr *api.ErrExecutionRevertedWithData
require.ErrorAs(t, err, &dataErr, "Expected error to be ErrExecutionRevertedWithData")
require.Contains(t, dataErr.Message, "execution reverted", "Expected 'execution reverted' message")

// Get the error data
require.Equal(t, dataErr.ErrorData(), "DivideByZero()", "Expected error data to contain 'DivideByZero()'")
require.Equal(t, dataErr.Data, "DivideByZero()", "Expected error data to contain 'DivideByZero()'")
})
}

Expand Down Expand Up @@ -1543,10 +1527,10 @@ func TestEthEstimateGas(t *testing.T) {

if tc.expectedError != "" {
require.Error(t, err)
var dataErr jsonrpc.ErrorWithData
require.ErrorAs(t, err, &dataErr, "Expected error to implement jsonrpc.DataError")
require.Equal(t, tc.expectedErrMsg, dataErr.Error())
require.Contains(t, tc.expectedError, dataErr.ErrorData())
var dataErr *api.ErrExecutionRevertedWithData
require.ErrorAs(t, err, &dataErr, "Expected error to be ErrExecutionRevertedWithData")
require.Equal(t, tc.expectedErrMsg, dataErr.Message)
require.Contains(t, tc.expectedError, dataErr.Data)
}
})
}
Expand Down

0 comments on commit 0ba5a70

Please sign in to comment.