darkfid/
error.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2025 Dyne.org foundation
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as
7 * published by the Free Software Foundation, either version 3 of the
8 * License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17 */
18
19use darkfi::rpc::jsonrpc::{ErrorCode::ServerError, JsonError, JsonResult};
20
21/// Custom RPC errors available for darkfid.
22/// Please sort them sensefully.
23pub enum RpcError {
24    // Transaction-related errors
25    TxSimulationFail = -32110,
26    TxGasCalculationFail = -32111,
27
28    // State-related errors,
29    NotSynced = -32120,
30    UnknownBlockHeight = -32121,
31
32    // Parsing errors
33    ParseError = -32190,
34
35    // Contract-related errors
36    ContractZkasDbNotFound = -32200,
37    ContractStateNotFound = -32201,
38    ContractStateKeyNotFound = -32202,
39
40    // Miner errors
41    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        // Transaction-related errors
55        RpcError::TxSimulationFail => "Failed simulating transaction state change",
56        RpcError::TxGasCalculationFail => "Failed to calculate transaction's gas",
57        // State-related errors
58        RpcError::NotSynced => "Blockchain is not synced",
59        RpcError::UnknownBlockHeight => "Did not find block height",
60        // Parsing errors
61        RpcError::ParseError => "Parse error",
62        // Contract-related errors
63        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        // Miner errors
67        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}