fix: add unknown block error (#3779)

Co-authored-by: suneal <suneal@soulwalletlabs.com>
This commit is contained in:
suneal
2023-07-14 22:23:37 +08:00
committed by GitHub
parent 715d41dbc7
commit d1610f3df9
3 changed files with 23 additions and 5 deletions

View File

@@ -13,6 +13,9 @@ pub enum EthRpcErrorCode {
/// > If the block is not found, the callee SHOULD raise a JSON-RPC error (the recommended
/// > error code is -32001: Resource not found).
ResourceNotFound,
/// Thrown when querying for `finalized` or `safe` block before the merge transition is
/// finalized, <https://github.com/ethereum/execution-apis/blob/6d17705a875e52c26826124c2a8a15ed542aeca2/src/schemas/block.yaml#L109>
UnknownBlock,
}
impl EthRpcErrorCode {
@@ -23,6 +26,7 @@ impl EthRpcErrorCode {
EthRpcErrorCode::ExecutionError => 3,
EthRpcErrorCode::InvalidInput => -32000,
EthRpcErrorCode::ResourceNotFound => -32001,
EthRpcErrorCode::UnknownBlock => -39001,
}
}
}

View File

@@ -26,6 +26,10 @@ pub enum EthApiError {
PoolError(RpcPoolError),
#[error("Unknown block number")]
UnknownBlockNumber,
/// Thrown when querying for `finalized` or `safe` block before the merge transition is
/// finalized, <https://github.com/ethereum/execution-apis/blob/6d17705a875e52c26826124c2a8a15ed542aeca2/src/schemas/block.yaml#L109>
#[error("Unknown block")]
UnknownSafeOrFinalizedBlock,
#[error("Unknown block or tx index")]
UnknownBlockOrTxIndex,
#[error("Invalid block range")]
@@ -101,6 +105,9 @@ impl From<EthApiError> for ErrorObject<'static> {
EthApiError::UnknownBlockNumber | EthApiError::UnknownBlockOrTxIndex => {
rpc_error_with_code(EthRpcErrorCode::ResourceNotFound.code(), error.to_string())
}
EthApiError::UnknownSafeOrFinalizedBlock => {
rpc_error_with_code(EthRpcErrorCode::UnknownBlock.code(), error.to_string())
}
EthApiError::Unsupported(msg) => internal_rpc_err(msg),
EthApiError::InternalJsTracerError(msg) => internal_rpc_err(msg),
EthApiError::InvalidParams(msg) => invalid_params_rpc_err(msg),
@@ -143,11 +150,12 @@ impl From<reth_interfaces::provider::ProviderError> for EthApiError {
ProviderError::HeaderNotFound(_) |
ProviderError::BlockHashNotFound(_) |
ProviderError::BestBlockNotFound |
ProviderError::FinalizedBlockNotFound |
ProviderError::SafeBlockNotFound |
ProviderError::BlockNumberForTransactionIndexNotFound |
ProviderError::TotalDifficultyNotFound { .. } |
ProviderError::UnknownBlockHash(_) => EthApiError::UnknownBlockNumber,
ProviderError::FinalizedBlockNotFound | ProviderError::SafeBlockNotFound => {
EthApiError::UnknownSafeOrFinalizedBlock
}
err => EthApiError::Internal(err.into()),
}
}