Skip to content
This repository has been 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 35 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
16 changes: 12 additions & 4 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions client/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.
kvdb-memorydb = "0.7.0"
sp-test-primitives = { version = "2.0.0", path = "../../primitives/test-primitives" }
substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" }
thiserror = "1.0.21"
16 changes: 12 additions & 4 deletions client/api/src/light.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,13 +312,21 @@ pub mod tests {
use sp_test_primitives::{Block, Header, Extrinsic};
use super::*;

#[derive(Debug, thiserror::Error)]
#[error("Not implemented on test node")]
struct MockError;

impl Into<ClientError> for MockError {
fn into(self) -> ClientError {
ClientError::Application(Box::new(self))
}
}

pub type OkCallFetcher = Mutex<Vec<u8>>;

fn not_implemented_in_tests<T, E>() -> Ready<Result<T, E>>
where
E: std::convert::From<&'static str>,
fn not_implemented_in_tests<T>() -> Ready<Result<T, ClientError>>
{
futures::future::ready(Err("Not implemented on test node".into()))
futures::future::ready(Err(MockError.into()))
}

impl Fetcher<Block> for OkCallFetcher {
Expand Down
5 changes: 1 addition & 4 deletions client/basic-authorship/src/basic_authorship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,10 +208,7 @@ impl<A, B, Block, C> sp_consensus::Proposer<Block> for
}));

async move {
match rx.await {
Ok(x) => x,
Err(err) => Err(sp_blockchain::Error::Msg(err.to_string()))
}
rx.await?
}.boxed()
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/block-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ where
&state,
changes_trie_state.as_ref(),
parent_hash,
)?;
).map_err(|e| sp_blockchain::Error::StorageChanges(e))?;

Ok(BuiltBlock {
block: <Block as BlockT>::new(header, self.extrinsics),
Expand Down
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
3 changes: 2 additions & 1 deletion client/consensus/slots/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" }
futures = "0.3.4"
futures-timer = "3.0.1"
parking_lot = "0.10.0"
log = "0.4.8"
log = "0.4.11"
thiserror = "1.0.21"

[dev-dependencies]
substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" }
22 changes: 15 additions & 7 deletions client/consensus/slots/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
//! time during which certain events can and/or must occur. This crate
//! provides generic functionality for slots.

#![forbid(unsafe_code, missing_docs)]
#![forbid(unsafe_code)]
#![deny(missing_docs)]

mod slots;
mod aux_schema;
Expand Down Expand Up @@ -470,6 +471,15 @@ pub enum CheckedHeader<H, S> {
Checked(H, S),
}



#[derive(Debug, thiserror::Error)]
#[allow(missing_docs)]
pub enum Error<T> where T: SlotData + Clone + Debug + Send + Sync + 'static {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need all this bounds?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Putting bounds on the declaration of a struct should be prevented as much as possible. And I don't see any reason here?

Copy link
Contributor Author

@drahnr drahnr Nov 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug + Send + Sync + 'static are required, otherwise Error<T> will not be Send or Sync. Slot data is there for features. Clone can be removed though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You only need the bounds on T where you need to have Error Send and Sync

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#[error("Slot duration is invalid: {0:?}")]
SlotDurationInvalid(SlotDuration<T>),
}

/// A slot duration. Create with `get_or_compute`.
// The internal member should stay private here to maintain invariants of
// `get_or_compute`.
Expand All @@ -494,7 +504,7 @@ impl<T: SlotData + Clone> SlotData for SlotDuration<T> {
const SLOT_KEY: &'static [u8] = T::SLOT_KEY;
}

impl<T: Clone> SlotDuration<T> {
impl<T: Clone + Send + Sync + 'static> SlotDuration<T> {
/// Either fetch the slot duration from disk or compute it from the
/// genesis state.
///
Expand Down Expand Up @@ -532,10 +542,8 @@ impl<T: Clone> SlotDuration<T> {
}
}?;

if slot_duration.slot_duration() == 0 {
return Err(sp_blockchain::Error::Msg(
"Invalid value for slot_duration: the value must be greater than 0.".into(),
))
if slot_duration.slot_duration() == 0u64 {
return Err(sp_blockchain::Error::Application(Box::new(Error::SlotDurationInvalid(slot_duration))))
}

Ok(slot_duration)
Expand Down Expand Up @@ -939,7 +947,7 @@ mod test {
true, true, true, true,
];

assert_eq!(backoff, expected);
assert_eq!(backoff.as_slice(), &expected[..]);
}

#[test]
Expand Down
12 changes: 4 additions & 8 deletions client/db/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,7 @@ impl<Block: BlockT> Backend<Block> {
let is_archive_pruning = config.pruning.is_archive();
let blockchain = BlockchainDb::new(db.clone())?;
let meta = blockchain.meta.clone();
let map_e = |e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from(
format!("State database error: {:?}", e)
);
let map_e = |e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from_state_db(e);
let state_db: StateDb<_, _> = StateDb::new(
config.pruning.clone(),
!config.source.supports_ref_counting(),
Expand Down Expand Up @@ -1082,7 +1080,7 @@ impl<Block: BlockT> Backend<Block> {

trace!(target: "db", "Canonicalize block #{} ({:?})", new_canonical, hash);
let commit = self.storage.state_db.canonicalize_block(&hash)
.map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from(format!("State database error: {:?}", e)))?;
.map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from_state_db(e))?;
apply_state_commit(transaction, commit);
};

Expand Down Expand Up @@ -1212,9 +1210,7 @@ impl<Block: BlockT> Backend<Block> {
number_u64,
&pending_block.header.parent_hash(),
changeset,
).map_err(|e: sc_state_db::Error<io::Error>|
sp_blockchain::Error::from(format!("State database error: {:?}", e))
)?;
).map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from_state_db(e))?;
apply_state_commit(&mut transaction, commit);

// Check if need to finalize. Genesis is always finalized instantly.
Expand Down Expand Up @@ -1379,7 +1375,7 @@ impl<Block: BlockT> Backend<Block> {
transaction.set_from_vec(columns::META, meta_keys::FINALIZED_BLOCK, lookup_key);

let commit = self.storage.state_db.canonicalize_block(&f_hash)
.map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from(format!("State database error: {:?}", e)))?;
.map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from_state_db(e))?;
apply_state_commit(transaction, commit);

if !f_num.is_zero() {
Expand Down
3 changes: 1 addition & 2 deletions client/executor/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,15 @@ readme = "README.md"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
log = "0.4.8"
derive_more = "0.99.2"
parity-wasm = "0.41.0"
codec = { package = "parity-scale-codec", version = "1.3.4" }
wasmi = "0.6.2"
sp-core = { version = "2.0.0", path = "../../../primitives/core" }
sp-allocator = { version = "2.0.0", path = "../../../primitives/allocator" }
sp-wasm-interface = { version = "2.0.0", path = "../../../primitives/wasm-interface" }
sp-runtime-interface = { version = "2.0.0", path = "../../../primitives/runtime-interface" }
sp-serializer = { version = "2.0.0", path = "../../../primitives/serializer" }
thiserror = "1.0.21"

[features]
default = []
Loading