diff options
| author | Zeyla Hellyer <[email protected]> | 2017-02-11 21:10:20 -0800 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-02-11 21:10:20 -0800 |
| commit | 7e254c5c6098bb1a47bac26c9895098a46cdc53f (patch) | |
| tree | d8adb2ff9d709ae32f12286f1432996011367cbf /src/client/rest | |
| parent | Update voice example to latest changes (diff) | |
| download | serenity-7e254c5c6098bb1a47bac26c9895098a46cdc53f.tar.xz serenity-7e254c5c6098bb1a47bac26c9895098a46cdc53f.zip | |
Handle unsuccessful responses before decoding
Instead of assuming every request is successful, check that the class of
the status is successful (2xx), and if not, return a
`ClientError::InvalidRequest`, which is a renamed
`ClientError::UnexpectedStatusCode`.
Diffstat (limited to 'src/client/rest')
| -rw-r--r-- | src/client/rest/mod.rs | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/src/client/rest/mod.rs b/src/client/rest/mod.rs index b905921..253f0f9 100644 --- a/src/client/rest/mod.rs +++ b/src/client/rest/mod.rs @@ -33,7 +33,7 @@ use hyper::client::{ Request, }; use hyper::method::Method; -use hyper::status::StatusCode; +use hyper::status::{StatusClass, StatusCode}; use hyper::{Error as HyperError, Result as HyperResult, Url, header}; use multipart::client::Multipart; use self::ratelimiting::Route; @@ -1584,9 +1584,15 @@ pub fn unpin_message(channel_id: u64, message_id: u64) -> Result<()> { fn request<'a, F>(route: Route, f: F) -> Result<HyperResponse> where F: Fn() -> RequestBuilder<'a> { - ratelimiting::perform(route, || f() + let response = ratelimiting::perform(route, || f() .header(header::Authorization(TOKEN.lock().unwrap().clone())) - .header(header::ContentType::json())) + .header(header::ContentType::json()))?; + + if response.status.class() == StatusClass::Success { + Ok(response) + } else { + Err(Error::Client(ClientError::InvalidRequest(response.status))) + } } #[doc(hidden)] @@ -1626,7 +1632,7 @@ fn verify(expected_status_code: u16, mut response: HyperResponse) -> Result<()> debug!("Content: {}", s); - Err(Error::Client(ClientError::UnexpectedStatusCode(response.status))) + Err(Error::Client(ClientError::InvalidRequest(response.status))) } /// Representation of the method of a query to send for the [`get_guilds`] |