diff options
| author | Zeyla Hellyer <[email protected]> | 2018-01-01 11:56:38 -0800 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2018-01-01 11:56:38 -0800 |
| commit | be43836839a31714f58e3ffe81dd4b0aeab2ab59 (patch) | |
| tree | dfcd79318f8c94c022d0deb4b65d092a91fe00cf /src/http/mod.rs | |
| parent | Add an event for shard updates (diff) | |
| download | serenity-be43836839a31714f58e3ffe81dd4b0aeab2ab59.tar.xz serenity-be43836839a31714f58e3ffe81dd4b0aeab2ab59.zip | |
Give hyper Response in HTTP errors
Remove the `Error::UnknownStatus` variant, and change
`Error::InvalidRequest(StatusCode)` to
`Error::UnsuccessfulRequest(hyper::Response)`. This is done to give the
user more information on why a request was unsuccessful, as before the
user did not have access to the response body or headers.
Diffstat (limited to 'src/http/mod.rs')
| -rw-r--r-- | src/http/mod.rs | 31 |
1 files changed, 8 insertions, 23 deletions
diff --git a/src/http/mod.rs b/src/http/mod.rs index f7d4335..8abed78 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -48,7 +48,7 @@ use std::collections::BTreeMap; use std::default::Default; use std::fmt::Write as FmtWrite; use std::fs::File; -use std::io::{ErrorKind as IoErrorKind, Read}; +use std::io::ErrorKind as IoErrorKind; use std::path::{Path, PathBuf}; use std::sync::Arc; @@ -1708,7 +1708,7 @@ pub fn send_files<'a, T, It: IntoIterator<Item=T>>(channel_id: u64, files: It, m let response = request.send()?; if response.status.class() != StatusClass::Success { - return Err(Error::Http(HttpError::InvalidRequest(response.status))); + return Err(Error::Http(HttpError::UnsuccessfulRequest(response))); } serde_json::from_reader::<HyperResponse, Message>(response) @@ -1832,7 +1832,7 @@ fn request<'a, F>(route: Route, f: F) -> Result<HyperResponse> if response.status.class() == StatusClass::Success { Ok(response) } else { - Err(Error::Http(HttpError::InvalidRequest(response.status))) + Err(Error::Http(HttpError::UnsuccessfulRequest(response))) } } @@ -1849,30 +1849,15 @@ pub(crate) fn retry<'a, F>(f: F) -> HyperResult<HyperResponse> } } -fn verify(expected_status_code: u16, mut response: HyperResponse) -> Result<()> { - let expected_status = match expected_status_code { - 200 => StatusCode::Ok, - 204 => StatusCode::NoContent, - 401 => StatusCode::Unauthorized, - _ => { - let client_error = HttpError::UnknownStatus(expected_status_code); - - return Err(Error::Http(client_error)); - }, - }; - - if response.status == expected_status { +fn verify(expected: u16, response: HyperResponse) -> Result<()> { + if response.status.to_u16() == expected { return Ok(()); } - debug!("Expected {}, got {}", expected_status_code, response.status); - - let mut s = String::default(); - response.read_to_string(&mut s)?; - - debug!("Content: {}", s); + debug!("Expected {}, got {}", expected, response.status); + trace!("Unsuccessful response: {:?}", response); - Err(Error::Http(HttpError::InvalidRequest(response.status))) + Err(Error::Http(HttpError::UnsuccessfulRequest(response))) } /// Enum that allows a user to pass a `Path` or a `File` type to `send_files` |