Skip to content
Snippets Groups Projects
Commit 260a225a authored by Hugo Trentesaux's avatar Hugo Trentesaux
Browse files

more precise error type

parent 51f7f699
No related branches found
No related tags found
No related merge requests found
...@@ -56,7 +56,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -56,7 +56,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
} }
/// get balance /// get balance
pub async fn get_balance(data: Data) -> Result<(), anyhow::Error> { pub async fn get_balance(data: Data) -> Result<(), subxt::Error> {
let account_id = data.address(); let account_id = data.address();
let account_info = get_account_info(data.client(), &account_id).await?; let account_info = get_account_info(data.client(), &account_id).await?;
if let Some(account_info) = account_info { if let Some(account_info) = account_info {
......
...@@ -22,7 +22,7 @@ pub enum Subcommand { ...@@ -22,7 +22,7 @@ pub enum Subcommand {
} }
/// handle technical committee commands /// handle technical committee commands
pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<(), GcliError> { pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliError> {
let data = data.build_client().await?.build_indexer().await?; let data = data.build_client().await?.build_indexer().await?;
match command { match command {
Subcommand::Members => technical_committee_members(&data).await?, Subcommand::Members => technical_committee_members(&data).await?,
...@@ -46,7 +46,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<( ...@@ -46,7 +46,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> anyhow::Result<(
} }
/// list technical committee members /// list technical committee members
pub async fn technical_committee_members(data: &Data) -> Result<(), anyhow::Error> { pub async fn technical_committee_members(data: &Data) -> Result<(), subxt::Error> {
let client = data.client(); let client = data.client();
let indexer = &data.indexer; let indexer = &data.indexer;
...@@ -85,7 +85,6 @@ pub async fn technical_committee_members(data: &Data) -> Result<(), anyhow::Erro ...@@ -85,7 +85,6 @@ pub async fn technical_committee_members(data: &Data) -> Result<(), anyhow::Erro
.unwrap_or_else(|| account_id.to_string(),) .unwrap_or_else(|| account_id.to_string(),)
); );
} }
Ok(()) Ok(())
} }
...@@ -93,7 +92,7 @@ pub async fn technical_committee_members(data: &Data) -> Result<(), anyhow::Erro ...@@ -93,7 +92,7 @@ pub async fn technical_committee_members(data: &Data) -> Result<(), anyhow::Erro
// TODO: // TODO:
// * better formatting (format pubkeys to SS58 and add usernames) // * better formatting (format pubkeys to SS58 and add usernames)
// * display proposals indices // * display proposals indices
pub async fn technical_committee_proposals(client: &Client) -> anyhow::Result<()> { pub async fn technical_committee_proposals(client: &Client) -> Result<(), subxt::Error> {
let parent_hash = client let parent_hash = client
.storage() .storage()
.at_latest() .at_latest()
...@@ -112,7 +111,6 @@ pub async fn technical_committee_proposals(client: &Client) -> anyhow::Result<() ...@@ -112,7 +111,6 @@ pub async fn technical_committee_proposals(client: &Client) -> anyhow::Result<()
println!("{:#?}", item.value); println!("{:#?}", item.value);
println!(); println!();
} }
Ok(()) Ok(())
} }
...@@ -122,7 +120,7 @@ pub async fn technical_committee_vote( ...@@ -122,7 +120,7 @@ pub async fn technical_committee_vote(
proposal_hash: Hash, proposal_hash: Hash,
proposal_index: u32, proposal_index: u32,
vote: bool, vote: bool,
) -> anyhow::Result<(), subxt::Error> { ) -> Result<(), subxt::Error> {
submit_call_and_look_event::< submit_call_and_look_event::<
runtime::technical_committee::events::Voted, runtime::technical_committee::events::Voted,
StaticPayload<runtime::technical_committee::calls::types::Vote>, StaticPayload<runtime::technical_committee::calls::types::Vote>,
...@@ -140,7 +138,7 @@ pub async fn technical_committee_vote( ...@@ -140,7 +138,7 @@ pub async fn technical_committee_vote(
pub async fn technical_committee_propose( pub async fn technical_committee_propose(
data: &Data, data: &Data,
proposal: &str, proposal: &str,
) -> anyhow::Result<(), subxt::Error> { ) -> Result<(), subxt::Error> {
let raw_call = hex::decode(proposal).expect("invalid hex"); let raw_call = hex::decode(proposal).expect("invalid hex");
let call = codec::decode_from_bytes(raw_call.into()).expect("invalid call"); let call = codec::decode_from_bytes(raw_call.into()).expect("invalid call");
let payload = runtime::tx().technical_committee().propose(5, call, 100); let payload = runtime::tx().technical_committee().propose(5, call, 100);
......
...@@ -2,7 +2,7 @@ use crate::{indexer::*, *}; ...@@ -2,7 +2,7 @@ use crate::{indexer::*, *};
use futures::join; use futures::join;
use std::collections::BTreeMap; use std::collections::BTreeMap;
pub async fn monitor_expirations(data: &Data, blocks: u32, _sessions: u32) -> anyhow::Result<()> { pub async fn monitor_expirations(data: &Data, blocks: u32, _sessions: u32) -> Result<(), subxt::Error> {
let client = data.client(); let client = data.client();
let indexer = data.indexer.clone(); let indexer = data.indexer.clone();
...@@ -119,7 +119,7 @@ impl IdentityCache { ...@@ -119,7 +119,7 @@ impl IdentityCache {
} }
} }
pub async fn fetch_identity(&mut self, identity_id: IdtyId) -> anyhow::Result<String> { pub async fn fetch_identity(&mut self, identity_id: IdtyId) -> Result<String, GcliError> {
Ok(match self.identities.entry(identity_id) { Ok(match self.identities.entry(identity_id) {
hash_map::Entry::Occupied(entry) => entry.get().clone(), hash_map::Entry::Occupied(entry) => entry.get().clone(),
hash_map::Entry::Vacant(entry) => entry hash_map::Entry::Vacant(entry) => entry
......
...@@ -257,11 +257,11 @@ pub async fn get_identity( ...@@ -257,11 +257,11 @@ pub async fn get_identity(
// account_id → idty_id // account_id → idty_id
(None, Some(account_id), None) => get_idty_index_by_account_id(client, account_id) (None, Some(account_id), None) => get_idty_index_by_account_id(client, account_id)
.await? .await?
.ok_or_else(|| anyhow!("no identity for account '{account_id}'"))?, .ok_or_else(|| GcliError::Duniter(format!("no identity for account '{account_id}'")))?,
// pseudo → idty_id // pseudo → idty_id
(None, None, Some(pseudo)) => get_idty_index_by_name(client, pseudo) (None, None, Some(pseudo)) => get_idty_index_by_name(client, pseudo)
.await? .await?
.ok_or_else(|| anyhow!("no identity for name '{pseudo}'"))?, .ok_or_else(|| GcliError::Indexer(format!("no identity for name '{pseudo}'")))?,
_ => { _ => {
return Err(GcliError::Logic( return Err(GcliError::Logic(
"One and only one argument is needed to fetch the identity.".to_string(), "One and only one argument is needed to fetch the identity.".to_string(),
...@@ -271,14 +271,14 @@ pub async fn get_identity( ...@@ -271,14 +271,14 @@ pub async fn get_identity(
// idty_id → value // idty_id → value
let value = get_identity_by_index(client, index) let value = get_identity_by_index(client, index)
.await? .await?
.ok_or_else(|| anyhow!("no identity value for index {index}"))?; .ok_or_else(|| GcliError::Duniter(format!("no identity value for index {index}")))?;
// pseudo // pseudo
let pseudo = pseudo.unwrap_or(if let Some(indexer) = &indexer { let pseudo = pseudo.unwrap_or(if let Some(indexer) = &indexer {
indexer indexer
.username_by_index(index) .username_by_index(index)
.await .await
.ok_or_else(|| anyhow!("indexer does not have username for this index {index}"))? .ok_or_else(|| GcliError::Indexer(format!("indexer does not have username for this index {index}")))?
} else { } else {
"<no indexer>".to_string() "<no indexer>".to_string()
}); });
......
...@@ -65,7 +65,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -65,7 +65,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
} }
/// get balance of oneshot account /// get balance of oneshot account
pub async fn oneshot_account_balance(data: &Data) -> Result<(), anyhow::Error> { pub async fn oneshot_account_balance(data: &Data) -> Result<(), subxt::Error> {
println!( println!(
"balance of oneshot account {} is: {}", "balance of oneshot account {} is: {}",
data.address(), data.address(),
......
...@@ -110,20 +110,20 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE ...@@ -110,20 +110,20 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE
/// rotate session keys /// rotate session keys
/// (needs to be connected to unsafe RPC) /// (needs to be connected to unsafe RPC)
pub async fn rotate_keys(data: &Data) -> Result<SessionKeys, anyhow::Error> { pub async fn rotate_keys(data: &Data) -> Result<SessionKeys, GcliError> {
data.legacy_rpc_methods() data.legacy_rpc_methods()
.await .await
.author_rotate_keys() .author_rotate_keys()
.await .await
.map_err(|e| { .map_err(|e| {
anyhow!( GcliError::Duniter(format!(
"Please make sure you are connected to your validator node with the unsafe RPC \ "Please make sure you are connected to your validator node with the unsafe RPC \
API enabled {e}" API enabled {e}"
) ))
})? })?
.deref() .deref()
.try_into() .try_into()
.map_err(|e| anyhow!("Session keys have wrong length: {:?}", e)) .map_err(|e| GcliError::Duniter(format!("Session keys have wrong length: {:?}", e)))
} }
/// set session keys /// set session keys
...@@ -190,7 +190,7 @@ pub async fn go_offline(data: &Data) -> Result<(), subxt::Error> { ...@@ -190,7 +190,7 @@ pub async fn go_offline(data: &Data) -> Result<(), subxt::Error> {
} }
/// get online authorities /// get online authorities
pub async fn online(data: &Data) -> Result<(), anyhow::Error> { pub async fn online(data: &Data) -> Result<(), subxt::Error> {
let client = data.client(); let client = data.client();
let online_authorities = client let online_authorities = client
......
...@@ -232,11 +232,15 @@ impl Data { ...@@ -232,11 +232,15 @@ impl Data {
Ok(self) Ok(self)
} }
/// get properties /// get properties
pub async fn fetch_system_properties(mut self) -> Result<Self, anyhow::Error> { pub async fn fetch_system_properties(mut self) -> Result<Self, GcliError> {
let system_properties = self.legacy_rpc_methods().await.system_properties().await?; let system_properties = self.legacy_rpc_methods().await.system_properties().await?;
let system_properties = serde_json::from_value::<SystemProperties>( let system_properties = serde_json::from_value::<SystemProperties>(
serde_json::Value::Object(system_properties), serde_json::Value::Object(system_properties),
)?; )
.map_err(|e| {
dbg!(e);
GcliError::Duniter("could not read duniter system properties".to_string())
})?;
self.token_decimals = system_properties.token_decimals; self.token_decimals = system_properties.token_decimals;
self.token_symbol = system_properties.token_symbol; self.token_symbol = system_properties.token_symbol;
Ok(self) Ok(self)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment