@@ -27,6 +27,8 @@ pub struct NewObjectResponse {
2727#[ derive( Clone , Debug , serde:: Deserialize , serde:: Serialize ) ]
2828pub struct CommitResponse {
2929 pub status : String ,
30+ #[ serde( default ) ]
31+ pub message : Option < String > ,
3032}
3133
3234pub async fn query_objects < T : serde:: de:: DeserializeOwned > (
@@ -55,9 +57,22 @@ pub async fn new_object(servertype: impl Display) -> anyhow::Result<NewObjectRes
5557pub 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
6378pub async fn request_api (
@@ -149,6 +164,12 @@ fn calculate_app_id(token: &String) -> String {
149164}
150165
151166impl 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
269301impl < T : serde:: de:: DeserializeOwned > QueryResponse < T > {
0 commit comments