From 0102706321a00cfb39b356bdf2cf8d523b93a8ec Mon Sep 17 00:00:00 2001 From: Zeyla Hellyer Date: Sat, 27 May 2017 09:13:06 -0700 Subject: Fix incorrect attempted send_file deserialization If http::send_file received a non-success status code due to reasons such as sending to an invalid channel Id, not having permissions, or sending too large of a file, then it would still try to deserialize a response as if it were valid. To fix this, ensure that the response is successful. Document that if the file sent is too large, then `HttpError::InvalidRequest(PayloadTooLarge)` is returned. --- src/http/mod.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src/http') diff --git a/src/http/mod.rs b/src/http/mod.rs index d15cf45..eb4235d 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -1370,6 +1370,14 @@ pub fn remove_group_recipient(group_id: u64, user_id: u64) -> Result<()> { } /// Sends a file to a channel. +/// +/// # Errors +/// +/// Returns an +/// [`HttpError::InvalidRequest(PayloadTooLarge)`][`HttpError::InvalidRequest`] +/// if the file is too large to send. +/// +/// [`HttpError::InvalidRequest`]: enum.HttpError.html#variant.InvalidRequest pub fn send_file(channel_id: u64, mut file: R, filename: &str, map: JsonMap) -> Result { let uri = format!(api!("/channels/{}/messages"), channel_id); @@ -1400,6 +1408,10 @@ pub fn send_file(channel_id: u64, mut file: R, filename: &str, map: Jso let response = request.send()?; + if response.status.class() != StatusClass::Success { + return Err(Error::Http(HttpError::InvalidRequest(response.status))); + } + serde_json::from_reader::(response).map_err(From::from) } -- cgit v1.2.3