Skip to content

Commit

Permalink
Added missing error variants to handle error codes returned by the no…
Browse files Browse the repository at this point in the history
…de. (#394)

* Added missing error variants to handle error codes returned by the node.

* Removing catch-all statement
  • Loading branch information
zajko authored Jan 21, 2025
1 parent de43fbc commit 3b8c1fb
Showing 1 changed file with 243 additions and 5 deletions.
248 changes: 243 additions & 5 deletions rpc_sidecar/src/node_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,30 @@ pub enum InvalidTransactionOrDeploy {
/// Invalid transaction lane
#[error("invalid transaction lane")]
InvalidTransactionInvalidTransactionLane,
#[error("expected named arguments")]
ExpectedNamedArguments,
#[error("invalid transaction runtime")]
InvalidTransactionRuntime,
#[error("couldn't associate a transaction lane with the transaction")]
InvalidTransactionNoWasmLaneMatches,
#[error("entry point must be 'call'")]
InvalidTransactionEntryPointMustBeCall,
#[error("One of the payloads field cannot be deserialized")]
InvalidTransactionCannotDeserializeField,
#[error("Can't calculate hash of the payload fields")]
InvalidTransactionCannotCalculateFieldsHash,
#[error("Unexpected fields in payload")]
InvalidTransactionUnexpectedFields,
#[error("expected bytes arguments")]
InvalidTransactionExpectedBytesArguments,
#[error("Missing seed field in transaction")]
InvalidTransactionMissingSeed,
#[error("Pricing mode not supported")]
PricingModeNotSupported,
#[error("Gas limit not supported")]
InvalidDeployGasLimitNotSupported,
#[error("Invalid runtime for Transaction::Deploy")]
InvalidDeployInvalidRuntime,
}

impl From<ErrorCode> for InvalidTransactionOrDeploy {
Expand Down Expand Up @@ -634,6 +658,30 @@ impl From<ErrorCode> for InvalidTransactionOrDeploy {
ErrorCode::InvalidTransactionOrDeployUnspecified => {
Self::TransactionOrDeployUnspecified
}
ErrorCode::ExpectedNamedArguments => Self::ExpectedNamedArguments,
ErrorCode::InvalidTransactionRuntime => Self::InvalidTransactionRuntime,
ErrorCode::InvalidTransactionNoWasmLaneMatches => {
Self::InvalidTransactionNoWasmLaneMatches
}
ErrorCode::InvalidTransactionEntryPointMustBeCall => {
Self::InvalidTransactionEntryPointMustBeCall
}
ErrorCode::InvalidTransactionCannotDeserializeField => {
Self::InvalidTransactionCannotDeserializeField
}
ErrorCode::InvalidTransactionCannotCalculateFieldsHash => {
Self::InvalidTransactionCannotCalculateFieldsHash
}
ErrorCode::InvalidTransactionUnexpectedFields => {
Self::InvalidTransactionUnexpectedFields
}
ErrorCode::InvalidTransactionExpectedBytesArguments => {
Self::InvalidTransactionExpectedBytesArguments
}
ErrorCode::InvalidTransactionMissingSeed => Self::InvalidTransactionMissingSeed,
ErrorCode::PricingModeNotSupported => Self::PricingModeNotSupported,
ErrorCode::InvalidDeployGasLimitNotSupported => Self::InvalidDeployGasLimitNotSupported,
ErrorCode::InvalidDeployInvalidRuntime => Self::InvalidDeployInvalidRuntime,
_ => Self::TransactionOrDeployUnspecified,
}
}
Expand Down Expand Up @@ -687,6 +735,36 @@ pub enum Error {
BinaryProtocolVersionMismatch,
#[error("request was throttled by the node")]
RequestThrottled,
#[error("malformed information request")]
MalformedInformationRequest,
#[error("malformed version bytes in header of binary request")]
MalformedBinaryVersion,
#[error("malformed protocol version")]
MalformedProtocolVersion,
#[error("malformed transfer record key")]
TransferRecordMalformedKey,
#[error("malformed header of binary request")]
MalformedBinaryRequestHeader,
#[error("malformed binary request")]
MalformedBinaryRequest,
#[error("not found")]
NotFound,
#[error("header has unsupported protocol version")]
HeaderHasUnsupportedProtocolVersion,
#[error("node reported internal error")]
InternalNodeError,
#[error("bad request")]
BadRequest,
#[error("unsupported request")]
UnsupportedRequest,
#[error("dictionary URef not found")]
DictionaryURefNotFound,
#[error("no complete blocks")]
NoCompleteBlocks,
#[error("gas price tolerance too low")]
GasPriceToleranceTooLow,
#[error("received v1 transaction for speculative execution")]
ReceivedV1Transaction,
}

impl Error {
Expand Down Expand Up @@ -768,16 +846,46 @@ impl Error {
| ErrorCode::InvalidTransactionEntryPointCannotBeCall
| ErrorCode::InvalidTransactionInvalidTransactionLane
| ErrorCode::InvalidTransactionUnspecified
| ErrorCode::InvalidTransactionOrDeployUnspecified),
| ErrorCode::InvalidTransactionOrDeployUnspecified
| ErrorCode::ExpectedNamedArguments
| ErrorCode::InvalidTransactionRuntime
| ErrorCode::InvalidTransactionNoWasmLaneMatches
| ErrorCode::InvalidTransactionEntryPointMustBeCall
| ErrorCode::InvalidTransactionCannotDeserializeField
| ErrorCode::InvalidTransactionCannotCalculateFieldsHash
| ErrorCode::InvalidTransactionUnexpectedFields
| ErrorCode::InvalidTransactionExpectedBytesArguments
| ErrorCode::InvalidTransactionMissingSeed
| ErrorCode::PricingModeNotSupported
| ErrorCode::InvalidDeployGasLimitNotSupported
| ErrorCode::InvalidDeployInvalidRuntime),
) => Self::InvalidTransaction(InvalidTransactionOrDeploy::from(err)),
Ok(ErrorCode::RequestThrottled) => Self::RequestThrottled,
Ok(ErrorCode::MalformedInformationRequest) => Self::MalformedInformationRequest,
Ok(ErrorCode::MalformedBinaryVersion) => Self::MalformedBinaryVersion,
Ok(ErrorCode::MalformedProtocolVersion) => Self::MalformedProtocolVersion,
Ok(ErrorCode::TransferRecordMalformedKey) => Self::TransferRecordMalformedKey,
Ok(ErrorCode::MalformedBinaryRequestHeader) => Self::MalformedBinaryRequestHeader,
Ok(ErrorCode::MalformedBinaryRequest) => Self::MalformedBinaryRequest,
Ok(err @ (ErrorCode::WasmPreprocessing | ErrorCode::InvalidItemVariant)) => {
Self::SpecExecutionFailed(err.to_string())
}
Ok(err) => Self::UnexpectedNodeError {
message: err.to_string(),
code,
},
Ok(ErrorCode::NotFound) => Self::NotFound,
Ok(ErrorCode::UnsupportedProtocolVersion) => Self::HeaderHasUnsupportedProtocolVersion,
Ok(ErrorCode::InternalError) => Self::InternalNodeError,
Ok(ErrorCode::BadRequest) => Self::BadRequest,
Ok(ErrorCode::UnsupportedRequest) => Self::UnsupportedRequest,
Ok(ErrorCode::DictionaryURefNotFound) => Self::DictionaryURefNotFound,
Ok(ErrorCode::NoCompleteBlocks) => Self::NoCompleteBlocks,
Ok(ErrorCode::GasPriceToleranceTooLow) => Self::GasPriceToleranceTooLow,
Ok(ErrorCode::ReceivedV1Transaction) => Self::ReceivedV1Transaction,
Ok(ErrorCode::NoError) => {
//This code shouldn't be passed in an error scenario
Self::UnexpectedNodeError {
message: "Received unexpected 'NoError' error code".to_string(),
code: ErrorCode::NoError as u16,
}
}
Err(err) => Self::UnexpectedNodeError {
message: err.to_string(),
code,
Expand Down Expand Up @@ -1641,4 +1749,134 @@ mod tests {
// Expect 'TooManyMismatchResponses' error
assert!(matches!(res, Error::TooManyMismatchedResponses { max } if max == LIMIT));
}

#[test]
fn should_map_errors() {
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidTransactionOrDeployUnspecified as u16),
Error::InvalidTransaction(InvalidTransactionOrDeploy::TransactionOrDeployUnspecified)
));
assert!(matches!(
Error::from_error_code(ErrorCode::ExpectedNamedArguments as u16),
Error::InvalidTransaction(InvalidTransactionOrDeploy::ExpectedNamedArguments)
));
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidTransactionRuntime as u16),
Error::InvalidTransaction(InvalidTransactionOrDeploy::InvalidTransactionRuntime)
));
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidTransactionNoWasmLaneMatches as u16),
Error::InvalidTransaction(
InvalidTransactionOrDeploy::InvalidTransactionNoWasmLaneMatches
)
));
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidTransactionEntryPointMustBeCall as u16),
Error::InvalidTransaction(
InvalidTransactionOrDeploy::InvalidTransactionEntryPointMustBeCall
)
));
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidTransactionCannotDeserializeField as u16),
Error::InvalidTransaction(
InvalidTransactionOrDeploy::InvalidTransactionCannotDeserializeField
)
));
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidTransactionCannotCalculateFieldsHash as u16),
Error::InvalidTransaction(
InvalidTransactionOrDeploy::InvalidTransactionCannotCalculateFieldsHash
)
));
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidTransactionUnexpectedFields as u16),
Error::InvalidTransaction(
InvalidTransactionOrDeploy::InvalidTransactionUnexpectedFields
)
));
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidTransactionExpectedBytesArguments as u16),
Error::InvalidTransaction(
InvalidTransactionOrDeploy::InvalidTransactionExpectedBytesArguments
)
));
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidTransactionMissingSeed as u16),
Error::InvalidTransaction(InvalidTransactionOrDeploy::InvalidTransactionMissingSeed)
));
assert!(matches!(
Error::from_error_code(ErrorCode::PricingModeNotSupported as u16),
Error::InvalidTransaction(InvalidTransactionOrDeploy::PricingModeNotSupported)
));
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidDeployGasLimitNotSupported as u16),
Error::InvalidTransaction(
InvalidTransactionOrDeploy::InvalidDeployGasLimitNotSupported
)
));
assert!(matches!(
Error::from_error_code(ErrorCode::InvalidDeployInvalidRuntime as u16),
Error::InvalidTransaction(InvalidTransactionOrDeploy::InvalidDeployInvalidRuntime)
));
assert!(matches!(
Error::from_error_code(ErrorCode::MalformedInformationRequest as u16),
Error::MalformedInformationRequest
));
assert!(matches!(
Error::from_error_code(ErrorCode::MalformedBinaryVersion as u16),
Error::MalformedBinaryVersion
));
assert!(matches!(
Error::from_error_code(ErrorCode::MalformedProtocolVersion as u16),
Error::MalformedProtocolVersion
));
assert!(matches!(
Error::from_error_code(ErrorCode::TransferRecordMalformedKey as u16),
Error::TransferRecordMalformedKey
));
assert!(matches!(
Error::from_error_code(ErrorCode::MalformedBinaryRequestHeader as u16),
Error::MalformedBinaryRequestHeader
));
assert!(matches!(
Error::from_error_code(ErrorCode::MalformedBinaryRequest as u16),
Error::MalformedBinaryRequest
));
assert!(matches!(
Error::from_error_code(ErrorCode::NotFound as u16),
Error::NotFound
));
assert!(matches!(
Error::from_error_code(ErrorCode::UnsupportedProtocolVersion as u16),
Error::HeaderHasUnsupportedProtocolVersion
));
assert!(matches!(
Error::from_error_code(ErrorCode::InternalError as u16),
Error::InternalNodeError
));
assert!(matches!(
Error::from_error_code(ErrorCode::BadRequest as u16),
Error::BadRequest
));
assert!(matches!(
Error::from_error_code(ErrorCode::UnsupportedRequest as u16),
Error::UnsupportedRequest
));
assert!(matches!(
Error::from_error_code(ErrorCode::DictionaryURefNotFound as u16),
Error::DictionaryURefNotFound
));
assert!(matches!(
Error::from_error_code(ErrorCode::NoCompleteBlocks as u16),
Error::NoCompleteBlocks
));
assert!(matches!(
Error::from_error_code(ErrorCode::GasPriceToleranceTooLow as u16),
Error::GasPriceToleranceTooLow
));
assert!(matches!(
Error::from_error_code(ErrorCode::ReceivedV1Transaction as u16),
Error::ReceivedV1Transaction
));
}
}

0 comments on commit 3b8c1fb

Please sign in to comment.