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
4use sp_std::vec::Vec;4use sp_std::vec::Vec;
5use sp_core::H160;5use sp_core::H160;
6use codec::Decode;6use codec::Decode;
7use sp_runtime::DispatchError;
8
9type Result<T> = core::result::Result<T, DispatchError>;
710
8sp_api::decl_runtime_apis! {11sp_api::decl_runtime_apis! {
9 pub trait UniqueApi<CrossAccountId, AccountId> where12 pub trait UniqueApi<CrossAccountId, AccountId> where
10 AccountId: Decode,13 AccountId: Decode,
11 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,14 CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,
12 {15 {
13 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>;16 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>>;
14 fn token_exists(collection: CollectionId, token: TokenId) -> bool;17 fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool>;
1518
16 fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId;19 fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId>;
17 fn const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;20 fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
18 fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;21 fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
1922
20 fn collection_tokens(collection: CollectionId) -> u32;23 fn collection_tokens(collection: CollectionId) -> Result<u32>;
21 fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32;24 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;
22 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128;25 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128>;
23 fn allowance(26 fn allowance(
24 collection: CollectionId,27 collection: CollectionId,
25 sender: CrossAccountId,28 sender: CrossAccountId,
26 spender: CrossAccountId,29 spender: CrossAccountId,
27 token: TokenId,30 token: TokenId,
28 ) -> u128;31 ) -> Result<u128>;
2932
30 /// Used for ethereum integration33 /// Used for ethereum integration
31 fn eth_contract_code(account: H160) -> Option<Vec<u8>>;34 fn eth_contract_code(account: H160) -> Option<Vec<u8>>;
3235
33 fn adminlist(collection: CollectionId) -> Vec<CrossAccountId>;36 fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;
34 fn allowlist(collection: CollectionId) -> Vec<CrossAccountId>;37 fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;
35 fn allowed(collection: CollectionId, user: CrossAccountId) -> bool;38 fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool>;
36 fn last_token_id(collection: CollectionId) -> TokenId;39 fn last_token_id(collection: CollectionId) -> Result<TokenId>;
37 fn collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>>;40 fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>>;
38 fn collection_stats() -> CollectionStats;41 fn collection_stats() -> Result<CollectionStats>;
39 }42 }
40}43}
4144
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())
 		}
 	}