1use darkfi::rpc::jsonrpc::{ErrorCode::ServerError, JsonError, JsonResult};
20
21pub enum RpcError {
24 TxSimulationFail = -32110,
26 TxGasCalculationFail = -32111,
27
28 NotSynced = -32120,
30 UnknownBlockHeight = -32121,
31
32 ParseError = -32190,
34
35 ContractZkasDbNotFound = -32200,
37 ContractStateNotFound = -32201,
38 ContractStateKeyNotFound = -32202,
39
40 MinerMissingHeader = -32300,
42 MinerInvalidHeader = -32301,
43 MinerMissingRecipient = -32302,
44 MinerInvalidRecipient = -32303,
45 MinerInvalidSpendHook = -32304,
46 MinerInvalidUserData = -32305,
47 MinerMissingNonce = -32306,
48 MinerInvalidNonce = -32307,
49 MinerUnknownJob = -32308,
50}
51
52fn to_tuple(e: RpcError) -> (i32, String) {
53 let msg = match e {
54 RpcError::TxSimulationFail => "Failed simulating transaction state change",
56 RpcError::TxGasCalculationFail => "Failed to calculate transaction's gas",
57 RpcError::NotSynced => "Blockchain is not synced",
59 RpcError::UnknownBlockHeight => "Did not find block height",
60 RpcError::ParseError => "Parse error",
62 RpcError::ContractZkasDbNotFound => "zkas database not found for given contract",
64 RpcError::ContractStateNotFound => "Records not found for given contract state",
65 RpcError::ContractStateKeyNotFound => "Value not found for given contract state key",
66 RpcError::MinerMissingHeader => "Request is missing the Header hash",
68 RpcError::MinerInvalidHeader => "Request Header hash is invalid",
69 RpcError::MinerMissingRecipient => "Request is missing the recipient wallet address",
70 RpcError::MinerInvalidRecipient => "Request recipient wallet address is invalid",
71 RpcError::MinerInvalidSpendHook => "Request spend hook is invalid",
72 RpcError::MinerInvalidUserData => "Request user data is invalid",
73 RpcError::MinerMissingNonce => "Request is missing the Header nonce",
74 RpcError::MinerInvalidNonce => "Request Header nonce is invalid",
75 RpcError::MinerUnknownJob => "Request job is unknown",
76 };
77
78 (e as i32, msg.to_string())
79}
80
81pub fn server_error(e: RpcError, id: u16, msg: Option<&str>) -> JsonResult {
82 let (code, default_msg) = to_tuple(e);
83
84 if let Some(message) = msg {
85 return JsonError::new(ServerError(code), Some(message.to_string()), id).into()
86 }
87
88 JsonError::new(ServerError(code), Some(default_msg), id).into()
89}