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
--- 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<T> = core::result::Result<T, DispatchError>;
 
 sp_api::decl_runtime_apis! {
 	pub trait UniqueApi<CrossAccountId, AccountId> where
 		AccountId: Decode,
 		CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,
 	{
-		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>;
-		fn token_exists(collection: CollectionId, token: TokenId) -> bool;
+		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>>;
+		fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool>;
 
-		fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId;
-		fn const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;
-		fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;
+		fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId>;
+		fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
+		fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>>;
 
-		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<u32>;
+		fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32>;
+		fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128>;
 		fn allowance(
 			collection: CollectionId,
 			sender: CrossAccountId,
 			spender: CrossAccountId,
 			token: TokenId,
-		) -> u128;
+		) -> Result<u128>;
 
 		/// Used for ethereum integration
 		fn eth_contract_code(account: H160) -> Option<Vec<u8>>;
 
-		fn adminlist(collection: CollectionId) -> Vec<CrossAccountId>;
-		fn allowlist(collection: CollectionId) -> Vec<CrossAccountId>;
-		fn allowed(collection: CollectionId, user: CrossAccountId) -> bool;
-		fn last_token_id(collection: CollectionId) -> TokenId;
-		fn collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>>;
-		fn collection_stats() -> CollectionStats;
+		fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;
+		fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>>;
+		fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool>;
+		fn last_token_id(collection: CollectionId) -> Result<TokenId>;
+		fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>>;
+		fn collection_stats() -> Result<CollectionStats>;
 	}
 }
modifiedruntime/src/lib.rsdiffbeforeafterboth
1616
17use sp_api::impl_runtime_apis;17use sp_api::impl_runtime_apis;
18use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H256, U256, H160};18use sp_core::{crypto::KeyTypeId, OpaqueMetadata, H256, U256, H160};
19use sp_runtime::DispatchError;
19// #[cfg(any(feature = "std", test))]20// #[cfg(any(feature = "std", test))]
20// pub use sp_runtime::BuildStorage;21// pub use sp_runtime::BuildStorage;
2122
1019 ($collection:ident.$method:ident($($name:ident),*)) => {{1020 ($collection:ident.$method:ident($($name:ident),*)) => {{
1020 use pallet_unique::dispatch::Dispatched;1021 use pallet_unique::dispatch::Dispatched;
10211022
1022 let collection = Dispatched::dispatch(<pallet_common::CollectionHandle<Runtime>>::new($collection).unwrap());1023 let collection = Dispatched::dispatch(<pallet_common::CollectionHandle<Runtime>>::try_get($collection)?);
1023 let dispatch = collection.as_dyn();1024 let dispatch = collection.as_dyn();
10241025
1025 dispatch.$method($($name),*)1026 Ok(dispatch.$method($($name),*))
1026 }};1027 }};
1027}1028}
1028impl_runtime_apis! {1029impl_runtime_apis! {
1029 impl up_rpc::UniqueApi<Block, CrossAccountId, AccountId>1030 impl up_rpc::UniqueApi<Block, CrossAccountId, AccountId>
1030 for Runtime1031 for Runtime
1031 {1032 {
1032 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId> {1033 fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Result<Vec<TokenId>, DispatchError> {
1033 dispatch_unique_runtime!(collection.account_tokens(account))1034 dispatch_unique_runtime!(collection.account_tokens(account))
1034 }1035 }
1035 fn token_exists(collection: CollectionId, token: TokenId) -> bool {1036 fn token_exists(collection: CollectionId, token: TokenId) -> Result<bool, DispatchError> {
1036 dispatch_unique_runtime!(collection.token_exists(token))1037 dispatch_unique_runtime!(collection.token_exists(token))
1037 }1038 }
10381039
1039 fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId {1040 fn token_owner(collection: CollectionId, token: TokenId) -> Result<CrossAccountId, DispatchError> {
1040 dispatch_unique_runtime!(collection.token_owner(token))1041 dispatch_unique_runtime!(collection.token_owner(token))
1041 }1042 }
1042 fn const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8> {1043 fn const_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {
1043 dispatch_unique_runtime!(collection.const_metadata(token))1044 dispatch_unique_runtime!(collection.const_metadata(token))
1044 }1045 }
1045 fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8> {1046 fn variable_metadata(collection: CollectionId, token: TokenId) -> Result<Vec<u8>, DispatchError> {
1046 dispatch_unique_runtime!(collection.variable_metadata(token))1047 dispatch_unique_runtime!(collection.variable_metadata(token))
1047 }1048 }
10481049
1049 fn collection_tokens(collection: CollectionId) -> u32 {1050 fn collection_tokens(collection: CollectionId) -> Result<u32, DispatchError> {
1050 dispatch_unique_runtime!(collection.collection_tokens())1051 dispatch_unique_runtime!(collection.collection_tokens())
1051 }1052 }
1052 fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32 {1053 fn account_balance(collection: CollectionId, account: CrossAccountId) -> Result<u32, DispatchError> {
1053 dispatch_unique_runtime!(collection.account_balance(account))1054 dispatch_unique_runtime!(collection.account_balance(account))
1054 }1055 }
1055 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128 {1056 fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> Result<u128, DispatchError> {
1056 dispatch_unique_runtime!(collection.balance(account, token))1057 dispatch_unique_runtime!(collection.balance(account, token))
1057 }1058 }
1058 fn allowance(1059 fn allowance(
1059 collection: CollectionId,1060 collection: CollectionId,
1060 sender: CrossAccountId,1061 sender: CrossAccountId,
1061 spender: CrossAccountId,1062 spender: CrossAccountId,
1062 token: TokenId,1063 token: TokenId,
1063 ) -> u128 {1064 ) -> Result<u128, DispatchError> {
1064 dispatch_unique_runtime!(collection.allowance(sender, spender, token))1065 dispatch_unique_runtime!(collection.allowance(sender, spender, token))
1065 }1066 }
10661067
1069 .or_else(|| <pallet_evm_migration::OnMethodCall<Runtime>>::get_code(&account))1070 .or_else(|| <pallet_evm_migration::OnMethodCall<Runtime>>::get_code(&account))
1070 .or_else(|| <pallet_evm_contract_helpers::HelpersOnMethodCall<Self>>::get_code(&account))1071 .or_else(|| <pallet_evm_contract_helpers::HelpersOnMethodCall<Self>>::get_code(&account))
1071 }1072 }
1072 fn adminlist(collection: CollectionId) -> Vec<CrossAccountId> {1073 fn adminlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {
1073 <pallet_common::Pallet<Runtime>>::adminlist(collection)1074 Ok(<pallet_common::Pallet<Runtime>>::adminlist(collection))
1074 }1075 }
1075 fn allowlist(collection: CollectionId) -> Vec<CrossAccountId> {1076 fn allowlist(collection: CollectionId) -> Result<Vec<CrossAccountId>, DispatchError> {
1076 <pallet_common::Pallet<Runtime>>::allowlist(collection)1077 Ok(<pallet_common::Pallet<Runtime>>::allowlist(collection))
1077 }1078 }
1078 fn allowed(collection: CollectionId, user: CrossAccountId) -> bool {1079 fn allowed(collection: CollectionId, user: CrossAccountId) -> Result<bool, DispatchError> {
1079 <pallet_common::Pallet<Runtime>>::allowed(collection, user)1080 Ok(<pallet_common::Pallet<Runtime>>::allowed(collection, user))
1080 }1081 }
1081 fn last_token_id(collection: CollectionId) -> TokenId {1082 fn last_token_id(collection: CollectionId) -> Result<TokenId, DispatchError> {
1082 dispatch_unique_runtime!(collection.last_token_id())1083 dispatch_unique_runtime!(collection.last_token_id())
1083 }1084 }
1084 fn collection_by_id(collection: CollectionId) -> Option<Collection<AccountId>> {1085 fn collection_by_id(collection: CollectionId) -> Result<Option<Collection<AccountId>>, DispatchError> {
1085 <pallet_common::CollectionById<Runtime>>::get(collection)1086 Ok(<pallet_common::CollectionById<Runtime>>::get(collection))
1086 }1087 }
1087 fn collection_stats() -> CollectionStats {1088 fn collection_stats() -> Result<CollectionStats, DispatchError> {
1088 <pallet_common::Pallet<Runtime>>::collection_stats()1089 Ok(<pallet_common::Pallet<Runtime>>::collection_stats())
1089 }1090 }
1090 }1091 }
10911092