git.delta.rocks / unique-network / refs/commits / b92deacde238

difftreelog

feat return rpc error on not found collection

Yaroslav Bolyukin2021-12-06parent: #789924d.patch.diff
in: master

3 files changed

modifiedclient/rpc/src/lib.rsdiffbeforeafterboth
--- 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))?
 		}
 	};
 }
modifiedprimitives/rpc/src/lib.rsdiffbeforeafterboth
before · primitives/rpc/src/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]23use up_data_structs::{CollectionId, TokenId, Collection, CollectionStats};4use sp_std::vec::Vec;5use sp_core::H160;6use codec::Decode;78sp_api::decl_runtime_apis! {9	pub trait UniqueApi<CrossAccountId, AccountId> where10		AccountId: Decode,11		CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,12	{13		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>;14		fn token_exists(collection: CollectionId, token: TokenId) -> bool;1516		fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId;17		fn const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;18		fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;1920		fn collection_tokens(collection: CollectionId) -> u32;21		fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32;22		fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128;23		fn allowance(24			collection: CollectionId,25			sender: CrossAccountId,26			spender: CrossAccountId,27			token: TokenId,28		) -> u128;2930		/// Used for ethereum integration31		fn eth_contract_code(account: H160) -> Option<Vec<u8>>;3233		fn adminlist(collection: CollectionId) -> Vec<CrossAccountId>;34		fn allowlist(collection: CollectionId) -> Vec<CrossAccountId>;35		fn allowed(collection: CollectionId, user: CrossAccountId) -> bool;36		fn last_token_id(collection: CollectionId) -> TokenId;37		fn collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>>;38		fn collection_stats() -> CollectionStats;39	}40}
after · primitives/rpc/src/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]23use up_data_structs::{CollectionId, TokenId, Collection, CollectionStats};4use sp_std::vec::Vec;5use sp_core::H160;6use codec::Decode;7use sp_runtime::DispatchError;89type Result<T> = core::result::Result<T, DispatchError>;1011sp_api::decl_runtime_apis! {12	pub trait UniqueApi<CrossAccountId, AccountId> where13		AccountId: Decode,14		CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,15	{16		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>>;17		fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool>;1819		fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId>;20		fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;21		fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;2223		fn collection_tokens(collection: CollectionId) -> Result<u32>;24		fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;25		fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128>;26		fn allowance(27			collection: CollectionId,28			sender: CrossAccountId,29			spender: CrossAccountId,30			token: TokenId,31		) -> Result<u128>;3233		/// Used for ethereum integration34		fn eth_contract_code(account: H160) -> Option<Vec<u8>>;3536		fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;37		fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;38		fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool>;39		fn last_token_id(collection: CollectionId) -> Result<TokenId>;40		fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>>;41		fn collection_stats() -> Result<CollectionStats>;42	}43}
modifiedruntime/src/lib.rsdiffbeforeafterboth
--- 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(<pallet_common::CollectionHandle<Runtime>>::new($collection).unwrap());
+		let collection = Dispatched::dispatch(<pallet_common::CollectionHandle<Runtime>>::try_get($collection)?);
 		let dispatch = collection.as_dyn();
 
-		dispatch.$method($($name),*)
+		Ok(dispatch.$method($($name),*))
 	}};
 }
 impl_runtime_apis! {
 	impl up_rpc::UniqueApi<Block, CrossAccountId, AccountId>
 		for Runtime
 	{
-		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId> {
+		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>, DispatchError> {
 			dispatch_unique_runtime!(collection.account_tokens(account))
 		}
-		fn token_exists(collection: CollectionId, token: TokenId) -> bool {
+		fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool, DispatchError> {
 			dispatch_unique_runtime!(collection.token_exists(token))
 		}
 
-		fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId {
+		fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId, DispatchError> {
 			dispatch_unique_runtime!(collection.token_owner(token))
 		}
-		fn const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8> {
+		fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {
 			dispatch_unique_runtime!(collection.const_metadata(token))
 		}
-		fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8> {
+		fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {
 			dispatch_unique_runtime!(collection.variable_metadata(token))
 		}
 
-		fn collection_tokens(collection: CollectionId) -> u32 {
+		fn collection_tokens(collection: CollectionId) -> Result<u32, DispatchError> {
 			dispatch_unique_runtime!(collection.collection_tokens())
 		}
-		fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32 {
+		fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32, DispatchError> {
 			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<u128, DispatchError> {
 			dispatch_unique_runtime!(collection.balance(account, token))
 		}
 		fn allowance(
@@ -1060,7 +1061,7 @@
 			sender: CrossAccountId,
 			spender: CrossAccountId,
 			token: TokenId,
-		) -> u128 {
+		) -> Result<u128, DispatchError> {
 			dispatch_unique_runtime!(collection.allowance(sender, spender, token))
 		}
 
@@ -1069,23 +1070,23 @@
 				.or_else(|| <pallet_evm_migration::OnMethodCall<Runtime>>::get_code(&account))
 				.or_else(|| <pallet_evm_contract_helpers::HelpersOnMethodCall<Self>>::get_code(&account))
 		}
-		fn adminlist(collection: CollectionId) -> Vec<CrossAccountId> {
-			<pallet_common::Pallet<Runtime>>::adminlist(collection)
+		fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {
+			Ok(<pallet_common::Pallet<Runtime>>::adminlist(collection))
 		}
-		fn allowlist(collection: CollectionId) -> Vec<CrossAccountId> {
-			<pallet_common::Pallet<Runtime>>::allowlist(collection)
+		fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {
+			Ok(<pallet_common::Pallet<Runtime>>::allowlist(collection))
 		}
-		fn allowed(collection: CollectionId, user: CrossAccountId) -> bool {
-			<pallet_common::Pallet<Runtime>>::allowed(collection, user)
+		fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool, DispatchError> {
+			Ok(<pallet_common::Pallet<Runtime>>::allowed(collection, user))
 		}
-		fn last_token_id(collection: CollectionId) -> TokenId {
+		fn last_token_id(collection: CollectionId) -> Result<TokenId, DispatchError> {
 			dispatch_unique_runtime!(collection.last_token_id())
 		}
-		fn collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>> {
-			<pallet_common::CollectionById<Runtime>>::get(collection)
+		fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>, DispatchError> {
+			Ok(<pallet_common::CollectionById<Runtime>>::get(collection))
 		}
-		fn collection_stats() -> CollectionStats {
-			<pallet_common::Pallet<Runtime>>::collection_stats()
+		fn collection_stats() -> Result<CollectionStats, DispatchError> {
+			Ok(<pallet_common::Pallet<Runtime>>::collection_stats())
 		}
 	}