wasm: add error_for_status to wasm response (#779)

Adds the error_for_status and error_for_status_ref functions
to wasm::Response
This commit is contained in:
Paolo Barbolini
2020-01-10 02:06:56 +01:00
committed by Sean McArthur
parent d662c850a9
commit 1478313756

View File

@@ -89,6 +89,28 @@ impl Response {
buffer.copy_to(&mut bytes); buffer.copy_to(&mut bytes);
Ok(bytes.into()) Ok(bytes.into())
} }
// util methods
/// Turn a response into an error if the server returned an error.
pub fn error_for_status(self) -> crate::Result<Self> {
let status = self.status();
if status.is_client_error() || status.is_server_error() {
Err(crate::error::status_code(*self.url, status))
} else {
Ok(self)
}
}
/// Turn a reference to a response into an error if the server returned an error.
pub fn error_for_status_ref(&self) -> crate::Result<&Self> {
let status = self.status();
if status.is_client_error() || status.is_server_error() {
Err(crate::error::status_code(*self.url.clone(), status))
} else {
Ok(self)
}
}
} }
impl fmt::Debug for Response { impl fmt::Debug for Response {