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
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct RpcStatusResponse {
    #[serde(flatten)]
    pub status_response: near_primitives_v01::views::StatusResponse,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct RpcHealthResponse;

#[derive(thiserror::Error, Debug, Serialize, Deserialize)]
#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RpcStatusError {
    #[error("Node is syncing")]
    NodeIsSyncing,
    #[error("No blocks for {elapsed:?}")]
    NoNewBlocks { elapsed: std::time::Duration },
    #[error("Epoch Out Of Bounds {epoch_id:?}")]
    EpochOutOfBounds { epoch_id: near_primitives_v01::types::EpochId },
    #[error("The node reached its limits. Try again later. More details: {error_message}")]
    InternalError { error_message: String },
}

impl From<near_primitives_v01::views::StatusResponse> for RpcStatusResponse {
    fn from(status_response: near_primitives_v01::views::StatusResponse) -> Self {
        Self { status_response }
    }
}

impl From<near_primitives_v01::views::StatusResponse> for RpcHealthResponse {
    fn from(_status_response: near_primitives_v01::views::StatusResponse) -> Self {
        Self {}
    }
}

impl From<near_client_primitives::types::StatusError> for RpcStatusError {
    fn from(error: near_client_primitives::types::StatusError) -> Self {
        match error {
            near_client_primitives::types::StatusError::InternalError { error_message } => {
                Self::InternalError { error_message }
            }
            near_client_primitives::types::StatusError::NodeIsSyncing => Self::NodeIsSyncing,
            near_client_primitives::types::StatusError::NoNewBlocks { elapsed } => {
                Self::NoNewBlocks { elapsed }
            }
            near_client_primitives::types::StatusError::EpochOutOfBounds { epoch_id } => {
                Self::EpochOutOfBounds { epoch_id }
            }
            near_client_primitives::types::StatusError::Unreachable { ref error_message } => {
                tracing::warn!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
                near_metrics::inc_counter_vec(
                    &crate::metrics::RPC_UNREACHABLE_ERROR_COUNT,
                    &["RpcStatusError"],
                );
                Self::InternalError { error_message: error.to_string() }
            }
        }
    }
}

impl From<actix::MailboxError> for RpcStatusError {
    fn from(error: actix::MailboxError) -> Self {
        Self::InternalError { error_message: error.to_string() }
    }
}

impl From<RpcStatusError> for crate::errors::RpcError {
    fn from(error: RpcStatusError) -> Self {
        let error_data = match serde_json::to_value(error) {
            Ok(value) => value,
            Err(err) => {
                return Self::new_internal_error(
                    None,
                    format!("Failed to serialize RpcStateChangesError: {:?}", err),
                )
            }
        };
        Self::new_internal_or_handler_error(Some(error_data.clone()), error_data)
    }
}