Skip to content

Commit 6e064df

Browse files
committed
Improved error handling
1 parent 728e835 commit 6e064df

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

crates/cli/src/subcommands/dns.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use crate::common_args;
22
use crate::config::Config;
3-
use crate::util::{add_auth_header_opt, get_auth_header};
3+
use crate::util::{add_auth_header_opt, get_auth_header, ResponseExt};
44
use clap::ArgMatches;
55
use clap::{Arg, Command};
66

7-
use spacetimedb_client_api_messages::name::DomainName;
7+
use spacetimedb_client_api_messages::name::{DomainName, SetDomainsResult};
88

99
pub fn cli() -> Command {
1010
Command::new("rename")
@@ -44,10 +44,22 @@ pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::E
4444
let builder = add_auth_header_opt(builder, &auth_header);
4545

4646
let response = builder.send().await?;
47-
let status = response.status();
48-
let response_body = response.text().await?;
47+
let status = &response.status();
48+
let result : SetDomainsResult = response.json_or_error().await?;
49+
4950
if !status.is_success() {
50-
anyhow::bail!("Error: {response_body}");
51+
anyhow::bail!(match result {
52+
SetDomainsResult::Success => "".to_string(),
53+
SetDomainsResult::PermissionDenied { domain } =>
54+
format!("Permission denied for domain: {}", domain),
55+
SetDomainsResult::PermissionDeniedOnAny { domains } =>
56+
format!("Permission denied for domains: {:?}", domains),
57+
SetDomainsResult::DatabaseNotFound =>
58+
format!("Database {} not found", database_identity),
59+
SetDomainsResult::NotYourDatabase { .. } =>
60+
format!("You cannot rename {} because it is owned by another identity.", database_identity),
61+
SetDomainsResult::OtherError(err) => err,
62+
});
5163
}
5264

5365
println!("Name set to {} for identity {}.", domain, database_identity);

crates/client-api/src/routes/database.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,13 +704,12 @@ pub async fn set_names<S: ControlStateDelegate>(
704704
));
705705
}
706706

707-
for name in validated_names {
707+
for name in &validated_names {
708708
if ctx.lookup_identity(name.as_str()).unwrap().is_some() {
709709
return Ok((
710710
StatusCode::BAD_REQUEST,
711711
axum::Json(name::SetDomainsResult::OtherError(
712-
format!("Cannot rename to {} because it already is in use.", name.as_str()))),
713-
));
712+
format!("Cannot rename to {} because it already is in use.", name.as_str())))));
714713
}
715714
}
716715

0 commit comments

Comments
 (0)