mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-18 18:02:24 +00:00
internal/ethapi: expose revert reason
This commit is contained in:
parent
50a02c49d9
commit
410510ac0c
1 changed files with 11 additions and 9 deletions
|
|
@ -29,10 +29,16 @@ import (
|
|||
// revertError is an API error that encompasses an EVM revert with JSON error
|
||||
// code and a binary data blob.
|
||||
type revertError struct {
|
||||
error
|
||||
reason string // revert reason hex encoded
|
||||
}
|
||||
|
||||
func (e *revertError) Error() string {
|
||||
if e.reason != "" {
|
||||
return fmt.Sprintf("%s: %v", vm.ErrExecutionReverted, e.reason)
|
||||
}
|
||||
return vm.ErrExecutionReverted.Error()
|
||||
}
|
||||
|
||||
// ErrorCode returns the JSON error code for a revert.
|
||||
// See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
|
||||
func (e *revertError) ErrorCode() int {
|
||||
|
|
@ -46,16 +52,12 @@ func (e *revertError) ErrorData() interface{} {
|
|||
|
||||
// newRevertError creates a revertError instance with the provided revert data.
|
||||
func newRevertError(revert []byte) *revertError {
|
||||
var err error
|
||||
reason, errUnpack := abi.UnpackRevert(revert)
|
||||
if errUnpack == nil {
|
||||
err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, reason)
|
||||
} else {
|
||||
err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, hexutil.Encode(revert))
|
||||
reason := hexutil.Encode(revert)
|
||||
if r, errUnpack := abi.UnpackRevert(revert); errUnpack == nil {
|
||||
reason = r
|
||||
}
|
||||
return &revertError{
|
||||
error: err,
|
||||
reason: hexutil.Encode(revert),
|
||||
reason: reason,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue