aboutsummaryrefslogtreecommitdiff
path: root/src/http
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2018-01-01 11:56:38 -0800
committerZeyla Hellyer <[email protected]>2018-01-01 11:56:38 -0800
commitbe43836839a31714f58e3ffe81dd4b0aeab2ab59 (patch)
treedfcd79318f8c94c022d0deb4b65d092a91fe00cf /src/http
parentAdd an event for shard updates (diff)
downloadserenity-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')
-rw-r--r--src/http/error.rs16
-rw-r--r--src/http/mod.rs31
2 files changed, 15 insertions, 32 deletions
diff --git a/src/http/error.rs b/src/http/error.rs
index 6e51358..12de978 100644
--- a/src/http/error.rs
+++ b/src/http/error.rs
@@ -1,20 +1,17 @@
-use hyper::status::StatusCode;
+use hyper::client::Response;
use std::error::Error as StdError;
use std::fmt::{Display, Formatter, Result as FmtResult};
-#[derive(Clone, Debug, Eq, Hash, PartialEq)]
+#[derive(Debug)]
pub enum Error {
- /// When a status code was unexpectedly received for a request's status.
- InvalidRequest(StatusCode),
+ /// When a non-successful status code was received for a request.
+ UnsuccessfulRequest(Response),
/// When the decoding of a ratelimit header could not be properly decoded
/// into an `i64`.
RateLimitI64,
/// When the decoding of a ratelimit header could not be properly decoded
/// from UTF-8.
RateLimitUtf8,
- /// When a status is received, but the verification to ensure the response
- /// is valid does not recognize the status.
- UnknownStatus(u16),
}
impl Display for Error {
@@ -24,10 +21,11 @@ impl Display for Error {
impl StdError for Error {
fn description(&self) -> &str {
match *self {
- Error::InvalidRequest(_) => "Received an unexpected status code",
+ Error::UnsuccessfulRequest(_) => {
+ "A non-successful response status code was received"
+ },
Error::RateLimitI64 => "Error decoding a header into an i64",
Error::RateLimitUtf8 => "Error decoding a header from UTF-8",
- Error::UnknownStatus(_) => "Verification does not understand status",
}
}
}
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`