remove error wrapper type (#1871)

* get rid of glue wrapper error type

* cargo update -p sp-io

* cargo update -p sp-io

* "Update Substrate"

Co-authored-by: Andronik Ordian <write@reusable.software>
Co-authored-by: parity-processbot <>
This commit is contained in:
Bernhard Schuster
2020-10-28 17:42:23 +01:00
committed by GitHub
parent bbf7fc8d0b
commit 8a305ac963
6 changed files with 180 additions and 204 deletions
+145 -139
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -14,6 +14,7 @@ readme = "README.md"
[dependencies] [dependencies]
cli = { package = "polkadot-cli", path = "cli" } cli = { package = "polkadot-cli", path = "cli" }
color-eyre = "0.5.6" color-eyre = "0.5.6"
thiserror = "1"
futures = "0.3.4" futures = "0.3.4"
service = { package = "polkadot-service", path = "node/service" } service = { package = "polkadot-service", path = "node/service" }
parity-util-mem = { version = "*", default-features = false, features = ["jemalloc-global"] } parity-util-mem = { version = "*", default-features = false, features = ["jemalloc-global"] }
+1
View File
@@ -15,6 +15,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies] [dependencies]
log = "0.4.11" log = "0.4.11"
thiserror = "1.0.21"
structopt = { version = "0.3.8", optional = true } structopt = { version = "0.3.8", optional = true }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" } sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" }
+2
View File
@@ -17,6 +17,7 @@ sp-wasm-interface = { git = "https://github.com/paritytech/substrate", branch =
polkadot-core-primitives = { path = "../core-primitives", default-features = false } polkadot-core-primitives = { path = "../core-primitives", default-features = false }
# all optional crates. # all optional crates.
thiserror = { version = "1.0.21", optional = true }
derive_more = { version = "0.99.11", optional = true } derive_more = { version = "0.99.11", optional = true }
serde = { version = "1.0.102", default-features = false, features = [ "derive" ], optional = true } serde = { version = "1.0.102", default-features = false, features = [ "derive" ], optional = true }
sp-externalities = { git = "https://github.com/paritytech/substrate", branch = "master", optional = true } sp-externalities = { git = "https://github.com/paritytech/substrate", branch = "master", optional = true }
@@ -34,6 +35,7 @@ default = ["std"]
wasm-api = [] wasm-api = []
std = [ std = [
"codec/std", "codec/std",
"thiserror",
"derive_more", "derive_more",
"serde/std", "serde/std",
"sp-std/std", "sp-std/std",
+30 -32
View File
@@ -81,63 +81,61 @@ pub enum ExecutionMode {
} }
#[derive(Debug, derive_more::Display, derive_more::From)] #[derive(Debug, thiserror::Error)]
/// Candidate validation error. /// Candidate validation error.
pub enum ValidationError { pub enum ValidationError {
/// Validation failed due to internal reasons. The candidate might still be valid. /// Validation failed due to internal reasons. The candidate might still be valid.
Internal(InternalError), #[error(transparent)]
Internal(#[from] InternalError),
/// Candidate is invalid. /// Candidate is invalid.
InvalidCandidate(InvalidCandidate), #[error(transparent)]
InvalidCandidate(#[from] InvalidCandidate),
} }
/// Error type that indicates invalid candidate. /// Error type that indicates invalid candidate.
#[derive(Debug, derive_more::Display, derive_more::From)] #[derive(Debug, thiserror::Error)]
pub enum InvalidCandidate { pub enum InvalidCandidate {
/// Wasm executor error. /// Wasm executor error.
#[display(fmt = "WASM executor error: {:?}", _0)] #[error("WASM executor error")]
WasmExecutor(sc_executor::error::Error), WasmExecutor(#[from] sc_executor::error::Error),
/// Call data is too large. /// Call data is too large.
#[display(fmt = "Validation parameters are {} bytes, max allowed is {}", _0, MAX_RUNTIME_MEM)] #[error("Validation parameters are {0} bytes, max allowed is {}", MAX_RUNTIME_MEM)]
#[from(ignore)]
ParamsTooLarge(usize), ParamsTooLarge(usize),
/// Code size it too large. /// Code size it too large.
#[display(fmt = "WASM code is {} bytes, max allowed is {}", _0, MAX_CODE_MEM)] #[error("WASM code is {0} bytes, max allowed is {}", MAX_CODE_MEM)]
CodeTooLarge(usize), CodeTooLarge(usize),
/// Error decoding returned data. /// Error decoding returned data.
#[display(fmt = "Validation function returned invalid data.")] #[error("Validation function returned invalid data.")]
BadReturn, BadReturn,
#[display(fmt = "Validation function timeout.")] #[error("Validation function timeout.")]
Timeout, Timeout,
#[display(fmt = "External WASM execution error: {}", _0)] #[error("External WASM execution error: {0}")]
ExternalWasmExecutor(String), ExternalWasmExecutor(String),
} }
impl core::convert::From<String> for InvalidCandidate {
fn from(s: String) -> Self {
Self::ExternalWasmExecutor(s)
}
}
/// Host error during candidate validation. This does not indicate an invalid candidate. /// Host error during candidate validation. This does not indicate an invalid candidate.
#[derive(Debug, derive_more::Display, derive_more::From)] #[derive(Debug, thiserror::Error)]
pub enum InternalError { pub enum InternalError {
#[display(fmt = "IO error: {}", _0)] #[error("IO error: {0}")]
Io(std::io::Error), Io(#[from] std::io::Error),
#[display(fmt = "System error: {}", _0)]
System(Box<dyn std::error::Error + Send>), #[error("System error: {0}")]
#[display(fmt = "Shared memory error: {}", _0)] System(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
#[cfg(not(any(target_os = "android", target_os = "unknown")))] #[cfg(not(any(target_os = "android", target_os = "unknown")))]
SharedMem(shared_memory::SharedMemError), #[error("Shared memory error: {0}")]
#[display(fmt = "WASM worker error: {}", _0)] SharedMem(#[from] shared_memory::SharedMemError),
#[error("WASM worker error: {0}")]
WasmWorker(String), WasmWorker(String),
} }
impl std::error::Error for ValidationError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
ValidationError::Internal(InternalError::Io(ref err)) => Some(err),
ValidationError::Internal(InternalError::System(ref err)) => Some(&**err),
#[cfg(not(any(target_os = "android", target_os = "unknown")))]
ValidationError::Internal(InternalError::SharedMem(ref err)) => Some(err),
ValidationError::InvalidCandidate(InvalidCandidate::WasmExecutor(ref err)) => Some(err),
_ => None,
}
}
}
/// Validate a candidate under the given validation code. /// Validate a candidate under the given validation code.
/// ///
+1 -33
View File
@@ -20,40 +20,8 @@
use color_eyre::eyre; use color_eyre::eyre;
use cli::Error as PolkaError;
use std::{error, fmt};
/// A helper to satisfy the requirements of `eyre`
/// compatible errors, which require `Send + Sync`
/// which are not satisfied by the `sp_*` crates.
#[derive(Debug)]
struct ErrorWrapper(std::sync::Arc<PolkaError>);
// nothing is going to be sent to another thread
// it merely exists to glue two distinct error
// types together where the requirements differ
// with `Sync + Send` and without them for `wasm`.
unsafe impl Sync for ErrorWrapper {}
unsafe impl Send for ErrorWrapper {}
impl error::Error for ErrorWrapper {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
(&*self.0).source().and_then(|e| e.source())
}
fn description(&self) -> &str {
"Error Wrapper"
}
}
impl fmt::Display for ErrorWrapper {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", &*self.0)
}
}
fn main() -> eyre::Result<()> { fn main() -> eyre::Result<()> {
color_eyre::install()?; color_eyre::install()?;
cli::run().map_err(|e| ErrorWrapper(std::sync::Arc::new(e)))?; cli::run()?;
Ok(()) Ok(())
} }