Skip to content

Commit

Permalink
refactor: improve error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
evilrobot-01 committed Mar 5, 2024
1 parent 427f9ee commit 31e4f50
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 58 deletions.
13 changes: 5 additions & 8 deletions pop-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -29,19 +29,16 @@ pub type Result<T> = core::result::Result<T, PopApiError>;
#[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),
}

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)),
}
}
}
Expand Down
102 changes: 52 additions & 50 deletions pop-api/src/v0/nfts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::RuntimeCall;
use crate::*;
use crate::{PopApiError::UnknownStatusCode, *};
use ink::prelude::vec::Vec;
use sp_runtime::{BoundedVec, MultiAddress};

Expand Down Expand Up @@ -309,56 +309,58 @@ pub enum Error {
WitnessRequired,
}

impl From<u32> for Error {
fn from(value: u32) -> Self {
impl TryFrom<u32> for Error {
type Error = PopApiError;

fn try_from(status_code: u32) -> std::result::Result<Self, Self::Error> {
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)),
}
}
}
Expand Down

0 comments on commit 31e4f50

Please sign in to comment.