From 71f257457db8bbd51094a809ea984b5f6dbe1d2c Mon Sep 17 00:00:00 2001 From: Confucian Date: Wed, 23 Oct 2024 17:30:16 +0800 Subject: [PATCH] chore: improve custom error formatting in revert messages --- internal/ethapi/errors.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/ethapi/errors.go b/internal/ethapi/errors.go index 54f6bd80d9..f96d055ba9 100644 --- a/internal/ethapi/errors.go +++ b/internal/ethapi/errors.go @@ -51,7 +51,7 @@ func newRevertError(revert []byte) *revertError { if errUnpack == nil { err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, reason) } else { - err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, hexutil.Encode(revert)) + err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, formatRevertError(revert)) } return &revertError{ error: err, @@ -59,6 +59,18 @@ func newRevertError(revert []byte) *revertError { } } +// formatRevertError formats the revert data into a custom error message +func formatRevertError(revert []byte) string { + if len(revert) >= 4 { + selector := revert[:4] + params := revert[4:] + return fmt.Sprintf("custom error %s: %s", + hexutil.Encode(selector), + hexutil.Encode(params)) + } + return hexutil.Encode(revert) +} + // TxIndexingError is an API error that indicates the transaction indexing is not // fully finished yet with JSON error code and a binary data blob. type TxIndexingError struct{}