Skip to content

Commit

Permalink
[CLI] Fix faucet errors when server returns a 503 (MystenLabs#18553)
Browse files Browse the repository at this point in the history
## Description 
Strengthen the error handling of `sui client faucet` command.

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [x] CLI: `sui client faucet` received some improvements in the way it
handles and responds to errors from the faucet service.
- [ ] Rust SDK:
  • Loading branch information
stefan-mysten authored and tx-tomcat committed Jul 29, 2024
1 parent 0bfe8bc commit ba2add1
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions crates/sui/src/client_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::{
};

use anyhow::{anyhow, bail, ensure, Context};
use axum::http::StatusCode;
use bip32::DerivationPath;
use clap::*;
use colored::Colorize;
Expand Down Expand Up @@ -2524,15 +2525,26 @@ pub async fn request_tokens_from_faucet(
.json(&json_body)
.send()
.await?;
if resp.status() == 429 {
bail!("Faucet received too many requests from this IP address. Please try again after 60 minutes.");
}
let faucet_resp: FaucetResponse = resp.json().await?;

if let Some(err) = faucet_resp.error {
bail!("Faucet request was unsuccessful: {err}")
} else {
println!("Request successful. It can take up to 1 minute to get the coin. Run sui client gas to check your gas coins.");
match resp.status() {
StatusCode::ACCEPTED => {
let faucet_resp: FaucetResponse = resp.json().await?;

if let Some(err) = faucet_resp.error {
bail!("Faucet request was unsuccessful: {err}")
} else {
println!("Request successful. It can take up to 1 minute to get the coin. Run sui client gas to check your gas coins.");
}
}
StatusCode::TOO_MANY_REQUESTS => {
bail!("Faucet service received too many requests from this IP address. Please try again after 60 minutes.");
}
StatusCode::SERVICE_UNAVAILABLE => {
bail!("Faucet service is currently overloaded or unavailable. Please try again later.");
}
status_code => {
bail!("Faucet request was unsuccessful: {status_code}");
}
}
Ok(())
}
Expand Down

0 comments on commit ba2add1

Please sign in to comment.