Skip to content

Commit

Permalink
Improve error messages
Browse files Browse the repository at this point in the history
Signed-off-by: Didier Wenzek <[email protected]>
  • Loading branch information
didier-wenzek committed Dec 3, 2024
1 parent 8bb6b67 commit 853653b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 15 deletions.
7 changes: 7 additions & 0 deletions crates/core/c8y_api/src/json_c8y_deserializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,13 @@ pub struct C8yDeviceProfile {
pub configuration: Vec<C8yDownloadConfigFile>,
}

/// Error returned by C8Y REST API
#[derive(Debug, Deserialize, Eq, PartialEq)]
pub struct C8yAPIError {
pub error: String,
pub message: String,
}

pub trait C8yDeviceControlOperationHelper {
fn from_json_value(value: serde_json::Value) -> Result<Self, serde_json::Error>
where
Expand Down
31 changes: 18 additions & 13 deletions crates/core/tedge/src/cli/certificate/c8y/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::log::MaybeFancy;
use crate::warning;
use anyhow::Context;
use anyhow::Error;
use c8y_api::json_c8y_deserializer::C8yAPIError;
use camino::Utf8PathBuf;
use certificate::CloudRootCerts;
use hyper::StatusCode;
Expand Down Expand Up @@ -72,7 +73,8 @@ impl DownloadCertCmd {
.with_context(|| format!("Fail to create the device CSR {}", self.csr_path))?;

let http = self.root_certs.blocking_client();
let url = format!("https://{}/.well-known/est/simpleenroll", self.c8y_url);
let c8y_url = &self.c8y_url;
let url = format!("https://{c8y_url}/.well-known/est/simpleenroll");
let url = Url::parse(&url)?;

let started = std::time::Instant::now();
Expand All @@ -84,19 +86,11 @@ impl DownloadCertCmd {
store_device_cert(&self.cert_path, cert)?;
return Ok(());
}
error!(
"Fail to extract a certificate from the response returned by {}",
self.c8y_url
);
error!("Fail to extract a certificate from the response returned by {c8y_url}");
}
Ok(response) => {
error!(
"The device {} is not registered yet on {}: {}:{:?}",
common_name,
self.c8y_url,
response.status(),
response.text()
);
let error = Self::c8y_error_message(response);
error!("The device {common_name} is not registered yet on {c8y_url}: {error}");
}
Err(err) => {
error!(
Expand All @@ -108,7 +102,9 @@ impl DownloadCertCmd {
}

if started.elapsed() > self.max_timeout {
return Err(anyhow::anyhow!("Maximum timeout elapsed. No certificate has been downloaded"));
return Err(anyhow::anyhow!(
"Maximum timeout elapsed. No certificate has been downloaded"
));
}
warning!("Will retry in {} seconds", self.retry_every.as_secs());
std::thread::sleep(self.retry_every);
Expand Down Expand Up @@ -154,4 +150,13 @@ impl DownloadCertCmd {
.body(csr.to_string())
.send()
}

fn c8y_error_message(response: Response) -> String {
let status = response.status().to_string();
if let Ok(C8yAPIError { message, .. }) = response.json() {
format!("{status}: {}", message)
} else {
status
}
}
}
10 changes: 8 additions & 2 deletions crates/core/tedge/src/cli/certificate/cli.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::time::Duration;
use super::create::CreateCertCmd;
use super::create_csr::CreateCsrCmd;
use super::remove::RemoveCertCmd;
Expand All @@ -10,6 +9,7 @@ use crate::command::BuildContext;
use crate::command::Command;
use crate::ConfigError;
use camino::Utf8PathBuf;
use std::time::Duration;
use tedge_config::OptionalConfigError;
use tedge_config::ProfileName;

Expand Down Expand Up @@ -134,7 +134,13 @@ impl BuildCommand for TEdgeCertCli {
cmd.into_boxed()
}

TEdgeCertCli::Download(DownloadCertCli::C8y { id, token, profile, retry_every, max_timeout }) => {
TEdgeCertCli::Download(DownloadCertCli::C8y {
id,
token,
profile,
retry_every,
max_timeout,
}) => {
let c8y_config = config.c8y.try_get(profile.as_deref())?;
let cmd = c8y::DownloadCertCmd {
device_id: id,
Expand Down

0 comments on commit 853653b

Please sign in to comment.