Skip to content

Commit

Permalink
Fix incorrect attempted send_file deserialization
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Zeyla Hellyer committed May 27, 2017
1 parent ddb2509 commit 0102706
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/http/mod.rs
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 6 additions & 0 deletions src/model/channel/channel_id.rs
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/model/channel/group.rs
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/model/channel/guild_channel.rs
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/model/channel/mod.rs
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/model/channel/private_channel.rs
Expand Up @@ -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
Expand Down

0 comments on commit 0102706

Please sign in to comment.