Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

chore/error: remove from str conversion and add deprecation notificat… #7472

Merged
39 commits merged into from
Nov 27, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
8467f4a
chore/error: remove from str conversion and add deprecation notificat…
Oct 30, 2020
91744cd
fixup changes
Nov 3, 2020
c1085eb
fix test looking for gone ::Msg variant
Nov 3, 2020
524ee71
another test fix
Nov 4, 2020
2a09d3c
one is duplicate, the other is not, so duplicates reported are n-1
Nov 4, 2020
dbbd20f
darn spaces
drahnr Nov 10, 2020
8d021f0
remove pointless doc comments of error variants without any value
Nov 10, 2020
311a58f
Merge remote-tracking branch 'origin/master' into bernhard-error-chore
Nov 11, 2020
8b3f12b
low hanging fruits (for a tall person)
Nov 13, 2020
2dbc971
moar error type variants
Nov 14, 2020
3e94fa6
avoid the storage modules for now
Nov 17, 2020
666dd5f
chore remove pointless error generic
Nov 17, 2020
69e849b
fix test for mocks, add a bunch of non_exhaustive
Nov 17, 2020
d54cb37
max line width
Nov 17, 2020
10a2278
Merge remote-tracking branch 'origin/master' into bernhard-error-chore
Nov 18, 2020
66b6989
test fixes due to error changes
Nov 20, 2020
49fe444
fin
Nov 25, 2020
2b5f9d7
Merge remote-tracking branch 'origin/master' into bernhard-error-chore
Nov 25, 2020
ff81007
error outputs... again
Nov 25, 2020
13ba47b
undo stderr adjustments
Nov 26, 2020
3dda02d
Update client/consensus/slots/src/lib.rs
drahnr Nov 26, 2020
eb7805b
remove closure clutter
drahnr Nov 26, 2020
536f11e
more error types
Nov 26, 2020
bc40955
introduce ApiError
Nov 26, 2020
31f0259
extract Mock error
Nov 26, 2020
717ec7d
ApiError refactor
Nov 26, 2020
b4c1348
even more error types
Nov 26, 2020
cc2353d
the last for now
Nov 26, 2020
1d364ac
chore unused deps
Nov 26, 2020
56a0b2d
another extraction
Nov 26, 2020
c080594
reduce should panic, due to extended error messages
Nov 26, 2020
077301e
error test happiness
Nov 26, 2020
0983c88
shift error lines by one
Nov 27, 2020
0e9d696
doc tests
Nov 27, 2020
4b752b0
Merge remote-tracking branch 'origin/master' into bernhard-error-chore
Nov 27, 2020
35d2814
white space
drahnr Nov 27, 2020
13810eb
Into -> From
drahnr Nov 27, 2020
6df60d0
remove pointless codec
drahnr Nov 27, 2020
d0c0d24
avoid pointless self import
drahnr Nov 27, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

45 changes: 21 additions & 24 deletions client/cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,64 +25,61 @@ pub type Result<T> = std::result::Result<T, Error>;

/// Error type for the CLI.
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
/// Io error
#[error(transparent)]
Io(#[from] std::io::Error),
/// Cli error

#[error(transparent)]
Cli(#[from] structopt::clap::Error),
/// Service error

#[error(transparent)]
Service(#[from] sc_service::Error),
/// Client error

#[error(transparent)]
Client(#[from] sp_blockchain::Error),
/// scale codec error

#[error(transparent)]
Codec(#[from] parity_scale_codec::Error),
/// Input error

#[error("Invalid input: {0}")]
Input(String),
/// Invalid listen multiaddress

#[error("Invalid listen multiaddress")]
InvalidListenMultiaddress,
/// Application specific error chain sequence forwarder.
#[error(transparent)]
Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
/// URI error.

#[error("Invalid URI; expecting either a secret URI or a public URI.")]
InvalidUri(crypto::PublicError),
/// Signature length mismatch.

#[error("Signature has an invalid length. Read {read} bytes, expected {expected} bytes")]
SignatureInvalidLength {
/// Amount of signature bytes read.
read: usize,
/// Expected number of signature bytes.
expected: usize,
},
/// Missing base path argument.

#[error("The base path is missing, please provide one")]
MissingBasePath,
/// Unknown key type specifier or missing key type specifier.

#[error("Unknown key type, must be a known 4-character sequence")]
KeyTypeInvalid,
/// Signature verification failed.

#[error("Signature verification failed")]
SignatureInvalid,
/// Storing a given key failed.

#[error("Key store operation failed")]
KeyStoreOperation,
/// An issue with the underlying key storage was encountered.

#[error("Key storage issue encountered")]
KeyStorage(#[from] sc_keystore::Error),
/// Bytes are not decodable when interpreted as hexadecimal string.
#[error("Invalid hex base data")]

#[error("Invalid hexadecimal string data")]
HexDataConversion(#[from] hex::FromHexError),
/// Shortcut type to specify types on the fly, discouraged.
#[deprecated = "Use `Forwarded` with an error type instead."]
#[error("Other: {0}")]
Other(String),

/// Application specific error chain sequence forwarder.
#[error(transparent)]
Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
}

impl std::convert::From<&str> for Error {
Expand All @@ -93,7 +90,7 @@ impl std::convert::From<&str> for Error {

impl std::convert::From<String> for Error {
fn from(s: String) -> Error {
Error::Input(s.to_string())
Error::Input(s)
}
}

Expand Down
4 changes: 2 additions & 2 deletions client/service/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ wasmtime = [
test-helpers = []

[dependencies]
derive_more = "0.99.2"
thiserror = "1.0.21"
futures01 = { package = "futures", version = "0.1.29" }
futures = { version = "0.3.4", features = ["compat"] }
jsonrpc-pubsub = "15.1"
jsonrpc-core = "15.1"
rand = "0.7.3"
parking_lot = "0.10.0"
lazy_static = "1.4.0"
log = "0.4.8"
log = "0.4.11"
slog = { version = "2.5.2", features = ["nested-values"] }
futures-timer = "3.0.1"
wasm-timer = "0.2"
Expand Down
21 changes: 6 additions & 15 deletions client/service/src/client/wasm_override.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,13 @@ 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<HashMap<u32, WasmBlob>> {

let handle_err = |e: std::io::Error | -> sp_blockchain::Error {
sp_blockchain::Error::Msg(format!("{}", e.to_string()))
sp_blockchain::Error::WasmOverrideIo(dir.to_owned(), 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();
Expand All @@ -149,9 +146,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)
Expand Down Expand Up @@ -238,12 +233,8 @@ mod tests {
match scraped {
Err(e) => {
match e {
sp_blockchain::Error::Msg(msg) => {
let is_match = msg
.matches("Duplicate WASM Runtimes found")
.map(ToString::to_string)
.collect::<Vec<String>>();
assert!(is_match.len() >= 1)
sp_blockchain::Error::DuplicateWasmRuntime(duplicates) => {
assert_eq!(duplicates.len(), 1);
},
_ => panic!("Test should end with Msg Error Variant")
}
Expand Down
60 changes: 28 additions & 32 deletions client/service/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,34 @@ use sp_blockchain;
pub type Result<T> = std::result::Result<T, Error>;

/// Service errors.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
/// Client error.
Client(sp_blockchain::Error),
/// IO error.
Io(std::io::Error),
/// Consensus error.
Consensus(sp_consensus::Error),
/// Network error.
Network(sc_network::error::Error),
/// Keystore error.
Keystore(sc_keystore::Error),
/// Best chain selection strategy is missing.
#[display(fmt="Best chain selection strategy (SelectChain) is not provided.")]
#[error(transparent)]
Client(#[from] sp_blockchain::Error),

#[error(transparent)]
Io(#[from] std::io::Error),

#[error(transparent)]
Consensus(#[from] sp_consensus::Error),

#[error(transparent)]
Network(#[from] sc_network::error::Error),

#[error(transparent)]
Keystore(#[from] sc_keystore::Error),

#[error("Best chain selection strategy (SelectChain) is not provided.")]
SelectChainRequired,
/// Tasks executor is missing.
#[display(fmt="Tasks executor hasn't been provided.")]

#[error("Tasks executor hasn't been provided.")]
TaskExecutorRequired,
/// Other error.

#[error("Prometheus metrics error")]
Prometheus(#[from] prometheus_endpoint::PrometheusError),

#[error("Other: {0}")]
Other(String),
}

Expand All @@ -55,21 +64,8 @@ impl<'a> From<&'a str> for Error {
}
}

impl From<prometheus_endpoint::PrometheusError> for Error {
fn from(e: prometheus_endpoint::PrometheusError) -> Self {
Error::Other(format!("Prometheus error: {}", e))
}
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Client(ref err) => Some(err),
Error::Io(ref err) => Some(err),
Error::Consensus(ref err) => Some(err),
Error::Network(ref err) => Some(err),
Error::Keystore(ref err) => Some(err),
_ => None,
}
impl<'a> From<String> for Error {
fn from(s: String) -> Self {
Error::Other(s)
}
}
14 changes: 5 additions & 9 deletions client/sync-state-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,13 @@ impl<TBl, TCl> SyncStateRpcHandler<TBl, TCl>
fn build_sync_state(&self) -> Result<sc_chain_spec::LightSyncState<TBl>, 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,
Expand Down
2 changes: 1 addition & 1 deletion client/transaction-pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.4" }
derive_more = "0.99.2"
thiserror = "1.0.21"
futures = { version = "0.3.1", features = ["compat"] }
futures-diagnose = "1.0"
intervalier = "0.4.0"
Expand Down
30 changes: 11 additions & 19 deletions client/transaction-pool/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,22 @@ use sp_transaction_pool::error::Error as TxPoolError;
pub type Result<T> = std::result::Result<T, Error>;

/// Transaction pool error type.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error {
/// Pool error.
Pool(TxPoolError),
/// Blockchain error.
Blockchain(sp_blockchain::Error),
/// Error while converting a `BlockId`.
#[from(ignore)]
#[error("Transaction pool error")]
Pool(#[from] TxPoolError),

#[error("Blockchain error")]
Blockchain(#[from] sp_blockchain::Error),

#[error("Block conversion error: {0}")]
BlockIdConversion(String),
/// Error while calling the runtime api.
#[from(ignore)]

#[error("Runtime error: {0}")]
RuntimeApi(String),
}

impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Pool(ref err) => Some(err),
Error::Blockchain(ref err) => Some(err),
Error::BlockIdConversion(_) => None,
Error::RuntimeApi(_) => None,
}
}
}

impl sp_transaction_pool::error::IntoPoolError for Error {
fn into_pool_error(self) -> std::result::Result<TxPoolError, Self> {
Expand Down
4 changes: 2 additions & 2 deletions primitives/blockchain/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub trait Backend<Block: BlockT>: HeaderBackend<Block> + HeaderMetadata<Block, E
if let Some(max_number) = maybe_max_number {
loop {
let current_header = self.header(BlockId::Hash(current_hash.clone()))?
.ok_or_else(|| Error::from(format!("failed to get header for hash {}", current_hash)))?;
.ok_or_else(|| Error::MissingHashInHeader(current_hash.to_string()))?;

if current_header.number() <= &max_number {
best_hash = current_header.hash();
Expand All @@ -191,7 +191,7 @@ pub trait Backend<Block: BlockT>: HeaderBackend<Block> + HeaderMetadata<Block, E
}

let current_header = self.header(BlockId::Hash(current_hash.clone()))?
.ok_or_else(|| Error::from(format!("failed to get header for hash {}", current_hash)))?;
.ok_or_else(|| Error::MissingHashInHeader(current_hash.to_string()))?;

// stop search in this chain once we go below the target's block number
if current_header.number() < target_header.number() {
Expand Down
Loading