Skip to content

Commit

Permalink
Merge pull request #4033 from sumukhballal/revert-apiclient-error-mes…
Browse files Browse the repository at this point in the history
…sages

apiclient: revert set/apply client error messages
  • Loading branch information
sumukhballal authored Jun 5, 2024
2 parents aec1af3 + 243ea78 commit efbbe1a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
19 changes: 16 additions & 3 deletions sources/api/apiclient/src/apply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,16 @@ where

// Send the settings changes to the server in the same transaction. (They're quick local
// requests, so don't add the complexity of making them run concurrently.)
for (_input_source, json) in changes {
for (input_source, json) in changes {
let uri = format!("/settings?tx={}", transaction);
let method = "PATCH";
let (_status, _body) = crate::raw_request(&socket_path, &uri, method, Some(json))
.await
.context(error::PatchSnafu)?;
.context(error::PatchSnafu {
input_source,
uri,
method,
})?;
}

// Commit the transaction and apply it to the system.
Expand Down Expand Up @@ -192,8 +196,17 @@ mod error {
#[snafu(display("Settings from '{}' are not a TOML table / JSON object", input_source))]
ModelType { input_source: String },

#[snafu(display("{}", source))]
#[snafu(display(
"Failed to {} settings from '{}' to '{}': {}",
method,
input_source,
uri,
source
))]
Patch {
input_source: String,
uri: String,
method: String,
#[snafu(source(from(crate::Error, Box::new)))]
source: Box<crate::Error>,
},
Expand Down
14 changes: 9 additions & 5 deletions sources/api/apiclient/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,6 @@ mod error {
#[snafu(display("Failed to send request: {}", source))]
RequestSend { source: hyper::Error },

#[snafu(display("{}", body))]
CliResponseStatus { body: String },

#[snafu(display("Status {} when {}ing {}: {}", code.as_str(), method, uri, body))]
ResponseStatus {
method: String,
Expand Down Expand Up @@ -89,7 +86,15 @@ where
let (status, body) = raw_request_unchecked(&socket_path, &uri, &method, data).await?;

// Error if the response status is in not in the 2xx range.
ensure!(status.is_success(), error::CliResponseStatusSnafu { body });
ensure!(
status.is_success(),
error::ResponseStatusSnafu {
method: method.as_ref(),
code: status,
uri: uri.as_ref(),
body,
}
);

Ok((status, body))
}
Expand Down Expand Up @@ -139,7 +144,6 @@ where
.await
.context(error::ResponseBodyReadSnafu)?;
let body = String::from_utf8(body_bytes.to_vec()).context(error::NonUtf8ResponseSnafu)?;

Ok((status, body))
}

Expand Down
8 changes: 5 additions & 3 deletions sources/api/apiclient/src/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ where
let method = "PATCH";
let (_status, _body) = crate::raw_request(&socket_path, &uri, method, Some(settings_data))
.await
.context(error::RequestSnafu)?;
.context(error::RequestSnafu { uri, method })?;

// Commit the transaction and apply it to the system.
let uri = format!("/tx/commit_and_apply?tx={}", transaction);
let method = "POST";
let (_status, _body) = crate::raw_request(&socket_path, &uri, method, None)
.await
.context(error::RequestSnafu)?;
.context(error::RequestSnafu { uri, method })?;

Ok(())
}
Expand All @@ -42,8 +42,10 @@ mod error {
#[snafu(display("Unable to serialize data: {}", source))]
Serialize { source: serde_json::Error },

#[snafu(display("{}", source))]
#[snafu(display("Failed {} request to '{}': {}", method, uri, source))]
Request {
method: String,
uri: String,
#[snafu(source(from(crate::Error, Box::new)))]
source: Box<crate::Error>,
},
Expand Down

0 comments on commit efbbe1a

Please sign in to comment.