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 Feb 3, 2025
1 parent d8d0f3b commit dfb22a1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 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 @@ -551,6 +551,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
27 changes: 15 additions & 12 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 Down Expand Up @@ -156,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
}
}
}

0 comments on commit dfb22a1

Please sign in to comment.