From 147831375613a5e508487b2d85a99104ae1505af Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Fri, 10 Jan 2020 02:06:56 +0100 Subject: [PATCH] wasm: add error_for_status to wasm response (#779) Adds the error_for_status and error_for_status_ref functions to wasm::Response --- src/wasm/response.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/wasm/response.rs b/src/wasm/response.rs index 3653d11..be22cad 100644 --- a/src/wasm/response.rs +++ b/src/wasm/response.rs @@ -89,6 +89,28 @@ impl Response { buffer.copy_to(&mut bytes); 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 { + 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 {