aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZeyla Hellyer <[email protected]>2017-05-27 09:13:06 -0700
committerZeyla Hellyer <[email protected]>2017-05-27 09:13:06 -0700
commit0102706321a00cfb39b356bdf2cf8d523b93a8ec (patch)
tree6d2ba524b5ddeabf590b1c10d03900dbd1d8ef21
parentFix GuildChannel::create_permission anchor link (diff)
downloadserenity-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.
-rw-r--r--src/http/mod.rs12
-rw-r--r--src/model/channel/channel_id.rs6
-rw-r--r--src/model/channel/group.rs5
-rw-r--r--src/model/channel/guild_channel.rs5
-rw-r--r--src/model/channel/mod.rs5
-rw-r--r--src/model/channel/private_channel.rs5
6 files changed, 38 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)
}
diff --git a/src/model/channel/channel_id.rs b/src/model/channel/channel_id.rs
index b360bd1..121399a 100644
--- a/src/model/channel/channel_id.rs
+++ b/src/model/channel/channel_id.rs
@@ -382,6 +382,12 @@ impl ChannelId {
/// [`ModelError::MessageTooLong`] will be returned, containing the number
/// of unicode code points over the limit.
///
+ /// Returns an
+ /// [`HttpError::InvalidRequest(PayloadTooLarge)`][`HttpError::InvalidRequest`]
+ /// if the file is too large to send.
+ ///
+ ///
+ /// [`HttpError::InvalidRequest`]: ../http/enum.HttpError.html#variant.InvalidRequest
/// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong
/// [`CreateMessage::content`]: ../builder/struct.CreateMessage.html#method.content
/// [`GuildChannel`]: struct.GuildChannel.html
diff --git a/src/model/channel/group.rs b/src/model/channel/group.rs
index adafdc8..aab3e3a 100644
--- a/src/model/channel/group.rs
+++ b/src/model/channel/group.rs
@@ -285,11 +285,16 @@ impl Group {
///
/// # Errors
///
+ /// Returns an
+ /// [`HttpError::InvalidRequest(PayloadTooLarge)`][`HttpError::InvalidRequest`]
+ /// if the file is too large to send.
+ ///
/// If the content of the message is over the above limit, then a
/// [`ModelError::MessageTooLong`] will be returned, containing the number
/// of unicode code points over the limit.
///
/// [`ChannelId::send_file`]: struct.ChannelId.html#method.send_file
+ /// [`HttpError::InvalidRequest`]: ../http/enum.HttpError.html#variant.InvalidRequest
/// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong
/// [Attach Files]: permissions/constant.ATTACH_FILES.html
/// [Send Messages]: permissions/constant.SEND_MESSAGES.html
diff --git a/src/model/channel/guild_channel.rs b/src/model/channel/guild_channel.rs
index 93584b6..25dcc36 100644
--- a/src/model/channel/guild_channel.rs
+++ b/src/model/channel/guild_channel.rs
@@ -544,11 +544,16 @@ impl GuildChannel {
///
/// # Errors
///
+ /// Returns an
+ /// [`HttpError::InvalidRequest(PayloadTooLarge)`][`HttpError::InvalidRequest`]
+ /// if the file is too large to send.
+ ///
/// If the content of the message is over the above limit, then a
/// [`ModelError::MessageTooLong`] will be returned, containing the number
/// of unicode code points over the limit.
///
/// [`ChannelId::send_file`]: struct.ChannelId.html#method.send_file
+ /// [`HttpError::InvalidRequest`]: ../http/enum.HttpError.html#variant.InvalidRequest
/// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong
/// [Attach Files]: permissions/constant.ATTACH_FILES.html
/// [Send Messages]: permissions/constant.SEND_MESSAGES.html
diff --git a/src/model/channel/mod.rs b/src/model/channel/mod.rs
index b82b22c..300f402 100644
--- a/src/model/channel/mod.rs
+++ b/src/model/channel/mod.rs
@@ -280,11 +280,16 @@ impl Channel {
///
/// # Errors
///
+ /// Returns an
+ /// [`HttpError::InvalidRequest(PayloadTooLarge)`][`HttpError::InvalidRequest`]
+ /// if the file is too large to send.
+ ///
/// If the content of the message is over the above limit, then a
/// [`ModelError::MessageTooLong`] will be returned, containing the number
/// of unicode code points over the limit.
///
/// [`ChannelId::send_file`]: struct.ChannelId.html#method.send_file
+ /// [`HttpError::InvalidRequest`]: ../http/enum.HttpError.html#variant.InvalidRequest
/// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong
/// [Attach Files]: permissions/constant.ATTACH_FILES.html
/// [Send Messages]: permissions/constant.SEND_MESSAGES.html
diff --git a/src/model/channel/private_channel.rs b/src/model/channel/private_channel.rs
index 9e2aec1..d63efff 100644
--- a/src/model/channel/private_channel.rs
+++ b/src/model/channel/private_channel.rs
@@ -231,11 +231,16 @@ impl PrivateChannel {
///
/// # Errors
///
+ /// Returns an
+ /// [`HttpError::InvalidRequest(PayloadTooLarge)`][`HttpError::InvalidRequest`]
+ /// if the file is too large to send.
+ ///
/// If the content of the message is over the above limit, then a
/// [`ModelError::MessageTooLong`] will be returned, containing the number
/// of unicode code points over the limit.
///
/// [`ChannelId::send_file`]: struct.ChannelId.html#method.send_file
+ /// [`HttpError::InvalidRequest`]: ../http/enum.HttpError.html#variant.InvalidRequest
/// [`ModelError::MessageTooLong`]: enum.ModelError.html#variant.MessageTooLong
/// [Attach Files]: permissions/constant.ATTACH_FILES.html
/// [Send Messages]: permissions/constant.SEND_MESSAGES.html