Skip to content

Commit 39b3132

Browse files
drahnrBernhard SchusterAndronik Ordianbkchr
authored andcommitted
chore/error: remove from str conversion and add deprecation notificat… (paritytech#7472)
* chore/error: remove from str conversion and add deprecation notifications * fixup changes * fix test looking for gone ::Msg variant * another test fix * one is duplicate, the other is not, so duplicates reported are n-1 * darn spaces Co-authored-by: Andronik Ordian <[email protected]> * remove pointless doc comments of error variants without any value * low hanging fruits (for a tall person) * moar error type variants * avoid the storage modules for now They are in need of a refactor, and the pain is rather large removing all String error and DefaultError occurences. * chore remove pointless error generic * fix test for mocks, add a bunch of non_exhaustive * max line width * test fixes due to error changes * fin * error outputs... again * undo stderr adjustments * Update client/consensus/slots/src/lib.rs Co-authored-by: Bastian Köcher <[email protected]> * remove closure clutter Co-authored-by: Bastian Köcher <[email protected]> * more error types * introduce ApiError * extract Mock error * ApiError refactor * even more error types * the last for now * chore unused deps * another extraction * reduce should panic, due to extended error messages * error test happiness * shift error lines by one * doc tests * white space Co-authored-by: Bastian Köcher <[email protected]> * Into -> From Co-authored-by: Bastian Köcher <[email protected]> * remove pointless codec Co-authored-by: Bastian Köcher <[email protected]> * avoid pointless self import Co-authored-by: Bastian Köcher <[email protected]> Co-authored-by: Bernhard Schuster <[email protected]> Co-authored-by: Andronik Ordian <[email protected]> Co-authored-by: Bastian Köcher <[email protected]>
1 parent 7bef0db commit 39b3132

File tree

48 files changed

+498
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+498
-348
lines changed

Cargo.lock

Lines changed: 12 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

client/api/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ prometheus-endpoint = { package = "substrate-prometheus-endpoint", version = "0.
4646
kvdb-memorydb = "0.7.0"
4747
sp-test-primitives = { version = "2.0.0", path = "../../primitives/test-primitives" }
4848
substrate-test-runtime = { version = "2.0.0", path = "../../test-utils/runtime" }
49+
thiserror = "1.0.21"

client/api/src/light.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,21 @@ pub mod tests {
312312
use sp_test_primitives::{Block, Header, Extrinsic};
313313
use super::*;
314314

315+
#[derive(Debug, thiserror::Error)]
316+
#[error("Not implemented on test node")]
317+
struct MockError;
318+
319+
impl Into<ClientError> for MockError {
320+
fn into(self) -> ClientError {
321+
ClientError::Application(Box::new(self))
322+
}
323+
}
324+
315325
pub type OkCallFetcher = Mutex<Vec<u8>>;
316326

317-
fn not_implemented_in_tests<T, E>() -> Ready<Result<T, E>>
318-
where
319-
E: std::convert::From<&'static str>,
327+
fn not_implemented_in_tests<T>() -> Ready<Result<T, ClientError>>
320328
{
321-
futures::future::ready(Err("Not implemented on test node".into()))
329+
futures::future::ready(Err(MockError.into()))
322330
}
323331

324332
impl Fetcher<Block> for OkCallFetcher {

client/basic-authorship/src/basic_authorship.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,7 @@ impl<A, B, Block, C> sp_consensus::Proposer<Block> for
208208
}));
209209

210210
async move {
211-
match rx.await {
212-
Ok(x) => x,
213-
Err(err) => Err(sp_blockchain::Error::Msg(err.to_string()))
214-
}
211+
rx.await?
215212
}.boxed()
216213
}
217214
}

client/block-builder/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ where
212212
&state,
213213
changes_trie_state.as_ref(),
214214
parent_hash,
215-
)?;
215+
).map_err(|e| sp_blockchain::Error::StorageChanges(e))?;
216216

217217
Ok(BuiltBlock {
218218
block: <Block as BlockT>::new(header, self.extrinsics),

client/cli/src/error.rs

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,64 +25,61 @@ pub type Result<T> = std::result::Result<T, Error>;
2525

2626
/// Error type for the CLI.
2727
#[derive(Debug, thiserror::Error)]
28+
#[allow(missing_docs)]
2829
pub enum Error {
29-
/// Io error
3030
#[error(transparent)]
3131
Io(#[from] std::io::Error),
32-
/// Cli error
32+
3333
#[error(transparent)]
3434
Cli(#[from] structopt::clap::Error),
35-
/// Service error
35+
3636
#[error(transparent)]
3737
Service(#[from] sc_service::Error),
38-
/// Client error
38+
3939
#[error(transparent)]
4040
Client(#[from] sp_blockchain::Error),
41-
/// scale codec error
41+
4242
#[error(transparent)]
4343
Codec(#[from] parity_scale_codec::Error),
44-
/// Input error
44+
4545
#[error("Invalid input: {0}")]
4646
Input(String),
47-
/// Invalid listen multiaddress
47+
4848
#[error("Invalid listen multiaddress")]
4949
InvalidListenMultiaddress,
50-
/// Application specific error chain sequence forwarder.
51-
#[error(transparent)]
52-
Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
53-
/// URI error.
50+
5451
#[error("Invalid URI; expecting either a secret URI or a public URI.")]
5552
InvalidUri(crypto::PublicError),
56-
/// Signature length mismatch.
53+
5754
#[error("Signature has an invalid length. Read {read} bytes, expected {expected} bytes")]
5855
SignatureInvalidLength {
5956
/// Amount of signature bytes read.
6057
read: usize,
6158
/// Expected number of signature bytes.
6259
expected: usize,
6360
},
64-
/// Missing base path argument.
61+
6562
#[error("The base path is missing, please provide one")]
6663
MissingBasePath,
67-
/// Unknown key type specifier or missing key type specifier.
64+
6865
#[error("Unknown key type, must be a known 4-character sequence")]
6966
KeyTypeInvalid,
70-
/// Signature verification failed.
67+
7168
#[error("Signature verification failed")]
7269
SignatureInvalid,
73-
/// Storing a given key failed.
70+
7471
#[error("Key store operation failed")]
7572
KeyStoreOperation,
76-
/// An issue with the underlying key storage was encountered.
73+
7774
#[error("Key storage issue encountered")]
7875
KeyStorage(#[from] sc_keystore::Error),
79-
/// Bytes are not decodable when interpreted as hexadecimal string.
80-
#[error("Invalid hex base data")]
76+
77+
#[error("Invalid hexadecimal string data")]
8178
HexDataConversion(#[from] hex::FromHexError),
82-
/// Shortcut type to specify types on the fly, discouraged.
83-
#[deprecated = "Use `Forwarded` with an error type instead."]
84-
#[error("Other: {0}")]
85-
Other(String),
79+
80+
/// Application specific error chain sequence forwarder.
81+
#[error(transparent)]
82+
Application(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
8683
}
8784

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

9491
impl std::convert::From<String> for Error {
9592
fn from(s: String) -> Error {
96-
Error::Input(s.to_string())
93+
Error::Input(s)
9794
}
9895
}
9996

client/consensus/slots/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ sp-inherents = { version = "2.0.0", path = "../../../primitives/inherents" }
3131
futures = "0.3.4"
3232
futures-timer = "3.0.1"
3333
parking_lot = "0.10.0"
34-
log = "0.4.8"
34+
log = "0.4.11"
35+
thiserror = "1.0.21"
3536

3637
[dev-dependencies]
3738
substrate-test-runtime-client = { version = "2.0.0", path = "../../../test-utils/runtime/client" }

client/consensus/slots/src/lib.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
//! time during which certain events can and/or must occur. This crate
2121
//! provides generic functionality for slots.
2222
23-
#![forbid(unsafe_code, missing_docs)]
23+
#![forbid(unsafe_code)]
24+
#![deny(missing_docs)]
2425

2526
mod slots;
2627
mod aux_schema;
@@ -470,6 +471,15 @@ pub enum CheckedHeader<H, S> {
470471
Checked(H, S),
471472
}
472473

474+
475+
476+
#[derive(Debug, thiserror::Error)]
477+
#[allow(missing_docs)]
478+
pub enum Error<T> where T: SlotData + Clone + Debug + Send + Sync + 'static {
479+
#[error("Slot duration is invalid: {0:?}")]
480+
SlotDurationInvalid(SlotDuration<T>),
481+
}
482+
473483
/// A slot duration. Create with `get_or_compute`.
474484
// The internal member should stay private here to maintain invariants of
475485
// `get_or_compute`.
@@ -494,7 +504,7 @@ impl<T: SlotData + Clone> SlotData for SlotDuration<T> {
494504
const SLOT_KEY: &'static [u8] = T::SLOT_KEY;
495505
}
496506

497-
impl<T: Clone> SlotDuration<T> {
507+
impl<T: Clone + Send + Sync + 'static> SlotDuration<T> {
498508
/// Either fetch the slot duration from disk or compute it from the
499509
/// genesis state.
500510
///
@@ -532,10 +542,8 @@ impl<T: Clone> SlotDuration<T> {
532542
}
533543
}?;
534544

535-
if slot_duration.slot_duration() == 0 {
536-
return Err(sp_blockchain::Error::Msg(
537-
"Invalid value for slot_duration: the value must be greater than 0.".into(),
538-
))
545+
if slot_duration.slot_duration() == 0u64 {
546+
return Err(sp_blockchain::Error::Application(Box::new(Error::SlotDurationInvalid(slot_duration))))
539547
}
540548

541549
Ok(slot_duration)
@@ -939,7 +947,7 @@ mod test {
939947
true, true, true, true,
940948
];
941949

942-
assert_eq!(backoff, expected);
950+
assert_eq!(backoff.as_slice(), &expected[..]);
943951
}
944952

945953
#[test]

client/db/src/lib.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -891,9 +891,7 @@ impl<Block: BlockT> Backend<Block> {
891891
let is_archive_pruning = config.pruning.is_archive();
892892
let blockchain = BlockchainDb::new(db.clone())?;
893893
let meta = blockchain.meta.clone();
894-
let map_e = |e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from(
895-
format!("State database error: {:?}", e)
896-
);
894+
let map_e = |e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from_state_db(e);
897895
let state_db: StateDb<_, _> = StateDb::new(
898896
config.pruning.clone(),
899897
!config.source.supports_ref_counting(),
@@ -1082,7 +1080,7 @@ impl<Block: BlockT> Backend<Block> {
10821080

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

@@ -1212,9 +1210,7 @@ impl<Block: BlockT> Backend<Block> {
12121210
number_u64,
12131211
&pending_block.header.parent_hash(),
12141212
changeset,
1215-
).map_err(|e: sc_state_db::Error<io::Error>|
1216-
sp_blockchain::Error::from(format!("State database error: {:?}", e))
1217-
)?;
1213+
).map_err(|e: sc_state_db::Error<io::Error>| sp_blockchain::Error::from_state_db(e))?;
12181214
apply_state_commit(&mut transaction, commit);
12191215

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

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

13851381
if !f_num.is_zero() {

client/executor/common/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ readme = "README.md"
1414
targets = ["x86_64-unknown-linux-gnu"]
1515

1616
[dependencies]
17-
log = "0.4.8"
1817
derive_more = "0.99.2"
1918
parity-wasm = "0.41.0"
2019
codec = { package = "parity-scale-codec", version = "1.3.4" }
2120
wasmi = "0.6.2"
2221
sp-core = { version = "2.0.0", path = "../../../primitives/core" }
2322
sp-allocator = { version = "2.0.0", path = "../../../primitives/allocator" }
2423
sp-wasm-interface = { version = "2.0.0", path = "../../../primitives/wasm-interface" }
25-
sp-runtime-interface = { version = "2.0.0", path = "../../../primitives/runtime-interface" }
2624
sp-serializer = { version = "2.0.0", path = "../../../primitives/serializer" }
25+
thiserror = "1.0.21"
2726

2827
[features]
2928
default = []

0 commit comments

Comments
 (0)