Unverified Commit ac9bc28a authored by Bernhard Schuster's avatar Bernhard Schuster Committed by GitHub
Browse files

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's avatarAndronik Ordian <write@reusable.software>
Co-authored-by: parity-processbot <>
parent f6297ba8
Pipeline #112478 passed with stages
in 26 minutes and 35 seconds
This diff is collapsed.
......@@ -14,6 +14,7 @@ readme = "README.md"
[dependencies]
cli = { package = "polkadot-cli", path = "cli" }
color-eyre = "0.5.6"
thiserror = "1"
futures = "0.3.4"
service = { package = "polkadot-service", path = "node/service" }
parity-util-mem = { version = "*", default-features = false, features = ["jemalloc-global"] }
......
......@@ -15,6 +15,7 @@ crate-type = ["cdylib", "rlib"]
[dependencies]
log = "0.4.11"
thiserror = "1.0.21"
structopt = { version = "0.3.8", optional = true }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" }
sc-tracing = { git = "https://github.com/paritytech/substrate", branch = "master" }
......
......@@ -17,6 +17,7 @@ sp-wasm-interface = { git = "https://github.com/paritytech/substrate", branch =
polkadot-core-primitives = { path = "../core-primitives", default-features = false }
# all optional crates.
thiserror = { version = "1.0.21", optional = true }
derive_more = { version = "0.99.11", 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 }
......@@ -34,6 +35,7 @@ default = ["std"]
wasm-api = []
std = [
"codec/std",
"thiserror",
"derive_more",
"serde/std",
"sp-std/std",
......
......@@ -81,63 +81,61 @@ pub enum ExecutionMode {
}
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
/// Candidate validation error.
pub enum ValidationError {
/// Validation failed due to internal reasons. The candidate might still be valid.
Internal(InternalError),
#[error(transparent)]
Internal(#[from] InternalError),
/// Candidate is invalid.
InvalidCandidate(InvalidCandidate),
#[error(transparent)]
InvalidCandidate(#[from] InvalidCandidate),
}
/// Error type that indicates invalid candidate.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum InvalidCandidate {
/// Wasm executor error.
#[display(fmt = "WASM executor error: {:?}", _0)]
WasmExecutor(sc_executor::error::Error),
#[error("WASM executor error")]
WasmExecutor(#[from] sc_executor::error::Error),
/// Call data is too large.
#[display(fmt = "Validation parameters are {} bytes, max allowed is {}", _0, MAX_RUNTIME_MEM)]
#[from(ignore)]
#[error("Validation parameters are {0} bytes, max allowed is {}", MAX_RUNTIME_MEM)]
ParamsTooLarge(usize),
/// 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),
/// Error decoding returned data.
#[display(fmt = "Validation function returned invalid data.")]
#[error("Validation function returned invalid data.")]
BadReturn,
#[display(fmt = "Validation function timeout.")]
#[error("Validation function timeout.")]
Timeout,
#[display(fmt = "External WASM execution error: {}", _0)]
#[error("External WASM execution error: {0}")]
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.
#[derive(Debug, derive_more::Display, derive_more::From)]
#[derive(Debug, thiserror::Error)]
pub enum InternalError {
#[display(fmt = "IO error: {}", _0)]
Io(std::io::Error),
#[display(fmt = "System error: {}", _0)]
System(Box<dyn std::error::Error + Send>),
#[display(fmt = "Shared memory error: {}", _0)]
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("System error: {0}")]
System(#[from] Box<dyn std::error::Error + Send + Sync + 'static>),
#[cfg(not(any(target_os = "android", target_os = "unknown")))]
SharedMem(shared_memory::SharedMemError),
#[display(fmt = "WASM worker error: {}", _0)]
#[error("Shared memory error: {0}")]
SharedMem(#[from] shared_memory::SharedMemError),
#[error("WASM worker error: {0}")]
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.
///
......
......@@ -20,40 +20,8 @@
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<()> {
color_eyre::install()?;
cli::run().map_err(|e| ErrorWrapper(std::sync::Arc::new(e)))?;
cli::run()?;
Ok(())
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment