aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-02-11 21:10:20 -0800
committerZeyla Hellyer <[email protected]>2017-02-11 21:10:20 -0800
commit7e254c5c6098bb1a47bac26c9895098a46cdc53f (patch)
treed8adb2ff9d709ae32f12286f1432996011367cbf /src/client
parentUpdate voice example to latest changes (diff)
downloadserenity-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')
-rw-r--r--src/client/error.rs2
-rw-r--r--src/client/rest/mod.rs14
2 files changed, 11 insertions, 5 deletions
diff --git a/src/client/error.rs b/src/client/error.rs
index 8a92cdd..1eaa2c6 100644
--- a/src/client/error.rs
+++ b/src/client/error.rs
@@ -142,7 +142,7 @@ pub enum Error {
/// [`Context::edit_channel`]: struct.Context.html#method.edit_channel
UnexpectedChannelType(ChannelType),
/// When a status code was unexpectedly received for a request's status.
- UnexpectedStatusCode(StatusCode),
+ InvalidRequest(StatusCode),
/// When a status is received, but the verification to ensure the response
/// is valid does not recognize the status.
UnknownStatus(u16),
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`]