unc_jsonrpc_primitives/types/
blocks.rs

1use serde_json::Value;
2
3#[derive(thiserror::Error, Debug, serde::Serialize, serde::Deserialize)]
4#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")]
5pub enum RpcBlockError {
6    #[error("Block not found: {error_message}")]
7    UnknownBlock {
8        // We are skipping this field for now
9        // until we can provide useful struct like block_height or block_hash
10        // that was requested
11        #[serde(skip_serializing)]
12        error_message: String,
13    },
14    #[error("There are no fully synchronized blocks yet")]
15    NotSyncedYet,
16    #[error("The node reached its limits. Try again later. More details: {error_message}")]
17    InternalError { error_message: String },
18}
19
20#[derive(Debug, serde::Serialize, serde::Deserialize, arbitrary::Arbitrary)]
21pub struct RpcBlockRequest {
22    #[serde(flatten)]
23    pub block_reference: unc_primitives::types::BlockReference,
24}
25
26#[derive(Debug, serde::Serialize, serde::Deserialize)]
27pub struct RpcBlockResponse {
28    #[serde(flatten)]
29    pub block_view: unc_primitives::views::BlockView,
30}
31
32impl From<RpcBlockError> for crate::errors::RpcError {
33    fn from(error: RpcBlockError) -> Self {
34        let error_data = match &error {
35            RpcBlockError::UnknownBlock { error_message } => Some(Value::String(format!(
36                "DB Not Found Error: {} \n Cause: Unknown",
37                error_message
38            ))),
39            RpcBlockError::NotSyncedYet | RpcBlockError::InternalError { .. } => {
40                Some(Value::String(error.to_string()))
41            }
42        };
43
44        let error_data_value = match serde_json::to_value(error) {
45            Ok(value) => value,
46            Err(err) => {
47                return Self::new_internal_error(
48                    None,
49                    format!("Failed to serialize RpcBlockError: {:?}", err),
50                )
51            }
52        };
53
54        Self::new_internal_or_handler_error(error_data, error_data_value)
55    }
56}