sc_consensus_manual_seal/
error.rs1use futures::channel::{mpsc::SendError, oneshot};
23use jsonrpsee::types::error::{ErrorObject, ErrorObjectOwned};
24use sc_consensus::ImportResult;
25use sp_blockchain::Error as BlockchainError;
26use sp_consensus::Error as ConsensusError;
27use sp_inherents::Error as InherentsError;
28
29mod codes {
31	pub const SERVER_SHUTTING_DOWN: i32 = 10_000;
32	pub const BLOCK_IMPORT_FAILED: i32 = 11_000;
33	pub const EMPTY_TRANSACTION_POOL: i32 = 12_000;
34	pub const BLOCK_NOT_FOUND: i32 = 13_000;
35	pub const CONSENSUS_ERROR: i32 = 14_000;
36	pub const INHERENTS_ERROR: i32 = 15_000;
37	pub const BLOCKCHAIN_ERROR: i32 = 16_000;
38	pub const UNKNOWN_ERROR: i32 = 20_000;
39}
40
41#[derive(Debug, thiserror::Error)]
43pub enum Error {
44	#[error("Block import failed: {0:?}")]
46	BlockImportError(ImportResult),
47	#[error(
49		"Transaction pool is empty, set create_empty to true, if you want to create empty blocks"
50	)]
51	EmptyTransactionPool,
52	#[error("Consensus Error: {0}")]
54	ConsensusError(#[from] ConsensusError),
55	#[error("Inherents Error: {0}")]
57	InherentError(#[from] InherentsError),
58	#[error("Finalization Error: {0}")]
60	BlockchainError(#[from] BlockchainError),
61	#[error("Supplied parent_hash: {0} doesn't exist in chain")]
63	BlockNotFound(String),
64	#[error("{0}")]
66	StringError(String),
67	#[error("Consensus process is terminating")]
69	Canceled(#[from] oneshot::Canceled),
70	#[error("Consensus process is terminating")]
72	SendError(#[from] SendError),
73	#[error("Other error: {0}")]
75	Other(Box<dyn std::error::Error + Send + Sync>),
76}
77
78impl From<ImportResult> for Error {
79	fn from(err: ImportResult) -> Self {
80		Error::BlockImportError(err)
81	}
82}
83
84impl From<String> for Error {
85	fn from(s: String) -> Self {
86		Error::StringError(s)
87	}
88}
89
90impl Error {
91	fn to_code(&self) -> i32 {
92		use Error::*;
93		match self {
94			BlockImportError(_) => codes::BLOCK_IMPORT_FAILED,
95			BlockNotFound(_) => codes::BLOCK_NOT_FOUND,
96			EmptyTransactionPool => codes::EMPTY_TRANSACTION_POOL,
97			ConsensusError(_) => codes::CONSENSUS_ERROR,
98			InherentError(_) => codes::INHERENTS_ERROR,
99			BlockchainError(_) => codes::BLOCKCHAIN_ERROR,
100			SendError(_) | Canceled(_) => codes::SERVER_SHUTTING_DOWN,
101			_ => codes::UNKNOWN_ERROR,
102		}
103	}
104}
105
106impl From<Error> for ErrorObjectOwned {
107	fn from(err: Error) -> Self {
108		ErrorObject::owned(err.to_code(), err.to_string(), None::<()>)
109	}
110}