sc_rpc_api/system/
error.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0
5
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License as published by
8// the Free Software Foundation, either version 3 of the License, or
9// (at your option) any later version.
10
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14// GNU General Public License for more details.
15
16// You should have received a copy of the GNU General Public License
17// along with this program. If not, see <https://www.gnu.org/licenses/>.
18
19//! System RPC module errors.
20
21use crate::system::helpers::Health;
22use jsonrpsee::types::{
23	error::{ErrorCode, ErrorObject},
24	ErrorObjectOwned,
25};
26
27/// System RPC Result type.
28pub type Result<T> = std::result::Result<T, Error>;
29
30/// System RPC errors.
31#[derive(Debug, thiserror::Error)]
32pub enum Error {
33	/// Provided block range couldn't be resolved to a list of blocks.
34	#[error("Node is not fully functional: {}", .0)]
35	NotHealthy(Health),
36	/// Peer argument is malformatted.
37	#[error("{0}")]
38	MalformattedPeerArg(String),
39	/// Call to an unsafe RPC was denied.
40	#[error(transparent)]
41	UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
42	/// Internal error.
43	#[error("{0}")]
44	Internal(String),
45}
46
47// Base code for all system errors.
48const BASE_ERROR: i32 = crate::error::base::SYSTEM;
49// Provided block range couldn't be resolved to a list of blocks.
50const NOT_HEALTHY_ERROR: i32 = BASE_ERROR + 1;
51// Peer argument is malformatted.
52const MALFORMATTED_PEER_ARG_ERROR: i32 = BASE_ERROR + 2;
53
54impl From<Error> for ErrorObjectOwned {
55	fn from(e: Error) -> ErrorObjectOwned {
56		match e {
57			Error::NotHealthy(ref h) =>
58				ErrorObject::owned(NOT_HEALTHY_ERROR, e.to_string(), Some(h)),
59			Error::MalformattedPeerArg(e) =>
60				ErrorObject::owned(MALFORMATTED_PEER_ARG_ERROR, e, None::<()>),
61			Error::UnsafeRpcCalled(e) => e.into(),
62			Error::Internal(e) =>
63				ErrorObjectOwned::owned(ErrorCode::InternalError.code(), e, None::<()>),
64		}
65	}
66}