Skip to content

Commit

Permalink
change the way to handle default errors (Error and Panic)
Browse files Browse the repository at this point in the history
Signed-off-by: Masanori Yoshida <[email protected]>
  • Loading branch information
siburu committed Apr 26, 2024
1 parent f6fc68e commit d972fff
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
29 changes: 23 additions & 6 deletions pkg/relay/ethereum/error_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ import (

type ErrorRepository map[[4]byte]abi.Error

func defaultErrorABIs() ([]abi.Error, error) {
strT, err := abi.NewType("string", "", nil)
if err != nil {
return nil, fmt.Errorf("failed to create string type: %v", err)
}
uintT, err := abi.NewType("uint256", "", nil)
if err != nil {
return nil, fmt.Errorf("failed to create uint256 type: %v", err)
}

return []abi.Error{
abi.NewError("Error", []abi.Argument{{Name: "desc", Type: strT}}),
abi.NewError("Panic", []abi.Argument{{Name: "reason", Type: uintT}}),
}, nil
}

func CreateErrorRepository(abiPaths []string) (ErrorRepository, error) {
var errABIs []abi.Error

Expand Down Expand Up @@ -46,6 +62,13 @@ func CreateErrorRepository(abiPaths []string) (ErrorRepository, error) {
}

func NewErrorRepository(errABIs []abi.Error) (ErrorRepository, error) {
defaultErrABIs, err := defaultErrorABIs()
if err != nil {
return nil, fmt.Errorf("failed to create default error ABIs: %v", err)
}

errABIs = append(errABIs, defaultErrABIs...)

repo := make(ErrorRepository)
for _, errABI := range errABIs {
if err := repo.Add(errABI); err != nil {
Expand Down Expand Up @@ -102,12 +125,6 @@ func errorToJSON(errVal interface{}, errABI abi.Error) (string, error) {
}

func (r ErrorRepository) ParseError(errorData []byte) (string, error) {
// handle Error(string) and Panic(uint256)
if revertReason, err := abi.UnpackRevert(errorData); err == nil {
return revertReason, nil
}

// handle custom error
errABI, err := r.Get(errorData)
if err != nil {
return "", fmt.Errorf("failed to find error ABI: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/relay/ethereum/error_parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestRevertReasonParserDefault(t *testing.T) {
common.FromHex("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"),
)
require.NoError(t, err)
require.Equal(t, "Not enough Ether provided.", revertReason)
require.Equal(t, `Error{"desc":"Not enough Ether provided."}`, revertReason)
}

func TestRevertReasonParserAddedCustomError(t *testing.T) {
Expand All @@ -37,7 +37,7 @@ func TestRevertReasonParserAddedCustomError(t *testing.T) {
common.FromHex("0x08c379a00000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001a4e6f7420656e6f7567682045746865722070726f76696465642e000000000000"),
)
require.NoError(t, err)
require.Equal(t, "Not enough Ether provided.", revertReason)
require.Equal(t, `Error{"desc":"Not enough Ether provided."}`, revertReason)
}

func TestRevertReasonParserCustomError(t *testing.T) {
Expand Down

0 comments on commit d972fff

Please sign in to comment.