-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy patherrors.go
More file actions
105 lines (84 loc) · 3.44 KB
/
errors.go
File metadata and controls
105 lines (84 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package errors
import (
"errors"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common/hexutil"
gethVM "github.com/ethereum/go-ethereum/core/vm"
)
var (
// API specific errors
ErrEndpointNotSupported = errors.New("endpoint is not supported")
ErrRateLimit = errors.New("limit of requests per second reached")
ErrIndexOnlyMode = errors.New("transaction submission not allowed in index-only mode")
ErrExceedLogQueryLimit = errors.New("exceed max addresses or topics per search position")
// General errors
ErrInvalid = errors.New("invalid")
ErrRecoverable = errors.New("recoverable")
ErrDisconnected = NewRecoverableError(errors.New("disconnected"))
ErrMissingBlock = errors.New("missing block")
ErrMissingTransactions = errors.New("missing transactions")
// Transaction errors
ErrFailedTransaction = errors.New("failed transaction")
ErrInvalidTransaction = fmt.Errorf("%w: %w", ErrInvalid, ErrFailedTransaction)
// Storage errors
// ErrStorageNotInitialized indicates storage instance was not correctly initialized and contains empty required values.
ErrStorageNotInitialized = errors.New("storage not initialized")
// ErrEntityNotFound indicates the resource does not exist.
ErrEntityNotFound = errors.New("entity not found")
// ErrInvalidBlockRange indicates that the block range provided as start and end height is invalid.
ErrInvalidBlockRange = fmt.Errorf("%w %w", ErrInvalid, errors.New("block height range"))
// ErrHeightOutOfRange indicates the requested height is out of available range
ErrHeightOutOfRange = fmt.Errorf("%w %w", ErrInvalid, errors.New("height not in available range"))
)
func NewEndpointNotSupportedError(endpoint string) error {
return fmt.Errorf("%w: %s", ErrEndpointNotSupported, endpoint)
}
func NewHeightOutOfRangeError(height uint64) error {
return fmt.Errorf("%w: %d", ErrHeightOutOfRange, height)
}
func NewFailedTransactionError(reason string) error {
return fmt.Errorf("%w: %w", ErrFailedTransaction, errors.New(reason))
}
func NewInvalidTransactionError(err error) error {
return fmt.Errorf("%w: %w", ErrInvalidTransaction, err)
}
func NewTxGasPriceTooLowError(gasPrice *big.Int) error {
return NewInvalidTransactionError(fmt.Errorf(
"the minimum accepted gas price for transactions is: %d",
gasPrice,
))
}
func NewRecoverableError(err error) error {
return fmt.Errorf("%w: %w", ErrRecoverable, err)
}
// RevertError is an API error that encompasses an EVM revert with JSON error
// code and a binary data blob.
// We need this custom error type defined because the Geth server implementation
// expects this type when serialising the error API response.
type RevertError struct {
error
Reason string // revert reason hex encoded
}
// 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 {
return 3
}
// ErrorData returns the hex encoded revert reason.
func (e *RevertError) ErrorData() any {
return e.Reason
}
// NewRevertError creates a revertError instance with the provided revert data.
func NewRevertError(revert []byte) *RevertError {
err := gethVM.ErrExecutionReverted
reason, errUnpack := abi.UnpackRevert(revert)
if errUnpack == nil {
err = fmt.Errorf("%w: %v", gethVM.ErrExecutionReverted, reason)
}
return &RevertError{
error: err,
Reason: hexutil.Encode(revert),
}
}