Skip to content

Commit d0efc61

Browse files
authored
Add logging to client configuration discovery (#230)
Include info about where config is coming from (file, CLI, defaults) and improve error messages when connections fail. Check for 'successful' http requests that return 4xx statuses. Print a message if a response has neither data nor errors.
1 parent 2086bc5 commit d0efc61

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/client/config.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Display;
12
use std::io::ErrorKind;
23
use std::path::Path;
34

@@ -60,3 +61,20 @@ impl ClientConfiguration {
6061
self
6162
}
6263
}
64+
65+
impl Display for ClientConfiguration {
66+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67+
write!(f, "ClientConfiguration(host: ")?;
68+
match self.host {
69+
Some(ref h) => write!(f, "{h}")?,
70+
None => write!(f, "None")?,
71+
}
72+
write!(f, ", auth: ")?;
73+
match self.auth {
74+
Some(ref a) => write!(f, "{a}")?,
75+
None => write!(f, "None")?,
76+
}
77+
write!(f, ")")?;
78+
Ok(())
79+
}
80+
}

src/client/mod.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use graphql_client::{GraphQLQuery, Response};
55
use reqwest::Client;
66
use serde::de::DeserializeOwned;
77
use serde::Serialize;
8+
use tracing::info;
89
use url::Url;
910

1011
use crate::cli::client::{ClientCommand, ClientOptions, ConfigurationOptions};
@@ -25,13 +26,18 @@ pub async fn run_client(options: ClientOptions) {
2526
} = options;
2627

2728
let conf = match ClientConfiguration::from_default_file().await {
28-
Ok(conf) => conf.with_host(connection.host).with_auth(connection.auth),
29+
Ok(conf) => {
30+
info!("Configuration from file: {conf}");
31+
conf.with_host(connection.host).with_auth(connection.auth)
32+
}
2933
Err(e) => {
3034
println!("Could not read configuration: {e}");
3135
return;
3236
}
3337
};
3438

39+
info!("Configuration with CLI args included: {conf}");
40+
3541
let client = match NumtrackerClient::from_config(conf).await {
3642
Ok(client) => client,
3743
Err(e) => {
@@ -86,14 +92,16 @@ struct ConfigureMutation;
8692

8793
impl NumtrackerClient {
8894
async fn from_config(config: ClientConfiguration) -> Result<Self, ClientError> {
89-
let host = config
90-
.host
91-
.unwrap_or(Url::parse("http://localhost:8000").expect("Constant URL is valid"));
95+
let host = config.host.unwrap_or_else(|| {
96+
info!("No host specified, defaulting to localhost:8000");
97+
Url::parse("http://localhost:8000").expect("Constant URL is valid")
98+
});
9299

93100
let auth = match config.auth {
94101
Some(auth) => Some(cli_auth::get_access_token(&auth).await?),
95102
None => None,
96103
};
104+
info!("Querying {host} with auth: {auth:?}");
97105
Ok(NumtrackerClient { auth, host })
98106
}
99107

@@ -110,7 +118,13 @@ impl NumtrackerClient {
110118
None => client,
111119
Some(token) => client.bearer_auth(token),
112120
};
113-
client.json(&content).send().await?.json().await
121+
client
122+
.json(&content)
123+
.send()
124+
.await?
125+
.error_for_status()?
126+
.json()
127+
.await
114128
}
115129

116130
async fn query_configuration(self, instrument: Option<Vec<String>>) -> Result<(), ClientError> {
@@ -136,6 +150,8 @@ impl NumtrackerClient {
136150
conf.tracker_file_extension.unwrap_or(conf.instrument)
137151
);
138152
}
153+
} else if data.errors.is_none() {
154+
println!("Query returned no data or errors");
139155
}
140156
Ok(())
141157
}

0 commit comments

Comments
 (0)