sc_rpc_api/state/
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//! State RPC errors.
20
21use jsonrpsee::types::error::{ErrorObject, ErrorObjectOwned};
22
23/// State RPC Result type.
24pub type Result<T> = std::result::Result<T, Error>;
25
26/// State RPC errors.
27#[derive(Debug, thiserror::Error)]
28pub enum Error {
29	/// Client error.
30	#[error("Client error: {}", .0)]
31	Client(#[from] Box<dyn std::error::Error + Send + Sync>),
32	/// Provided block range couldn't be resolved to a list of blocks.
33	#[error("Cannot resolve a block range ['{:?}' ... '{:?}]. {}", .from, .to, .details)]
34	InvalidBlockRange {
35		/// Beginning of the block range.
36		from: String,
37		/// End of the block range.
38		to: String,
39		/// Details of the error message.
40		details: String,
41	},
42	/// Provided count exceeds maximum value.
43	#[error("count exceeds maximum value. value: {}, max: {}", .value, .max)]
44	InvalidCount {
45		/// Provided value
46		value: u32,
47		/// Maximum allowed value
48		max: u32,
49	},
50	/// Call to an unsafe RPC was denied.
51	#[error(transparent)]
52	UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
53}
54
55/// Base code for all state errors.
56const BASE_ERROR: i32 = crate::error::base::STATE;
57
58impl From<Error> for ErrorObjectOwned {
59	fn from(e: Error) -> ErrorObjectOwned {
60		match e {
61			Error::InvalidBlockRange { .. } =>
62				ErrorObject::owned(BASE_ERROR + 1, e.to_string(), None::<()>),
63			Error::InvalidCount { .. } =>
64				ErrorObject::owned(BASE_ERROR + 2, e.to_string(), None::<()>),
65			e => ErrorObject::owned(BASE_ERROR + 3, e.to_string(), None::<()>),
66		}
67	}
68}