snarkos_errors/rpc/
rpc.rs

1// Copyright (C) 2019-2020 Aleo Systems Inc.
2// This file is part of the snarkOS library.
3
4// The snarkOS library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The snarkOS library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the snarkOS library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::{
18    algorithms::CRHError,
19    consensus::ConsensusError,
20    dpc::DPCError,
21    network::SendError,
22    objects::{AccountError, BlockError, TransactionError},
23    storage::StorageError,
24};
25
26use std::fmt::Debug;
27
28#[derive(Debug, Error)]
29pub enum RpcError {
30    #[error("{}", _0)]
31    AccountError(AccountError),
32
33    #[error("{}", _0)]
34    BlockError(BlockError),
35
36    #[error("{}", _0)]
37    ConsensusError(ConsensusError),
38
39    #[error("{}: {}", _0, _1)]
40    Crate(&'static str, String),
41
42    #[error("{}", _0)]
43    CRHError(CRHError),
44
45    #[error("{}", _0)]
46    DPCError(DPCError),
47
48    #[error("invalid block hash: {}", _0)]
49    InvalidBlockHash(String),
50
51    #[error("invalid metadata: {}", _0)]
52    InvalidMetadata(String),
53
54    #[error("{}", _0)]
55    Message(String),
56
57    #[error("{}", _0)]
58    SendError(SendError),
59
60    #[error("{}", _0)]
61    StorageError(StorageError),
62
63    #[error("{}", _0)]
64    TransactionError(TransactionError),
65}
66
67impl From<AccountError> for RpcError {
68    fn from(error: AccountError) -> Self {
69        RpcError::AccountError(error)
70    }
71}
72
73impl From<BlockError> for RpcError {
74    fn from(error: BlockError) -> Self {
75        RpcError::BlockError(error)
76    }
77}
78
79impl From<ConsensusError> for RpcError {
80    fn from(error: ConsensusError) -> Self {
81        RpcError::ConsensusError(error)
82    }
83}
84
85impl From<CRHError> for RpcError {
86    fn from(error: CRHError) -> Self {
87        RpcError::CRHError(error)
88    }
89}
90
91impl From<DPCError> for RpcError {
92    fn from(error: DPCError) -> Self {
93        RpcError::DPCError(error)
94    }
95}
96
97impl From<SendError> for RpcError {
98    fn from(error: SendError) -> Self {
99        RpcError::SendError(error)
100    }
101}
102
103impl From<StorageError> for RpcError {
104    fn from(error: StorageError) -> Self {
105        RpcError::StorageError(error)
106    }
107}
108
109impl From<TransactionError> for RpcError {
110    fn from(error: TransactionError) -> Self {
111        RpcError::TransactionError(error)
112    }
113}
114
115impl From<hex::FromHexError> for RpcError {
116    fn from(error: hex::FromHexError) -> Self {
117        RpcError::Crate("hex", format!("{:?}", error))
118    }
119}
120
121impl From<jsonrpc_core::Error> for RpcError {
122    fn from(error: jsonrpc_core::Error) -> Self {
123        RpcError::Crate("jsonrpc_core", format!("{:?}", error))
124    }
125}
126
127impl From<std::io::Error> for RpcError {
128    fn from(error: std::io::Error) -> Self {
129        RpcError::Crate("std::io", format!("{:?}", error))
130    }
131}
132
133impl From<&'static str> for RpcError {
134    fn from(msg: &'static str) -> Self {
135        RpcError::Message(msg.into())
136    }
137}
138
139impl From<std::boxed::Box<dyn std::any::Any + std::marker::Send>> for RpcError {
140    fn from(error: std::boxed::Box<dyn std::any::Any + std::marker::Send>) -> Self {
141        RpcError::Crate("std::boxed::Box", format!("{:?}", error))
142    }
143}
144
145impl From<RpcError> for jsonrpc_core::Error {
146    fn from(_error: RpcError) -> Self {
147        jsonrpc_core::Error::invalid_request()
148    }
149}