From 8d0df0e99f0dbd7fa2e433199cd10a9abe015121 Mon Sep 17 00:00:00 2001 From: Nuno Alexandre Date: Mon, 20 Apr 2020 14:13:49 +0200 Subject: [PATCH 1/2] core: Add thiserror as a dependency --- Cargo.lock | 1 + core/Cargo.toml | 1 + 2 files changed, 2 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index b585c7cb..267aedca 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3699,6 +3699,7 @@ dependencies = [ "rand 0.7.3", "sp-core", "sp-runtime", + "thiserror", ] [[package]] diff --git a/core/Cargo.toml b/core/Cargo.toml index 9abe3f77..f2ebeb0b 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -21,6 +21,7 @@ std = [ [dependencies] derive-try-from-primitive = "1.0.0" rand = { version = "0.7.2", optional = true } +thiserror = "1.0" [dependencies.parity-scale-codec] default-features = false From f0789165d104030d6c5d6eece363fe559fb14092 Mon Sep 17 00:00:00 2001 From: Nuno Alexandre Date: Mon, 20 Apr 2020 14:33:50 +0200 Subject: [PATCH 2/2] core: Make RegistryError derive thiserror --- cli/src/lib.rs | 2 +- core/src/error.rs | 77 +++++++++++++++++------------------------ runtime/src/registry.rs | 2 +- 3 files changed, 34 insertions(+), 47 deletions(-) diff --git a/cli/src/lib.rs b/cli/src/lib.rs index e0f5225d..5e902b8f 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -151,7 +151,7 @@ pub enum CommandError { /// The subset of possible errors having led a transaction to failure. #[derive(Debug, ThisError)] pub enum TransactionError { - #[error("{0}")] + #[error(transparent)] RegistryError(RegistryError), #[error("{0:?}")] diff --git a/core/src/error.rs b/core/src/error.rs index 301003ae..6d4bd772 100644 --- a/core/src/error.rs +++ b/core/src/error.rs @@ -17,66 +17,53 @@ use crate::DispatchError; use derive_try_from_primitive::TryFromPrimitive; use std::convert::{TryFrom, TryInto}; +use thiserror::Error as ThisError; -#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)] +#[derive(Clone, Copy, Debug, Eq, PartialEq, ThisError, TryFromPrimitive)] #[repr(u8)] /// Errors describing failed Registry transactions. pub enum RegistryError { + #[error("the provided checkpoint does not exist")] InexistentCheckpointId = 0, + + #[error("a registered project must have an initial checkpoint")] + InexistentInitialProjectCheckpoint, + + #[error("the provided org does not exist")] InexistentOrg, + + #[error("the provided project does not exist")] + InexistentProjectId, + + #[error("the provided user does not exist")] InexistentUser, + + #[error("an org with the same ID already exists")] DuplicateOrgId, + + #[error("a project with the same ID already exists")] DuplicateProjectId, + + #[error("a user with the same ID already exists.")] DuplicateUserId, - InexistentProjectId, + + #[error("the provided fee is insufficient")] InsufficientFee, + + #[error("the sender is not a project member")] InsufficientSenderPermissions, - InexistentParentCheckpoint, - InexistentInitialProjectCheckpoint, + + #[error("the provided checkpoint is not a descendant of the project's initial checkpoint")] InvalidCheckpointAncestry, - NonUnregisterableUser, - UnregisterableOrg, - UserAccountAssociated, -} -impl From for &'static str { - fn from(error: RegistryError) -> &'static str { - match error { - RegistryError::InexistentCheckpointId => "The provided checkpoint does not exist", - RegistryError::InexistentOrg => "The provided org does not exist", - RegistryError::InexistentUser => "The provided user does not exist", - RegistryError::DuplicateOrgId => "An org with the same ID already exists.", - RegistryError::DuplicateProjectId => "A project with a similar ID already exists.", - RegistryError::DuplicateUserId => "A user with the same ID already exists.", - RegistryError::InexistentProjectId => "Project does not exist", - RegistryError::InsufficientFee => "The provided fee is insufficient.", - RegistryError::InsufficientSenderPermissions => "Sender is not a project member", - RegistryError::InexistentParentCheckpoint => "Parent checkpoint does not exist", - RegistryError::InexistentInitialProjectCheckpoint => { - "A registered project must have an initial checkpoint." - } - RegistryError::InvalidCheckpointAncestry => { - "The provided checkpoint is not a descendant of the project's initial checkpoint." - } - RegistryError::NonUnregisterableUser => { - "The provided user is not eligible for unregistration." - } - RegistryError::UnregisterableOrg => { - "The provided org is not elibile for unregistration." - } - RegistryError::UserAccountAssociated => { - "The account is already associated with a user." - } - } - } -} + #[error("the provided user is not eligible for unregistration")] + UnregisterableUser, -#[cfg(feature = "std")] -impl core::fmt::Display for RegistryError { - fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result { - let s: &str = self.clone().into(); - write!(f, "{}", s) - } + #[error("the provided org is not elibile for unregistration")] + UnregisterableOrg, + + #[error("the account is already associated with a user")] + UserAccountAssociated, } // The index with which the registry runtime module is declared diff --git a/runtime/src/registry.rs b/runtime/src/registry.rs index d3f690bd..dbc343c7 100644 --- a/runtime/src/registry.rs +++ b/runtime/src/registry.rs @@ -273,7 +273,7 @@ decl_module! { Ok(()) } else { - Err(RegistryError::NonUnregisterableUser.into()) + Err(RegistryError::UnregisterableUser.into()) } } }