From 31e4f506d4a5e04c385a51bf5fa15a03f6fdde75 Mon Sep 17 00:00:00 2001 From: Frank Bell Date: Tue, 5 Mar 2024 09:45:32 +0000 Subject: [PATCH] refactor: improve error handling --- pop-api/src/lib.rs | 13 ++---- pop-api/src/v0/nfts.rs | 102 +++++++++++++++++++++-------------------- 2 files changed, 57 insertions(+), 58 deletions(-) diff --git a/pop-api/src/lib.rs b/pop-api/src/lib.rs index ca56b679e..149d11d3a 100644 --- a/pop-api/src/lib.rs +++ b/pop-api/src/lib.rs @@ -2,9 +2,9 @@ pub mod v0; -pub use pop_api_primitives as primitives; -use crate::PopApiError::Nfts; +use crate::PopApiError::{Nfts, UnknownStatusCode}; use ink::{env::Environment, ChainExtensionInstance}; +pub use pop_api_primitives as primitives; use scale; use sp_runtime::MultiSignature; pub use v0::nfts; @@ -29,7 +29,7 @@ pub type Result = core::result::Result; #[derive(Debug, Copy, Clone, PartialEq, Eq, scale::Encode, scale::Decode)] #[cfg_attr(feature = "std", derive(scale_info::TypeInfo))] pub enum PopApiError { - RuntimeError, + UnknownStatusCode(u32), Nfts(nfts::Error), } @@ -37,11 +37,8 @@ impl ink::env::chain_extension::FromStatusCode for PopApiError { fn from_status_code(status_code: u32) -> core::result::Result<(), Self> { match status_code { 0 => Ok(()), - 1 => Err(Self::RuntimeError), - 50_000..=50_999 => { - return Err(Nfts((status_code - 50_000).into())); - } - _ => panic!("encountered unknown status code"), + 50_000..=50_999 => Err(Nfts((status_code - 50_000).try_into()?)), + _ => Err(UnknownStatusCode(status_code)), } } } diff --git a/pop-api/src/v0/nfts.rs b/pop-api/src/v0/nfts.rs index 89f47a760..eef11f295 100644 --- a/pop-api/src/v0/nfts.rs +++ b/pop-api/src/v0/nfts.rs @@ -1,5 +1,5 @@ use super::RuntimeCall; -use crate::*; +use crate::{PopApiError::UnknownStatusCode, *}; use ink::prelude::vec::Vec; use sp_runtime::{BoundedVec, MultiAddress}; @@ -309,56 +309,58 @@ pub enum Error { WitnessRequired, } -impl From for Error { - fn from(value: u32) -> Self { +impl TryFrom for Error { + type Error = PopApiError; + + fn try_from(status_code: u32) -> std::result::Result { use Error::*; - match value { - 0 => NoPermission, - 1 => UnknownCollection, - 2 => AlreadyExists, - 3 => ApprovalExpired, - 4 => WrongOwner, - 5 => BadWitness, - 6 => CollectionIdInUse, - 7 => ItemsNonTransferable, - 8 => NotDelegate, - 9 => WrongDelegate, - 10 => Unapproved, - 11 => Unaccepted, - 12 => ItemLocked, - 13 => LockedItemAttributes, - 14 => LockedCollectionAttributes, - 15 => LockedItemMetadata, - 16 => LockedCollectionMetadata, - 17 => MaxSupplyReached, - 18 => MaxSupplyLocked, - 19 => MaxSupplyTooSmall, - 20 => UnknownItem, - 21 => UnknownSwap, - 22 => MetadataNotFound, - 23 => AttributeNotFound, - 24 => NotForSale, - 25 => BidTooLow, - 26 => ReachedApprovalLimit, - 27 => DeadlineExpired, - 28 => WrongDuration, - 29 => MethodDisabled, - 30 => WrongSetting, - 31 => InconsistentItemConfig, - 32 => NoConfig, - 33 => RolesNotCleared, - 34 => MintNotStarted, - 35 => MintEnded, - 36 => AlreadyClaimed, - 37 => IncorrectData, - 38 => WrongOrigin, - 39 => WrongSignature, - 40 => IncorrectMetadata, - 41 => MaxAttributesLimitReached, - 42 => WrongNamespace, - 43 => CollectionNotEmpty, - 44 => WitnessRequired, - _ => panic!("encountered unknown status code"), + match status_code { + 0 => Ok(NoPermission), + 1 => Ok(UnknownCollection), + 2 => Ok(AlreadyExists), + 3 => Ok(ApprovalExpired), + 4 => Ok(WrongOwner), + 5 => Ok(BadWitness), + 6 => Ok(CollectionIdInUse), + 7 => Ok(ItemsNonTransferable), + 8 => Ok(NotDelegate), + 9 => Ok(WrongDelegate), + 10 => Ok(Unapproved), + 11 => Ok(Unaccepted), + 12 => Ok(ItemLocked), + 13 => Ok(LockedItemAttributes), + 14 => Ok(LockedCollectionAttributes), + 15 => Ok(LockedItemMetadata), + 16 => Ok(LockedCollectionMetadata), + 17 => Ok(MaxSupplyReached), + 18 => Ok(MaxSupplyLocked), + 19 => Ok(MaxSupplyTooSmall), + 20 => Ok(UnknownItem), + 21 => Ok(UnknownSwap), + 22 => Ok(MetadataNotFound), + 23 => Ok(AttributeNotFound), + 24 => Ok(NotForSale), + 25 => Ok(BidTooLow), + 26 => Ok(ReachedApprovalLimit), + 27 => Ok(DeadlineExpired), + 28 => Ok(WrongDuration), + 29 => Ok(MethodDisabled), + 30 => Ok(WrongSetting), + 31 => Ok(InconsistentItemConfig), + 32 => Ok(NoConfig), + 33 => Ok(RolesNotCleared), + 34 => Ok(MintNotStarted), + 35 => Ok(MintEnded), + 36 => Ok(AlreadyClaimed), + 37 => Ok(IncorrectData), + 38 => Ok(WrongOrigin), + 39 => Ok(WrongSignature), + 40 => Ok(IncorrectMetadata), + 41 => Ok(MaxAttributesLimitReached), + 42 => Ok(WrongNamespace), + 43 => Ok(CollectionNotEmpty), + 44 => Ok(WitnessRequired), + _ => Err(UnknownStatusCode(status_code)), } } }