diff --git a/Cargo.lock b/Cargo.lock index 90c9fc0..7f9cbc8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -683,7 +683,7 @@ dependencies = [ "semver 1.0.26", "serde", "serde_json", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -2875,6 +2875,7 @@ version = "0.1.0" dependencies = [ "polkavm", "pvq-primitives", + "thiserror 2.0.12", "tracing", ] @@ -2890,6 +2891,7 @@ dependencies = [ "pvq-primitives", "scale-info", "serde", + "thiserror 2.0.12", "tracing", "tracing-subscriber", ] @@ -3083,7 +3085,7 @@ checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43" dependencies = [ "getrandom 0.2.15", "libredox", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -3556,7 +3558,7 @@ dependencies = [ "sp-state-machine", "sp-trie", "sp-version", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -3702,7 +3704,7 @@ dependencies = [ "sp-storage", "ss58-registry", "substrate-bip39", - "thiserror", + "thiserror 1.0.69", "tracing", "w3f-bls", "zeroize", @@ -3779,7 +3781,7 @@ dependencies = [ "parity-scale-codec", "scale-info", "sp-runtime", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -3838,7 +3840,7 @@ version = "11.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f0c768c11afbe698a090386876911da4236af199cd38a5866748df4d8628aeff" dependencies = [ - "thiserror", + "thiserror 1.0.69", "zstd", ] @@ -3983,7 +3985,7 @@ dependencies = [ "sp-externalities", "sp-panic-handler", "sp-trie", - "thiserror", + "thiserror 1.0.69", "tracing", "trie-db", ] @@ -4017,7 +4019,7 @@ dependencies = [ "parity-scale-codec", "sp-inherents", "sp-runtime", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -4059,7 +4061,7 @@ dependencies = [ "schnellru", "sp-core", "sp-externalities", - "thiserror", + "thiserror 1.0.69", "tracing", "trie-db", "trie-root", @@ -4080,7 +4082,7 @@ dependencies = [ "sp-runtime", "sp-std", "sp-version-proc-macro", - "thiserror", + "thiserror 1.0.69", ] [[package]] @@ -4310,7 +4312,16 @@ version = "1.0.69" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52" dependencies = [ - "thiserror-impl", + "thiserror-impl 1.0.69", +] + +[[package]] +name = "thiserror" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708" +dependencies = [ + "thiserror-impl 2.0.12", ] [[package]] @@ -4324,6 +4335,17 @@ dependencies = [ "syn 2.0.100", ] +[[package]] +name = "thiserror-impl" +version = "2.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.100", +] + [[package]] name = "thread_local" version = "1.1.8" @@ -4714,7 +4736,7 @@ dependencies = [ "strum 0.24.1", "strum_macros 0.24.3", "tempfile", - "thiserror", + "thiserror 1.0.69", "wasm-opt-cxx-sys", "wasm-opt-sys", ] diff --git a/Cargo.toml b/Cargo.toml index 4cb41cc..6f24267 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -77,6 +77,7 @@ scale-info = { version = "2.11.3", default-features = false, features = [ tracing = { version = "0.1.40", default-features = false } serde = { version = "1.0.215", default-features = false, features = ["derive"] } serde_json = { version = "1.0.110", default-features = false } +thiserror = { version = "2", default-features = false } # std clap = { version = "4.5.4", features = ["derive"] } diff --git a/pvq-executor/Cargo.toml b/pvq-executor/Cargo.toml index 795a8e6..bbb23e7 100644 --- a/pvq-executor/Cargo.toml +++ b/pvq-executor/Cargo.toml @@ -14,6 +14,8 @@ polkavm = { workspace = true } pvq-primitives = { workspace = true } +thiserror = { workspace = true } + [features] default = ["std"] -std = ["tracing/std", "polkavm/std", "pvq-primitives/std"] +std = ["tracing/std", "polkavm/std", "pvq-primitives/std", "thiserror/std"] diff --git a/pvq-executor/src/error.rs b/pvq-executor/src/error.rs index 4d624f2..8aba798 100644 --- a/pvq-executor/src/error.rs +++ b/pvq-executor/src/error.rs @@ -1,30 +1,24 @@ use pvq_primitives::PvqError; -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum PvqExecutorError { + #[error("Invalid PVQ program format")] InvalidProgramFormat, + #[error("Memory access error: {0}")] MemoryAccessError(polkavm::MemoryAccessError), // Extract from the PVM CallError + #[error("Trap")] Trap, // Extract from the PVM CallError + #[error("Not enough gas")] NotEnoughGas, // Usually a custom error type from the extension system definition + #[error("User error: {0}")] User(UserError), // Other errors directly from the PVM + #[error("Other PVM error: {0}")] OtherPvmError(polkavm::Error), } -impl From for PvqExecutorError { - fn from(err: polkavm::MemoryAccessError) -> Self { - Self::MemoryAccessError(err) - } -} - -impl From for PvqExecutorError { - fn from(err: polkavm::Error) -> Self { - Self::OtherPvmError(err) - } -} - impl From> for PvqExecutorError { fn from(err: polkavm::CallError) -> Self { match err { @@ -36,6 +30,33 @@ impl From> for PvqExecutorError From for PvqExecutorError { + fn from(e: polkavm::Error) -> Self { + Self::OtherPvmError(e) + } +} + +impl From for PvqExecutorError { + fn from(e: polkavm::MemoryAccessError) -> Self { + Self::MemoryAccessError(e) + } +} + +#[cfg(feature = "std")] +impl From> for PvqError { + fn from(e: PvqExecutorError) -> PvqError { + match e { + PvqExecutorError::InvalidProgramFormat => "Invalid PVQ program format".to_string(), + PvqExecutorError::MemoryAccessError(_) => "Memory access error".to_string(), + PvqExecutorError::Trap => "Trap".to_string(), + PvqExecutorError::NotEnoughGas => "Not enough gas".to_string(), + PvqExecutorError::User(user_error) => format!("Host call error: {user_error:?}"), + PvqExecutorError::OtherPvmError(pvm_error) => format!("Other error: {pvm_error:?}"), + } + } +} + +#[cfg(not(feature = "std"))] impl From> for PvqError { fn from(e: PvqExecutorError) -> PvqError { match e { diff --git a/pvq-extension/Cargo.toml b/pvq-extension/Cargo.toml index c1ee690..5e543b7 100644 --- a/pvq-extension/Cargo.toml +++ b/pvq-extension/Cargo.toml @@ -17,6 +17,7 @@ parity-scale-codec = { workspace = true } scale-info = { workspace = true } pvq-primitives = { workspace = true } serde = { workspace = true } +thiserror = { workspace = true } [dev-dependencies] tracing-subscriber = { workspace = true } @@ -31,4 +32,5 @@ std = [ "scale-info/std", "tracing/std", "serde/std", + "thiserror/std", ] diff --git a/pvq-extension/src/calldata.rs b/pvq-extension/src/calldata.rs index 94385a5..3810f25 100644 --- a/pvq-extension/src/calldata.rs +++ b/pvq-extension/src/calldata.rs @@ -15,8 +15,9 @@ pub trait Dispatchable { } /// Error type for dispatch operations -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum DispatchError { + #[error("PhantomData")] PhantomData, } diff --git a/pvq-extension/src/error.rs b/pvq-extension/src/error.rs index 617984f..d3ac67f 100644 --- a/pvq-extension/src/error.rs +++ b/pvq-extension/src/error.rs @@ -1,48 +1,36 @@ // TODO: contain source error use crate::DispatchError; use parity_scale_codec::Error as CodecError; -use scale_info::prelude::fmt; -use scale_info::prelude::fmt::{Display, Formatter}; /// Errors that can occur when working with extensions // Typically will be used as a UserError -#[derive(Debug)] +#[derive(Debug, thiserror::Error)] pub enum ExtensionError { /// Permission denied for the requested operation + #[error("Permission denied")] PermissionError, /// Failed to allocate memory + #[error("Failed to allocate memory")] MemoryAllocationError, /// Error accessing memory + #[error("Memory access error: {0}")] MemoryAccessError(polkavm::MemoryAccessError), /// Error decoding data + #[error("Decode error: {0}")] DecodeError(CodecError), /// Error dispatching a call - DispatchError(DispatchError), + #[error("Dispatch error: {0:?}")] + DispatchError(#[from] DispatchError), /// The requested extension is not supported + #[error("Unsupported extension")] UnsupportedExtension, } -impl Display for ExtensionError { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - match self { - Self::PermissionError => write!(f, "Permission denied"), - Self::MemoryAllocationError => write!(f, "Failed to allocate memory"), - Self::MemoryAccessError(e) => write!(f, "Memory access error: {:?}", e), - Self::DecodeError(e) => write!(f, "Decode error: {:?}", e), - Self::DispatchError(e) => write!(f, "Dispatch error: {:?}", e), - Self::UnsupportedExtension => write!(f, "Unsupported extension"), - } - } -} - -#[cfg(feature = "std")] -impl std::error::Error for ExtensionError {} - impl From for ExtensionError { fn from(e: polkavm::MemoryAccessError) -> Self { Self::MemoryAccessError(e) @@ -54,9 +42,3 @@ impl From for ExtensionError { Self::DecodeError(e) } } - -impl From for ExtensionError { - fn from(e: DispatchError) -> Self { - Self::DispatchError(e) - } -} diff --git a/pvq-primitives/src/lib.rs b/pvq-primitives/src/lib.rs index cfbac95..7f4f93d 100644 --- a/pvq-primitives/src/lib.rs +++ b/pvq-primitives/src/lib.rs @@ -3,13 +3,19 @@ extern crate alloc; use alloc::vec::Vec; +#[cfg(not(feature = "std"))] use parity_scale_codec::{Decode, Encode}; +#[cfg(not(feature = "std"))] use scale_info::TypeInfo; pub type PvqResult = Result; pub type PvqResponse = Vec; +#[cfg(feature = "std")] +pub type PvqError = String; + +#[cfg(not(feature = "std"))] #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, TypeInfo)] pub enum PvqError { FailedToDecode,