diff options
| author | Zeyla Hellyer <[email protected]> | 2017-05-27 09:13:06 -0700 |
|---|---|---|
| committer | Zeyla Hellyer <[email protected]> | 2017-05-27 09:13:06 -0700 |
| commit | 0102706321a00cfb39b356bdf2cf8d523b93a8ec (patch) | |
| tree | 6d2ba524b5ddeabf590b1c10d03900dbd1d8ef21 /src/http/mod.rs | |
| parent | Fix GuildChannel::create_permission anchor link (diff) | |
| download | serenity-0102706321a00cfb39b356bdf2cf8d523b93a8ec.tar.xz serenity-0102706321a00cfb39b356bdf2cf8d523b93a8ec.zip | |
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.
Diffstat (limited to 'src/http/mod.rs')
| -rw-r--r-- | src/http/mod.rs | 12 |
1 files changed, 12 insertions, 0 deletions
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<R: Read>(channel_id: u64, mut file: R, filename: &str, map: JsonMap) -> Result<Message> { let uri = format!(api!("/channels/{}/messages"), channel_id); @@ -1400,6 +1408,10 @@ pub fn send_file<R: Read>(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::<HyperResponse, Message>(response).map_err(From::from) } |