From b92deacde2383080bba396a59b1277e92fc47d79 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Mon, 06 Dec 2021 17:27:25 +0000 Subject: [PATCH] feat: return rpc error on not found collection --- --- a/client/rpc/src/lib.rs +++ b/client/rpc/src/lib.rs @@ -143,13 +143,16 @@ let api = self.client.runtime_api(); let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash)); - api.$method_name(&at, $($name),*).map_err(|e| RpcError { + let result = api.$method_name(&at, $($name),*).map_err(|e| RpcError { code: ErrorCode::ServerError(Error::RuntimeError.into()), message: "Unable to query".into(), data: Some(format!("{:?}", e).into()), - }) $( - .map($mapper) - )? + })?; + result.map_err(|e| RpcError { + code: ErrorCode::InvalidParams, + message: "Runtime returned error".into(), + data: Some(format!("{:?}", e).into()), + })$(.map($mapper))? } }; } --- a/primitives/rpc/src/lib.rs +++ b/primitives/rpc/src/lib.rs @@ -4,37 +4,40 @@ use sp_std::vec::Vec; use sp_core::H160; use codec::Decode; +use sp_runtime::DispatchError; + +type Result = core::result::Result; sp_api::decl_runtime_apis! { pub trait UniqueApi where AccountId: Decode, CrossAccountId: pallet_common::account::CrossAccountId, { - fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec; - fn token_exists(collection: CollectionId, token: TokenId) -> bool; + fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result>; + fn token_exists(collection: CollectionId, token: TokenId) -> Result; - fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId; - fn const_metadata(collection: CollectionId, token: TokenId) -> Vec; - fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec; + fn token_owner(collection: CollectionId, token: TokenId) -> Result; + fn const_metadata(collection: CollectionId, token: TokenId) -> Result>; + fn variable_metadata(collection: CollectionId, token: TokenId) -> Result>; - fn collection_tokens(collection: CollectionId) -> u32; - fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32; - fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128; + fn collection_tokens(collection: CollectionId) -> Result; + fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result; + fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result; fn allowance( collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId, - ) -> u128; + ) -> Result; /// Used for ethereum integration fn eth_contract_code(account: H160) -> Option>; - fn adminlist(collection: CollectionId) -> Vec; - fn allowlist(collection: CollectionId) -> Vec; - fn allowed(collection: CollectionId, user: CrossAccountId) -> bool; - fn last_token_id(collection: CollectionId) -> TokenId; - fn collection_by_id(collection: CollectionId) -> Option>; - fn collection_stats() -> CollectionStats; + fn adminlist(collection: CollectionId) -> Result>; + fn allowlist(collection: CollectionId) -> Result>; + fn allowed(collection: CollectionId, user: CrossAccountId) -> Result; + fn last_token_id(collection: CollectionId) -> Result; + fn collection_by_id(collection: CollectionId) -> Result>>; + fn collection_stats() -> Result; } } --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -16,6 +16,7 @@ use sp_api::impl_runtime_apis; use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H256, U256, H160}; +use sp_runtime::DispatchError; // #[cfg(any(feature = "std", test))] // pub use sp_runtime::BuildStorage; @@ -1019,40 +1020,40 @@ ($collection:ident.$method:ident($($name:ident),*)) => {{ use pallet_unique::dispatch::Dispatched; - let collection = Dispatched::dispatch(>::new($collection).unwrap()); + let collection = Dispatched::dispatch(>::try_get($collection)?); let dispatch = collection.as_dyn(); - dispatch.$method($($name),*) + Ok(dispatch.$method($($name),*)) }}; } impl_runtime_apis! { impl up_rpc::UniqueApi for Runtime { - fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec { + fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result, DispatchError> { dispatch_unique_runtime!(collection.account_tokens(account)) } - fn token_exists(collection: CollectionId, token: TokenId) -> bool { + fn token_exists(collection: CollectionId, token: TokenId) -> Result { dispatch_unique_runtime!(collection.token_exists(token)) } - fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId { + fn token_owner(collection: CollectionId, token: TokenId) -> Result { dispatch_unique_runtime!(collection.token_owner(token)) } - fn const_metadata(collection: CollectionId, token: TokenId) -> Vec { + fn const_metadata(collection: CollectionId, token: TokenId) -> Result, DispatchError> { dispatch_unique_runtime!(collection.const_metadata(token)) } - fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec { + fn variable_metadata(collection: CollectionId, token: TokenId) -> Result, DispatchError> { dispatch_unique_runtime!(collection.variable_metadata(token)) } - fn collection_tokens(collection: CollectionId) -> u32 { + fn collection_tokens(collection: CollectionId) -> Result { dispatch_unique_runtime!(collection.collection_tokens()) } - fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32 { + fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result { dispatch_unique_runtime!(collection.account_balance(account)) } - fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128 { + fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result { dispatch_unique_runtime!(collection.balance(account, token)) } fn allowance( @@ -1060,7 +1061,7 @@ sender: CrossAccountId, spender: CrossAccountId, token: TokenId, - ) -> u128 { + ) -> Result { dispatch_unique_runtime!(collection.allowance(sender, spender, token)) } @@ -1069,23 +1070,23 @@ .or_else(|| >::get_code(&account)) .or_else(|| >::get_code(&account)) } - fn adminlist(collection: CollectionId) -> Vec { - >::adminlist(collection) + fn adminlist(collection: CollectionId) -> Result, DispatchError> { + Ok(>::adminlist(collection)) } - fn allowlist(collection: CollectionId) -> Vec { - >::allowlist(collection) + fn allowlist(collection: CollectionId) -> Result, DispatchError> { + Ok(>::allowlist(collection)) } - fn allowed(collection: CollectionId, user: CrossAccountId) -> bool { - >::allowed(collection, user) + fn allowed(collection: CollectionId, user: CrossAccountId) -> Result { + Ok(>::allowed(collection, user)) } - fn last_token_id(collection: CollectionId) -> TokenId { + fn last_token_id(collection: CollectionId) -> Result { dispatch_unique_runtime!(collection.last_token_id()) } - fn collection_by_id(collection: CollectionId) -> Option> { - >::get(collection) + fn collection_by_id(collection: CollectionId) -> Result>, DispatchError> { + Ok(>::get(collection)) } - fn collection_stats() -> CollectionStats { - >::collection_stats() + fn collection_stats() -> Result { + Ok(>::collection_stats()) } } -- gitstuff