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 PingFailed = -32300,
42}
43
44fn to_tuple(e: RpcError) -> (i32, String) {
45 let msg = match e {
46 RpcError::TxSimulationFail => "Failed simulating transaction state change",
48 RpcError::TxGasCalculationFail => "Failed to calculate transaction's gas",
49 RpcError::NotSynced => "Blockchain is not synced",
51 RpcError::UnknownBlockHeight => "Did not find block height",
52 RpcError::ParseError => "Parse error",
54 RpcError::ContractZkasDbNotFound => "zkas database not found for given contract",
56 RpcError::ContractStateNotFound => "Records not found for given contract state",
57 RpcError::ContractStateKeyNotFound => "Value not found for given contract state key",
58 RpcError::PingFailed => "Miner daemon ping error",
60 };
61
62 (e as i32, msg.to_string())
63}
64
65pub fn server_error(e: RpcError, id: u16, msg: Option<&str>) -> JsonResult {
66 let (code, default_msg) = to_tuple(e);
67
68 if let Some(message) = msg {
69 return JsonError::new(ServerError(code), Some(message.to_string()), id).into()
70 }
71
72 JsonError::new(ServerError(code), Some(default_msg), id).into()
73}