Skip to content

Commit d6c24d5

Browse files
committed
Improve error reporting, Add some helper functions to implement api methods manually
1 parent d17b14a commit d6c24d5

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

rust/src/api.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ pub struct NewObjectResponse {
2727
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
2828
pub struct CommitResponse {
2929
pub status: String,
30+
#[serde(default)]
31+
pub message: Option<String>,
3032
}
3133

3234
pub async fn query_objects<T: serde::de::DeserializeOwned>(
@@ -55,9 +57,22 @@ pub async fn new_object(servertype: impl Display) -> anyhow::Result<NewObjectRes
5557
pub async fn commit_changes(commit: &Commit) -> anyhow::Result<CommitResponse> {
5658
let config = Config::build_from_environment()?;
5759
let response = request_api(COMMIT_ENDPOINT, serde_json::to_value(commit)?, config).await?;
58-
let response = response.error_for_status()?;
60+
let status = response.status();
61+
let body = response.json::<CommitResponse>().await?;
5962

60-
Ok(response.json().await?)
63+
if status.is_client_error() || status.is_server_error() {
64+
return Err(anyhow::anyhow!("Unable to process request").context(format!("{:?}", body)));
65+
}
66+
67+
if body.status == "error" {
68+
return Err(anyhow::anyhow!(
69+
"Error while committing {}",
70+
body.message
71+
.unwrap_or_else(|| String::from("Unknown commit error"))
72+
));
73+
}
74+
75+
Ok(body)
6176
}
6277

6378
pub async fn request_api(
@@ -149,6 +164,12 @@ fn calculate_app_id(token: &String) -> String {
149164
}
150165

151166
impl Server {
167+
pub fn clear(&mut self, attribute: impl ToString) -> anyhow::Result<&mut Self> {
168+
self.attributes.clear(attribute.to_string());
169+
170+
Ok(self)
171+
}
172+
152173
pub fn set(
153174
&mut self,
154175
attribute: impl ToString,
@@ -264,6 +285,17 @@ impl Server {
264285

265286
set
266287
}
288+
289+
/// Rolls back the changes.
290+
///
291+
/// Returns the reverted changes
292+
pub fn rollback(&mut self) -> Changeset {
293+
let old_changes = self.changeset();
294+
295+
self.changes = Changeset::default();
296+
297+
old_changes
298+
}
267299
}
268300

269301
impl<T: serde::de::DeserializeOwned> QueryResponse<T> {

rust/src/commit.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,12 @@ impl Dataset {
172172
Self(Default::default())
173173
}
174174

175+
pub fn clear(&mut self, name: impl ToString) -> &mut Self {
176+
self.0.remove(&name.to_string());
177+
178+
self
179+
}
180+
175181
pub fn set(
176182
&mut self,
177183
name: impl ToString,

rust/src/config.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::fmt::{Debug, Formatter};
22
use std::path::Path;
3-
use std::rc::Rc;
4-
use std::sync::Mutex;
3+
use std::sync::{Arc, Mutex};
54

65
use signature::Signer;
76

@@ -24,7 +23,7 @@ pub struct Config {
2423
pub enum SshSigner {
2524
Agent(
2625
Box<ssh_key::PublicKey>,
27-
Box<Rc<Mutex<ssh_agent_client_rs::Client>>>,
26+
Box<Arc<Mutex<ssh_agent_client_rs::Client>>>,
2827
),
2928
Key(Box<ssh_key::PrivateKey>),
3029
}
@@ -72,7 +71,7 @@ impl Config {
7271

7372
return Ok(Some(SshSigner::Agent(
7473
Box::new(key),
75-
Box::new(Rc::new(Mutex::new(client))),
74+
Box::new(Arc::new(Mutex::new(client))),
7675
)));
7776
}
7877
}
@@ -120,3 +119,6 @@ impl Signer<ssh_key::Signature> for SshSigner {
120119
}
121120
}
122121
}
122+
123+
unsafe impl Send for SshSigner {}
124+
unsafe impl Sync for SshSigner {}

rust/src/new_object.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::ops::{Deref, DerefMut};
22

33
use crate::api::{commit_changes, new_object, NewObjectResponse, Server};
4-
use crate::commit::{AttributeValue, Changeset, Commit};
4+
use crate::commit::{AttributeValue, Changeset, Commit, Dataset};
55
use crate::query::Query;
66

77
#[derive(Clone, Debug)]
@@ -12,6 +12,18 @@ pub struct NewObject {
1212
}
1313

1414
impl NewObject {
15+
pub fn from_dataset(dataset: Dataset) -> Self {
16+
Self {
17+
object_id: None,
18+
server: Server {
19+
object_id: 0,
20+
attributes: dataset,
21+
changes: Default::default(),
22+
},
23+
deferred_changes: Default::default(),
24+
}
25+
}
26+
1527
pub async fn request_new(servertype: impl ToString) -> anyhow::Result<Self> {
1628
let servertype = servertype.to_string();
1729
let NewObjectResponse { result } = new_object(&servertype).await?;
@@ -31,7 +43,7 @@ impl NewObject {
3143
servertype: impl ToString,
3244
hostname: impl ToString,
3345
) -> anyhow::Result<Self> {
34-
let mut new_object = Self::request_new(servertype).await?;
46+
let mut new_object = Self::request_new(servertype.to_string()).await?;
3547

3648
if let Ok(server) = Query::builder()
3749
.filter("hostname", hostname.to_string())
@@ -89,6 +101,16 @@ impl NewObject {
89101
Ok(server)
90102
}
91103

104+
///
105+
/// Gets the initial commit data and the follow-up commit of deferred changes
106+
///
107+
pub fn get_commit(self) -> (Commit, Commit) {
108+
(
109+
Commit::new().create(self.server.attributes),
110+
Commit::new().update(self.deferred_changes),
111+
)
112+
}
113+
92114
///
93115
/// The deferred method allows you to pre-update the newly created object
94116
///

0 commit comments

Comments
 (0)