referrerpolicy=no-referrer-when-downgrade

sc_rpc_spec_v2/chain_head/
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//! Error helpers for `chainHead` RPC module.
20
21use jsonrpsee::types::error::ErrorObject;
22
23/// ChainHead RPC errors.
24#[derive(Debug, thiserror::Error)]
25pub enum Error {
26	/// Maximum number of chainHead_follow has been reached.
27	#[error("Maximum number of chainHead_follow has been reached")]
28	ReachedLimits,
29	/// The provided block hash is invalid.
30	#[error("Invalid block hash")]
31	InvalidBlock,
32	/// The follow subscription was started with `withRuntime` set to `false`.
33	#[error("The `chainHead_follow` subscription was started with `withRuntime` set to `false`")]
34	InvalidRuntimeCall(String),
35	/// Wait-for-continue event not generated.
36	#[error("Wait for continue event was not generated for the subscription")]
37	InvalidContinue,
38	/// Received duplicate hashes for the `chainHead_unpin` method.
39	#[error("Received duplicate hashes for the `chainHead_unpin` method")]
40	InvalidDuplicateHashes,
41	/// Invalid parameter provided to the RPC method.
42	#[error("Invalid parameter: {0}")]
43	InvalidParam(String),
44	/// Internal error.
45	#[error("Internal error: {0}")]
46	InternalError(String),
47}
48
49/// Errors for `chainHead` RPC module, as defined in
50/// <https://github.com/paritytech/json-rpc-interface-spec>.
51pub mod rpc_spec_v2 {
52	/// Maximum number of chainHead_follow has been reached.
53	pub const REACHED_LIMITS: i32 = -32800;
54	/// The provided block hash is invalid.
55	pub const INVALID_BLOCK_ERROR: i32 = -32801;
56	/// The follow subscription was started with `withRuntime` set to `false`.
57	pub const INVALID_RUNTIME_CALL: i32 = -32802;
58	/// Wait-for-continue event not generated.
59	pub const INVALID_CONTINUE: i32 = -32803;
60	/// Received duplicate hashes for the `chainHead_unpin` method.
61	pub const INVALID_DUPLICATE_HASHES: i32 = -32804;
62}
63
64/// General purpose errors, as defined in
65/// <https://www.jsonrpc.org/specification#error_object>.
66pub mod json_rpc_spec {
67	/// Invalid parameter error.
68	pub const INVALID_PARAM_ERROR: i32 = -32602;
69	/// Internal error.
70	pub const INTERNAL_ERROR: i32 = -32603;
71}
72
73impl From<Error> for ErrorObject<'static> {
74	fn from(e: Error) -> Self {
75		let msg = e.to_string();
76
77		match e {
78			Error::ReachedLimits =>
79				ErrorObject::owned(rpc_spec_v2::REACHED_LIMITS, msg, None::<()>),
80			Error::InvalidBlock =>
81				ErrorObject::owned(rpc_spec_v2::INVALID_BLOCK_ERROR, msg, None::<()>),
82			Error::InvalidRuntimeCall(_) =>
83				ErrorObject::owned(rpc_spec_v2::INVALID_RUNTIME_CALL, msg, None::<()>),
84			Error::InvalidContinue =>
85				ErrorObject::owned(rpc_spec_v2::INVALID_CONTINUE, msg, None::<()>),
86			Error::InvalidDuplicateHashes =>
87				ErrorObject::owned(rpc_spec_v2::INVALID_DUPLICATE_HASHES, msg, None::<()>),
88			Error::InvalidParam(_) =>
89				ErrorObject::owned(json_rpc_spec::INVALID_PARAM_ERROR, msg, None::<()>),
90			Error::InternalError(_) =>
91				ErrorObject::owned(json_rpc_spec::INTERNAL_ERROR, msg, None::<()>),
92		}
93	}
94}