Skip to content

Commit 3405dc6

Browse files
chore: improve error message for 'dfx deploy'. (#4018)
* Improve error message for 'dfx deploy'. * Fix lint. * Rename the variable. Co-authored-by: Eric Swanson <[email protected]> * Add test for failing to create canister without enough cycles. * Fixed shellcheck. * Show the correct command according to the network. --------- Co-authored-by: Eric Swanson <[email protected]>
1 parent 3200e13 commit 3405dc6

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ This affects the following commands:
3737
- `dfx canister update-settings`
3838
- `dfx ledger fabricate-cycles`
3939

40+
### chore: improve `dfx deploy` messages.
41+
42+
If users run `dfx deploy` without enough cycles, show additional messages to indicate what to do next.
43+
```
44+
Error explanation:
45+
Insufficient cycles balance to create the canister.
46+
How to resolve the error:
47+
Please top up your cycles balance by converting ICP to cycles like below:
48+
'dfx cycles convert --amount=0.123'.
49+
```
50+
4051
# 0.24.3
4152

4253
### feat: Bitcoin support in PocketIC

e2e/tests-dfx/cycles-ledger.bash

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,22 @@ current_time_nanoseconds() {
591591

592592
assert_command dfx deploy
593593

594+
# Test canister creation failure before topping up cycles.
595+
cd ..
596+
dfx_new canister_creation_failed
597+
598+
# shellcheck disable=SC2030,SC2031
599+
export DFX_DISABLE_AUTO_WALLET=1
600+
assert_command_fail dfx canister create canister_creation_failed_backend --with-cycles 1T --identity alice
601+
assert_contains "Insufficient cycles balance to create the canister."
602+
603+
## Back to top up cycles.
604+
cd ../temporary
605+
594606
assert_command dfx canister call depositor deposit "(record {to = record{owner = principal \"$ALICE\";};cycles = 13_400_000_000_000;})" --identity cycle-giver
595607
assert_command dfx canister call depositor deposit "(record {to = record{owner = principal \"$ALICE\"; subaccount = opt blob \"$ALICE_SUBACCT1_CANDID\"};cycles = 2_600_000_000_000;})" --identity cycle-giver
596608

609+
# shellcheck disable=SC2103
597610
cd ..
598611
dfx_new
599612
# setup done

src/dfx/src/lib/diagnosis.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
use crate::lib::cycles_ledger_types::create_canister::CreateCanisterError;
12
use crate::lib::error_code;
23
use anyhow::Error as AnyhowError;
34
use dfx_core::error::root_key::FetchRootKeyError;
5+
use dfx_core::network::provider::get_network_context;
46
use ic_agent::agent::{RejectCode, RejectResponse};
57
use ic_agent::AgentError;
68
use ic_asset::error::{GatherAssetDescriptorsError, SyncError, UploadContentError};
@@ -72,6 +74,12 @@ pub fn diagnose(err: &AnyhowError) -> Diagnosis {
7274
}
7375
}
7476

77+
if let Some(create_canister_err) = err.downcast_ref::<CreateCanisterError>() {
78+
if insufficient_cycles(create_canister_err) {
79+
return diagnose_insufficient_cycles();
80+
}
81+
}
82+
7583
NULL_DIAGNOSIS
7684
}
7785

@@ -262,3 +270,28 @@ fn diagnose_ledger_not_found() -> Diagnosis {
262270

263271
(Some(explanation.to_string()), Some(suggestion.to_string()))
264272
}
273+
274+
fn insufficient_cycles(err: &CreateCanisterError) -> bool {
275+
matches!(err, CreateCanisterError::InsufficientFunds { balance: _ })
276+
}
277+
278+
fn diagnose_insufficient_cycles() -> Diagnosis {
279+
let network = match get_network_context() {
280+
Ok(value) => {
281+
if value == "local" {
282+
"".to_string()
283+
} else {
284+
format!(" --network {}", value)
285+
}
286+
}
287+
Err(_) => "".to_string(),
288+
};
289+
290+
let explanation = "Insufficient cycles balance to create the canister.";
291+
let suggestion = format!(
292+
"Please top up your cycles balance by converting ICP to cycles like below:
293+
'dfx cycles convert --amount=0.123{}'",
294+
network
295+
);
296+
(Some(explanation.to_string()), Some(suggestion))
297+
}

0 commit comments

Comments
 (0)