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
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Serialize, Deserialize)]
pub struct RpcProtocolConfigRequest {
#[serde(flatten)]
pub block_reference: near_primitives_v01::types::BlockReference,
}
impl RpcProtocolConfigRequest {
pub fn parse(
value: Option<Value>,
) -> Result<RpcProtocolConfigRequest, crate::errors::RpcParseError> {
crate::utils::parse_params::<near_primitives_v01::types::BlockReference>(value)
.map(|block_reference| RpcProtocolConfigRequest { block_reference })
}
}
#[derive(Serialize, Deserialize)]
pub struct RpcProtocolConfigResponse {
#[serde(flatten)]
pub config_view: near_chain_configs::ProtocolConfigView,
}
#[derive(thiserror::Error, Debug, Serialize, Deserialize)]
#[serde(tag = "name", content = "info", rename_all = "SCREAMING_SNAKE_CASE")]
pub enum RpcProtocolConfigError {
#[error("Block has never been observed: {error_message}")]
UnknownBlock {
#[serde(skip_serializing)]
error_message: String,
},
#[error("The node reached its limits. Try again later. More details: {error_message}")]
InternalError { error_message: String },
}
impl From<near_client_primitives::types::GetProtocolConfigError> for RpcProtocolConfigError {
fn from(error: near_client_primitives::types::GetProtocolConfigError) -> Self {
match error {
near_client_primitives::types::GetProtocolConfigError::UnknownBlock(error_message) => {
Self::UnknownBlock { error_message }
}
near_client_primitives::types::GetProtocolConfigError::IOError(error_message) => {
Self::InternalError { error_message }
}
near_client_primitives::types::GetProtocolConfigError::Unreachable(
ref error_message,
) => {
tracing::warn!(target: "jsonrpc", "Unreachable error occurred: {}", &error_message);
near_metrics::inc_counter_vec(
&crate::metrics::RPC_UNREACHABLE_ERROR_COUNT,
&["RpcProtocolConfigError"],
);
Self::InternalError { error_message: error.to_string() }
}
}
}
}
impl From<actix::MailboxError> for RpcProtocolConfigError {
fn from(error: actix::MailboxError) -> Self {
Self::InternalError { error_message: error.to_string() }
}
}
impl From<RpcProtocolConfigError> for crate::errors::RpcError {
fn from(error: RpcProtocolConfigError) -> Self {
let error_data = match &error {
RpcProtocolConfigError::UnknownBlock { error_message } => {
Some(Value::String(format!("Block Not Found: {}", error_message)))
}
RpcProtocolConfigError::InternalError { .. } => Some(Value::String(error.to_string())),
};
let error_data_value = match serde_json::to_value(error) {
Ok(value) => value,
Err(err) => {
return Self::new_internal_error(
None,
format!("Failed to serialize RpcProtocolConfigError: {:?}", err),
)
}
};
Self::new_internal_or_handler_error(error_data, error_data_value)
}
}