sc_rpc_spec_v2/archive/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 `archive` RPC module.
20
21use jsonrpsee::types::error::ErrorObject;
22
23/// ChainHead RPC errors.
24#[derive(Debug, thiserror::Error)]
25pub enum Error {
26 /// Invalid parameter provided to the RPC method.
27 #[error("Invalid parameter: {0}")]
28 InvalidParam(String),
29 /// Runtime call failed.
30 #[error("Runtime call: {0}")]
31 RuntimeCall(String),
32 /// Failed to fetch leaves.
33 #[error("Failed to fetch leaves of the chain: {0}")]
34 FetchLeaves(String),
35}
36
37// Base code for all `archive` errors.
38const BASE_ERROR: i32 = 3000;
39/// Invalid parameter error.
40const INVALID_PARAM_ERROR: i32 = BASE_ERROR + 1;
41/// Runtime call error.
42const RUNTIME_CALL_ERROR: i32 = BASE_ERROR + 2;
43/// Failed to fetch leaves.
44const FETCH_LEAVES_ERROR: i32 = BASE_ERROR + 3;
45
46impl From<Error> for ErrorObject<'static> {
47 fn from(e: Error) -> Self {
48 let msg = e.to_string();
49
50 match e {
51 Error::InvalidParam(_) => ErrorObject::owned(INVALID_PARAM_ERROR, msg, None::<()>),
52 Error::RuntimeCall(_) => ErrorObject::owned(RUNTIME_CALL_ERROR, msg, None::<()>),
53 Error::FetchLeaves(_) => ErrorObject::owned(FETCH_LEAVES_ERROR, msg, None::<()>),
54 }
55 .into()
56 }
57}
58
59/// The error type for errors that can never happen.
60//
61// NOTE: Can't use std::convert::Infallible because of the orphan-rule
62pub enum Infallible {}
63
64impl From<Infallible> for ErrorObject<'static> {
65 fn from(e: Infallible) -> Self {
66 match e {}
67 }
68}