From 0c9b12feb620103990f5033b8163447e3122748d Mon Sep 17 00:00:00 2001 From: Vincent Zhang Date: Wed, 4 Dec 2024 07:51:49 +0800 Subject: [PATCH] Addressed review comments. --- CHANGELOG.md | 2 +- src/dfx/src/lib/diagnosis.rs | 2 ++ src/dfx/src/lib/operations/ledger.rs | 22 +++++++++------------- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d8dec8e193..16d3505118 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,7 +60,7 @@ Please top up your ICP balance. Your account address for receiving ICP from centralized exchanges: 8494c01329531c06254ff45dad87db806ae6ed935ad6a504cdbc00a935db7b49 (run `dfx ledger account-id` to display) -Your principal for ICP wallets and decentralized exchanges:ueuar-wxbnk-bdcsr-dnrh3-rsyq6-ffned-h64ox-vxywi-gzawf-ot4pv-sqe +Your principal for ICP wallets and decentralized exchanges: ueuar-wxbnk-bdcsr-dnrh3-rsyq6-ffned-h64ox-vxywi-gzawf-ot4pv-sqe (run `dfx identity get-principal` to display) ``` diff --git a/src/dfx/src/lib/diagnosis.rs b/src/dfx/src/lib/diagnosis.rs index d90a133f11..49bd3b2b77 100644 --- a/src/dfx/src/lib/diagnosis.rs +++ b/src/dfx/src/lib/diagnosis.rs @@ -17,6 +17,8 @@ pub type Diagnosis = (Option, Option); pub const NULL_DIAGNOSIS: Diagnosis = (None, None); #[derive(ThisError, Debug)] +/// If you do not need the generic error diagnosis to run, you can add a DiagnosedError with .context(err: DiagnosedError). +/// In that case, no extra diagnosis is attempted and the last-added explanation and suggestion are printed out. pub struct DiagnosedError { /// A user-friendly explanation of what went wrong. pub error_explanation: Option, diff --git a/src/dfx/src/lib/operations/ledger.rs b/src/dfx/src/lib/operations/ledger.rs index 426906da1d..37f882ea79 100644 --- a/src/dfx/src/lib/operations/ledger.rs +++ b/src/dfx/src/lib/operations/ledger.rs @@ -9,7 +9,7 @@ use crate::lib::{ }, nns_types::{account_identifier::AccountIdentifier, icpts::ICPTs}, }; -use anyhow::{bail, ensure, Context}; +use anyhow::{anyhow, bail, ensure, Context}; use backoff::backoff::Backoff; use backoff::ExponentialBackoff; use candid::{Decode, Encode, Principal}; @@ -144,7 +144,10 @@ pub async fn transfer( break duplicate_of; } Err(TransferError::InsufficientFunds { balance }) => { - return compose_insufficient_funds_error(agent, from_subaccount, balance); + return Err(anyhow!(TransferError::InsufficientFunds { balance })) + .with_context(|| { + diagnose_insufficient_funds_error(agent, from_subaccount) + }); } Err(transfer_err) => bail!(transfer_err), } @@ -168,11 +171,10 @@ pub async fn transfer( Ok(block_height) } -fn compose_insufficient_funds_error( +fn diagnose_insufficient_funds_error( agent: &Agent, subaccount: Option, - balance: ICPTs, -) -> DfxResult { +) -> DiagnosedError { let principal = agent.get_principal().unwrap(); // This should always succeed at this point. let explanation = "Insufficient ICP balance to finish the transfer transaction."; @@ -182,20 +184,14 @@ fn compose_insufficient_funds_error( Your account address for receiving ICP from centralized exchanges: {} (run `dfx ledger account-id` to display) -Your principal for ICP wallets and decentralized exchanges:{} +Your principal for ICP wallets and decentralized exchanges: {} (run `dfx identity get-principal` to display) ", AccountIdentifier::new(principal, subaccount), principal.to_text() ); - Err(DiagnosedError::new( - explanation.to_string(), - suggestion - )).context(format!( - "the debit account doesn't have enough funds to complete the transaction, current balance: {}", - balance - )) + DiagnosedError::new(explanation.to_string(), suggestion) } fn retryable(agent_error: &AgentError) -> bool {