github.1485827954.workers.dev/ethereum/go-ethereum@v1.14.3/internal/ethapi/errors.go (about)

     1  // Copyright 2024 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package ethapi
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/ethereum/go-ethereum/accounts/abi"
    23  	"github.com/ethereum/go-ethereum/common/hexutil"
    24  	"github.com/ethereum/go-ethereum/core/vm"
    25  )
    26  
    27  // revertError is an API error that encompasses an EVM revert with JSON error
    28  // code and a binary data blob.
    29  type revertError struct {
    30  	error
    31  	reason string // revert reason hex encoded
    32  }
    33  
    34  // ErrorCode returns the JSON error code for a revert.
    35  // See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
    36  func (e *revertError) ErrorCode() int {
    37  	return 3
    38  }
    39  
    40  // ErrorData returns the hex encoded revert reason.
    41  func (e *revertError) ErrorData() interface{} {
    42  	return e.reason
    43  }
    44  
    45  // newRevertError creates a revertError instance with the provided revert data.
    46  func newRevertError(revert []byte) *revertError {
    47  	err := vm.ErrExecutionReverted
    48  
    49  	reason, errUnpack := abi.UnpackRevert(revert)
    50  	if errUnpack == nil {
    51  		err = fmt.Errorf("%w: %v", vm.ErrExecutionReverted, reason)
    52  	}
    53  	return &revertError{
    54  		error:  err,
    55  		reason: hexutil.Encode(revert),
    56  	}
    57  }
    58  
    59  // TxIndexingError is an API error that indicates the transaction indexing is not
    60  // fully finished yet with JSON error code and a binary data blob.
    61  type TxIndexingError struct{}
    62  
    63  // NewTxIndexingError creates a TxIndexingError instance.
    64  func NewTxIndexingError() *TxIndexingError { return &TxIndexingError{} }
    65  
    66  // Error implement error interface, returning the error message.
    67  func (e *TxIndexingError) Error() string {
    68  	return "transaction indexing is in progress"
    69  }
    70  
    71  // ErrorCode returns the JSON error code for a revert.
    72  // See: https://github.com/ethereum/wiki/wiki/JSON-RPC-Error-Codes-Improvement-Proposal
    73  func (e *TxIndexingError) ErrorCode() int {
    74  	return -32000 // to be decided
    75  }
    76  
    77  // ErrorData returns the hex encoded revert reason.
    78  func (e *TxIndexingError) ErrorData() interface{} { return "transaction indexing is in progress" }