chore/error: remove from str conversion and add deprecation notificat… (#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 <write@reusable.software>

* 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 <bkchr@users.noreply.github.com>

* remove closure clutter

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* 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 <bkchr@users.noreply.github.com>

* Into -> From

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* remove pointless codec

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

* avoid pointless self import

Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>

Co-authored-by: Bernhard Schuster <bernhard@parity.io>
Co-authored-by: Andronik Ordian <write@reusable.software>
Co-authored-by: Bastian Köcher <bkchr@users.noreply.github.com>
This commit is contained in:
Bernhard Schuster
2020-11-27 19:37:53 +01:00
committed by GitHub
parent 6722a83ba6
commit 8c7d217091
48 changed files with 500 additions and 350 deletions
+38 -6
View File
@@ -74,6 +74,7 @@ use sp_core::OpaqueMetadata;
#[cfg(feature = "std")]
use std::{panic::UnwindSafe, cell::RefCell};
/// Maximum nesting level for extrinsics.
pub const MAX_EXTRINSIC_DEPTH: u32 = 256;
@@ -288,7 +289,7 @@ pub use sp_api_proc_macro::impl_runtime_apis;
/// /// Sets the error type that is being used by the mock implementation.
/// /// The error type is used by all runtime apis. It is only required to
/// /// be specified in one trait implementation.
/// type Error = String;
/// type Error = sp_api::ApiError;
///
/// fn build_block() -> Block {
/// unimplemented!("Not Required in tests")
@@ -315,6 +316,7 @@ pub use sp_api_proc_macro::impl_runtime_apis;
/// # use sp_runtime::{traits::Block as BlockT, generic::BlockId};
/// # use sp_test_primitives::Block;
/// # use sp_core::NativeOrEncoded;
/// # use codec;
/// #
/// # sp_api::decl_runtime_apis! {
/// # /// Declare the api trait.
@@ -331,15 +333,15 @@ pub use sp_api_proc_macro::impl_runtime_apis;
///
/// sp_api::mock_impl_runtime_apis! {
/// impl Balance<Block> for MockApi {
/// type Error = String;
/// type Error = sp_api::ApiError;
/// #[advanced]
/// fn get_balance(&self, at: &BlockId<Block>) -> Result<NativeOrEncoded<u64>, String> {
/// fn get_balance(&self, at: &BlockId<Block>) -> Result<NativeOrEncoded<u64>, Self::Error> {
/// println!("Being called at: {}", at);
///
/// Ok(self.balance.into())
/// }
/// #[advanced]
/// fn set_balance(at: &BlockId<Block>, val: u64) -> Result<NativeOrEncoded<()>, String> {
/// fn set_balance(at: &BlockId<Block>, val: u64) -> Result<NativeOrEncoded<()>, Self::Error> {
/// if let BlockId::Number(1) = at {
/// println!("Being called to set balance to: {}", val);
/// }
@@ -392,12 +394,42 @@ pub trait ConstructRuntimeApi<Block: BlockT, C: CallApiAt<Block>> {
fn construct_runtime_api<'a>(call: &'a C) -> ApiRef<'a, Self::RuntimeApi>;
}
/// An error describing which API call failed.
#[cfg_attr(feature = "std", derive(Debug, thiserror::Error, Eq, PartialEq))]
#[cfg_attr(feature = "std", error("Failed to execute API call {tag}"))]
#[cfg(feature = "std")]
pub struct ApiError {
tag: &'static str,
#[source]
error: codec::Error,
}
#[cfg(feature = "std")]
impl From<(&'static str, codec::Error)> for ApiError {
fn from((tag, error): (&'static str, codec::Error)) -> Self {
Self {
tag,
error,
}
}
}
#[cfg(feature = "std")]
impl ApiError {
pub fn new(tag: &'static str, error: codec::Error) -> Self {
Self {
tag,
error,
}
}
}
/// Extends the runtime api traits with an associated error type. This trait is given as super
/// trait to every runtime api trait.
#[cfg(feature = "std")]
pub trait ApiErrorExt {
/// Error type used by the runtime apis.
type Error: std::fmt::Debug + From<String>;
type Error: std::fmt::Debug + From<ApiError>;
}
/// Extends the runtime api implementation with some common functionality.
@@ -506,7 +538,7 @@ pub struct CallApiAtParams<'a, Block: BlockT, C, NC, Backend: StateBackend<HashF
#[cfg(feature = "std")]
pub trait CallApiAt<Block: BlockT> {
/// Error type used by the implementation.
type Error: std::fmt::Debug + From<String>;
type Error: std::fmt::Debug + From<ApiError>;
/// The state backend that is used to store the block states.
type StateBackend: StateBackend<HashFor<Block>>;