Skip to content

Commit

Permalink
Return None if server action returns nothing
Browse files Browse the repository at this point in the history
Some server actions return an empty body, which
is not valid JSON.
  • Loading branch information
milliams committed Oct 30, 2023
1 parent 8875327 commit e644b15
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/compute/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ pub async fn server_action_with_args<S1, Q>(
session: &Session,
id: S1,
action: Q,
) -> Result<serde_json::Value>
) -> Result<Option<serde_json::Value>>
where
S1: AsRef<str>,
Q: Serialize + Send + Debug,
Expand All @@ -331,10 +331,15 @@ where
.send()
.await?;
debug!("Successfully ran {:?} on server {}", action, id.as_ref());
response
.json::<serde_json::Value>()
.await
.map_err(|e| Error::new(ErrorKind::InvalidResponse, e.to_string()))
Ok(match response.content_length() {
Some(0) => None,
_ => Some(
response
.json::<serde_json::Value>()
.await
.map_err(|e| Error::new(ErrorKind::InvalidResponse, e.to_string()))?,
),
})
}

/// Whether key pair pagination is supported.
Expand Down
2 changes: 1 addition & 1 deletion src/compute/servers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl Server {
}

/// Run an action on the server.
pub async fn action(&mut self, action: ServerAction) -> Result<serde_json::Value> {
pub async fn action(&mut self, action: ServerAction) -> Result<Option<serde_json::Value>> {
api::server_action_with_args(&self.session, &self.inner.id, action).await
}
}
Expand Down

0 comments on commit e644b15

Please sign in to comment.