Skip to content
This repository has been archived by the owner on Feb 28, 2021. It is now read-only.

Commit

Permalink
Merge pull request #374 from radicle-dev/thiserror/registry-error
Browse files Browse the repository at this point in the history
Thiserror/registry error
  • Loading branch information
NunoAlexandre authored Apr 20, 2020
2 parents 5892c9d + f078916 commit 55c2f5b
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 47 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}")]
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
77 changes: 32 additions & 45 deletions core/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<RegistryError> 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
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ decl_module! {
Ok(())
}
else {
Err(RegistryError::NonUnregisterableUser.into())
Err(RegistryError::UnregisterableUser.into())
}
}
}
Expand Down

0 comments on commit 55c2f5b

Please sign in to comment.