From 8467f4a62f437b1bd32a7880019bcaaa275e1ecc Mon Sep 17 00:00:00 2001 From: Bernhard Schuster Date: Fri, 30 Oct 2020 15:57:00 +0100 Subject: [PATCH] chore/error: remove from str conversion and add deprecation notifications --- client/service/src/client/wasm_override.rs | 18 +++---- client/sync-state-rpc/src/lib.rs | 14 ++--- primitives/blockchain/src/backend.rs | 4 +- primitives/blockchain/src/error.rs | 61 +++++++++++++++++----- 4 files changed, 64 insertions(+), 33 deletions(-) diff --git a/client/service/src/client/wasm_override.rs b/client/service/src/client/wasm_override.rs index 1025b9633887d..748588f939c3e 100644 --- a/client/service/src/client/wasm_override.rs +++ b/client/service/src/client/wasm_override.rs @@ -119,16 +119,16 @@ where /// Scrapes a folder for WASM runtimes. /// Returns a hashmap of the runtime version and wasm runtime code. fn scrape_overrides(dir: &Path, executor: &E) -> Result> { - let handle_err = |e: std::io::Error | -> sp_blockchain::Error { - sp_blockchain::Error::Msg(format!("{}", e.to_string())) + + let handle_err = { + let dir = dir.to_owned(); + move |e: std::io::Error | -> sp_blockchain::Error { + sp_blockchain::Error::WasmOverrideIo(dir, e) + } }; if !dir.is_dir() { - return Err(sp_blockchain::Error::Msg(format!( - "Overwriting WASM requires a directory where \ - local WASM is stored. {:?} is not a directory", - dir, - ))); + return Err(sp_blockchain::Error::WasmOverrideNotADirectory(dir.to_owned())); } let mut overrides = HashMap::new(); @@ -149,9 +149,7 @@ where } if !duplicates.is_empty() { - let duplicate_file_list = duplicates.join("\n"); - let msg = format!("Duplicate WASM Runtimes found: \n{}\n", duplicate_file_list); - return Err(sp_blockchain::Error::Msg(msg)); + return Err(sp_blockchain::Error::DuplicateWasmRuntime(duplicates)); } Ok(overrides) diff --git a/client/sync-state-rpc/src/lib.rs b/client/sync-state-rpc/src/lib.rs index fa433e5e31d2d..e1c70d3c8fa20 100644 --- a/client/sync-state-rpc/src/lib.rs +++ b/client/sync-state-rpc/src/lib.rs @@ -79,17 +79,13 @@ impl SyncStateRpcHandler fn build_sync_state(&self) -> Result, sp_blockchain::Error> { let finalized_hash = self.client.info().finalized_hash; let finalized_header = self.client.header(BlockId::Hash(finalized_hash))? - .ok_or_else(|| sp_blockchain::Error::Msg( - format!("Failed to get the header for block {:?}", finalized_hash) - ))?; + .ok_or_else(|| sp_blockchain::Error::MissingHashInHeader(finalized_hash.to_string()))?; let finalized_block_weight = sc_consensus_babe::aux_schema::load_block_weight( - &*self.client, - finalized_hash, - )? - .ok_or_else(|| sp_blockchain::Error::Msg( - format!("Failed to load the block weight for block {:?}", finalized_hash) - ))?; + &*self.client, + finalized_hash, + )? + .ok_or_else(|| sp_blockchain::Error::MissingBlockWeightInHeader(finalized_hash.to_string()))?; Ok(sc_chain_spec::LightSyncState { finalized_block_header: finalized_header, diff --git a/primitives/blockchain/src/backend.rs b/primitives/blockchain/src/backend.rs index 1328dfb5752fc..4352a4de1d53a 100644 --- a/primitives/blockchain/src/backend.rs +++ b/primitives/blockchain/src/backend.rs @@ -172,7 +172,7 @@ pub trait Backend: HeaderBackend + HeaderMetadata: HeaderBackend + HeaderMetadata), + /// Blockchain error. #[error("Blockchain")] Blockchain(#[source] Box), + /// Invalid authorities set received from the runtime. #[error("Current state of blockchain has invalid authorities set")] InvalidAuthoritiesSet, + /// Could not get runtime version. #[error("Failed to get runtime version: {0}")] VersionInvalid(String), + /// Genesis config is invalid. #[error("Genesis config provided is invalid")] GenesisInvalid, + /// Error decoding header justification. #[error("error decoding justification for header")] JustificationDecode, + /// Justification for header is correctly encoded, but invalid. #[error("bad justification for header: {0}")] BadJustification(String), + /// Not available on light client. #[error("This method is not currently available when running in light client mode")] NotAvailableOnLightClient, + /// Invalid remote CHT-based proof. #[error("Remote node has responded with invalid header proof")] InvalidCHTProof, + /// Remote fetch has been cancelled. #[error("Remote data fetch has been cancelled")] RemoteFetchCancelled, + /// Remote fetch has been failed. #[error("Remote data fetch has been failed")] RemoteFetchFailed, + /// Error decoding call result. #[error("Error decoding call result of {0}")] CallResultDecode(&'static str, #[source] CodecError), + /// Error converting a parameter between runtime and node. #[error("Error converting `{0}` between runtime and node")] RuntimeParamConversion(String), + /// Changes tries are not supported. #[error("Changes tries are not supported by the runtime")] ChangesTriesNotSupported, + /// Error reading changes tries configuration. #[error("Error reading changes tries configuration")] ErrorReadingChangesTriesConfig, + /// Key changes query has failed. #[error("Failed to check changes proof: {0}")] ChangesTrieAccessFailed(String), + /// Last finalized block not parent of current. #[error("Did not finalize blocks in sequential order.")] NonSequentialFinalization(String), + /// Safety violation: new best block not descendent of last finalized. #[error("Potential long-range attack: block not in finalized chain.")] NotInFinalizedChain, + /// Hash that is required for building CHT is missing. #[error("Failed to get hash of block for building CHT")] MissingHashRequiredForCHT, + /// Invalid calculated state root on block import. #[error("Calculated state root does not match.")] InvalidStateRoot, + /// Incomplete block import pipeline. #[error("Incomplete block import pipeline.")] IncompletePipeline, + + /// Transaction pool initizliation is not complete. #[error("Transaction pool not ready for block production.")] TransactionPoolNotReady, + + /// Database yielded an error. #[error("Database")] DatabaseError(#[from] sp_database::error::DatabaseError), + + #[error("Failed to get header for hash {0}")] + MissingHashInHeader(String), + + #[error("Failed to load the block weight for block {0}")] + MissingBlockWeightInHeader(String), + + #[error("WASM override IO error")] + WasmOverrideIo(PathBuf, #[source] std::io::Error), + + #[error("Overwriting WASM requires a directory where local \ + WASM is stored. {0} is not a directory", .0.display())] + WasmOverrideNotADirectory(PathBuf), + + #[error("Duplicate WASM Runtimes found: \n{}\n", .0.join("\n") )] + DuplicateWasmRuntime(Vec), + /// A convenience variant for String + #[deprecated(note = "Introduce more typed error variants as needed")] #[error("{0}")] Msg(String), } -impl<'a> From<&'a str> for Error { - fn from(s: &'a str) -> Self { - Error::Msg(s.into()) - } -} - -impl From for Error { - fn from(s: String) -> Self { - Error::Msg(s) - } -} - impl From> for Error { fn from(e: Box) -> Self { Self::from_state(e)