sc_rpc_api/dev/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 Dev RPC module.
20
21use jsonrpsee::types::error::{ErrorObject, ErrorObjectOwned};
22
23/// Dev RPC Result type.
24pub type Result<T> = std::result::Result<T, Error>;
25
26/// Dev RPC errors.
27#[derive(Debug, thiserror::Error)]
28pub enum Error {
29 /// Failed to query specified block or its parent: Probably an invalid hash.
30 #[error("Error while querying block: {0}")]
31 BlockQueryError(Box<dyn std::error::Error + Send>),
32 /// The re-execution of the specified block failed.
33 #[error("Failed to re-execute the specified block")]
34 BlockExecutionFailed,
35 /// Failed to extract the proof.
36 #[error("Failed to extract the proof")]
37 ProofExtractionFailed,
38 /// The witness compaction failed.
39 #[error("Failed to create to compact the witness")]
40 WitnessCompactionFailed,
41 /// The method is marked as unsafe but unsafe flag wasn't supplied on the CLI.
42 #[error(transparent)]
43 UnsafeRpcCalled(#[from] crate::policy::UnsafeRpcError),
44}
45
46/// Base error code for all dev errors.
47const BASE_ERROR: i32 = crate::error::base::DEV;
48
49impl From<Error> for ErrorObjectOwned {
50 fn from(e: Error) -> Self {
51 let msg = e.to_string();
52
53 match e {
54 Error::BlockQueryError(_) => ErrorObject::owned(BASE_ERROR + 1, msg, None::<()>),
55 Error::BlockExecutionFailed => ErrorObject::owned(BASE_ERROR + 3, msg, None::<()>),
56 Error::WitnessCompactionFailed => ErrorObject::owned(BASE_ERROR + 4, msg, None::<()>),
57 Error::ProofExtractionFailed => ErrorObject::owned(BASE_ERROR + 5, msg, None::<()>),
58 Error::UnsafeRpcCalled(e) => e.into(),
59 }
60 }
61}