diff --git a/kube-client/src/api/core_methods.rs b/kube-client/src/api/core_methods.rs index d1e209c4e..f35a0614b 100644 --- a/kube-client/src/api/core_methods.rs +++ b/kube-client/src/api/core_methods.rs @@ -24,6 +24,11 @@ where /// Ok(()) /// } /// ``` + /// + /// # Errors + /// + /// This function assumes that the object is expected to always exist, and returns [`Error`] if it does not. + /// Consider using [`Api::get_opt`] if you need to handle missing objects. pub async fn get(&self, name: &str) -> Result { let mut req = self.request.get(name).map_err(Error::BuildRequest)?; req.extensions_mut().insert("get"); @@ -39,7 +44,7 @@ where /// async fn main() -> Result<(), Box> { /// let client = Client::try_default().await?; /// let pods: Api = Api::namespaced(client, "apps"); - /// if let Some(pod) = pods.try_get("blog").await? { + /// if let Some(pod) = pods.get_opt("blog").await? { /// // Pod was found /// } else { /// // Pod was not found @@ -47,7 +52,7 @@ where /// Ok(()) /// } /// ``` - pub async fn try_get(&self, name: &str) -> Result> { + pub async fn get_opt(&self, name: &str) -> Result> { match self.get(name).await { Ok(obj) => Ok(Some(obj)), Err(Error::Api(ErrorResponse { reason, .. })) if &reason == "NotFound" => Ok(None), diff --git a/kube/src/lib.rs b/kube/src/lib.rs index 6c1f7024c..06e9b3951 100644 --- a/kube/src/lib.rs +++ b/kube/src/lib.rs @@ -503,11 +503,11 @@ mod test { #[tokio::test] #[ignore] // needs cluster (lists cms) - async fn api_try_get_handles_404() -> Result<(), Box> { + async fn api_get_opt_handles_404() -> Result<(), Box> { let client = Client::try_default().await?; let api = Api::::default_namespaced(client); assert_eq!( - api.try_get("this-cm-does-not-exist-ajklisdhfqkljwhreq").await?, + api.get_opt("this-cm-does-not-exist-ajklisdhfqkljwhreq").await?, None ); Ok(())