chore: improve custom error formatting in revert messages

This commit is contained in:
Confucian 2024-10-23 17:30:16 +08:00
parent 3ef918538e
commit 71f257457d

View file

@ -51,7 +51,7 @@ func newRevertError(revert []byte) *revertError {
if errUnpack == nil { if errUnpack == nil {
err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, reason) err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, reason)
} else { } else {
err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, hexutil.Encode(revert)) err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, formatRevertError(revert))
} }
return &revertError{ return &revertError{
error: err, 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 // TxIndexingError is an API error that indicates the transaction indexing is not
// fully finished yet with JSON error code and a binary data blob. // fully finished yet with JSON error code and a binary data blob.
type TxIndexingError struct{} type TxIndexingError struct{}