referrerpolicy=no-referrer-when-downgrade

sc_rpc_spec_v2/statement/
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
19use jsonrpsee::types::error::ErrorObject;
20
21/// Error returned by statement RPC methods
22#[derive(Debug, thiserror::Error)]
23pub enum Error {
24	/// The subscription id is not active on this connection
25	#[error("Invalid statement subscription identifier")]
26	InvalidSubscription,
27	/// A request parameter is invalid
28	#[error("Invalid parameter: {0}")]
29	InvalidParam(String),
30	/// The server failed while handling a valid request
31	#[error("Internal error: {0}")]
32	InternalError(String),
33}
34
35/// Error codes defined by the statement RPC specification.
36pub mod rpc_spec_v2 {
37	/// Subscription identifier is invalid
38	pub const INVALID_SUBSCRIPTION: i32 = -32801;
39}
40
41/// Error codes defined by the JSON-RPC specification.
42pub mod json_rpc_spec {
43	/// Request parameters are invalid
44	pub const INVALID_PARAM_ERROR: i32 = -32602;
45	/// Internal server error occurs
46	pub const INTERNAL_ERROR: i32 = -32603;
47}
48
49impl From<Error> for ErrorObject<'static> {
50	fn from(e: Error) -> Self {
51		let msg = e.to_string();
52		match e {
53			Error::InvalidSubscription => {
54				ErrorObject::owned(rpc_spec_v2::INVALID_SUBSCRIPTION, msg, None::<()>)
55			},
56			Error::InvalidParam(_) => {
57				ErrorObject::owned(json_rpc_spec::INVALID_PARAM_ERROR, msg, None::<()>)
58			},
59			Error::InternalError(_) => {
60				ErrorObject::owned(json_rpc_spec::INTERNAL_ERROR, msg, None::<()>)
61			},
62		}
63	}
64}