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
// Copyright (c) 2022 MASSA LABS <info@massa.net>

use displaydoc::Display;
use jsonrpsee::types::{ErrorObject, ErrorObjectOwned};

use massa_hash::MassaHashError;
use massa_models::error::ModelsError;
use massa_time::TimeError;
use massa_versioning::versioning_factory::FactoryError;
use massa_wallet::WalletError;

/// Errors of the api component.
#[non_exhaustive]
#[derive(Display, thiserror::Error, Debug)]
pub enum ApiError {
    /// Send channel error: {0}
    SendChannelError(String),
    /// Receive channel error: {0}
    ReceiveChannelError(String),
    /// `massa_hash` error: {0}
    MassaHashError(#[from] MassaHashError),
    /// consensus error: {0}
    ConsensusError(String),
    /// execution error: {0}
    ExecutionError(String),
    /// Protocol error: {0}
    ProtocolError(String),
    /// Models error: {0}
    ModelsError(#[from] ModelsError),
    /// Time error: {0}
    TimeError(#[from] TimeError),
    /// Wallet error: {0}
    WalletError(#[from] WalletError),
    /// Not found
    NotFound,
    /// Inconsistency error: {0}
    InconsistencyError(String),
    /// Missing command sender: {0}
    MissingCommandSender(String),
    /// Missing configuration: {0}
    MissingConfig(String),
    /// The wrong API (either Public or Private) was called
    WrongAPI,
    /// Bad request: {0}
    BadRequest(String),
    /// Internal server error: {0}
    InternalServerError(String),
    /// Versioning Factory error: {0}
    FactoryError(#[from] FactoryError),
}

impl From<ApiError> for ErrorObjectOwned {
    fn from(err: ApiError) -> Self {
        // JSON-RPC Server errors codes must be between -32099 to -32000
        let code = match err {
            ApiError::BadRequest(_) => -32000,
            ApiError::InternalServerError(_) => -32001,
            ApiError::NotFound => -32004,
            ApiError::SendChannelError(_) => -32006,
            ApiError::ReceiveChannelError(_) => -32007,
            ApiError::MassaHashError(_) => -32008,
            ApiError::ConsensusError(_) => -32009,
            ApiError::ExecutionError(_) => -32010,
            ApiError::ProtocolError(_) => -32012,
            ApiError::ModelsError(_) => -32013,
            ApiError::TimeError(_) => -32014,
            ApiError::WalletError(_) => -32015,
            ApiError::InconsistencyError(_) => -32016,
            ApiError::MissingCommandSender(_) => -32017,
            ApiError::MissingConfig(_) => -32018,
            ApiError::WrongAPI => -32019,
            ApiError::FactoryError(_) => -32020,
        };

        ErrorObject::owned(code, err.to_string(), None::<()>)
    }
}