Merge branch 'master' into fix_some_message_deserialization_bugs

This commit is contained in:
Hirrolot 2021-12-28 19:49:08 +07:00 committed by GitHub
commit ff21d876c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 36 additions and 10 deletions

View file

@ -40,10 +40,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Mark `ApiError` as `non_exhaustive` ([#125][pr125])
- `InputFile` and related structures now do **not** implement `PartialEq`, `Eq` and `Hash` ([#133][pr133])
- How forwarded messages are represented ([#151][pr151])
- `RequestError::InvalidJson` now has a `raw` field with raw json for easier debugability ([#150][pr150])
[pr115]: https://github.com/teloxide/teloxide-core/pull/115
[pr125]: https://github.com/teloxide/teloxide-core/pull/125
[pr134]: https://github.com/teloxide/teloxide-core/pull/134
[pr150]: https://github.com/teloxide/teloxide-core/pull/150
### Fixed
@ -57,6 +59,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Type of response for `CopyMessage` method ([#141](pr141), [#142](pr142))
- Bad request serialization when the `language` field of `MessageEntityKind::Pre` is `None` ([#145](pr145))
- Deserialization of `MediaKind::Venue` ([#147][pr147])
- Deserialization of `VoiceChat{Started,Ended}` messages ([#153][pr153])
[pr119]: https://github.com/teloxide/teloxide-core/pull/119
[pr133]: https://github.com/teloxide/teloxide-core/pull/133
@ -65,6 +68,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[pr143]: https://github.com/teloxide/teloxide-core/pull/143
[pr145]: https://github.com/teloxide/teloxide-core/pull/145
[pr147]: https://github.com/teloxide/teloxide-core/pull/147
[pr153]: https://github.com/teloxide/teloxide-core/pull/153
[issue473]: https://github.com/teloxide/teloxide/issues/473
[issue427]: https://github.com/teloxide/teloxide/issues/427

View file

@ -43,8 +43,13 @@ pub enum RequestError {
/// description of the error.
///
/// [open an issue]: https://github.com/teloxide/teloxide/issues/new
#[error("An error while parsing JSON: {0}")]
InvalidJson(#[source] serde_json::Error),
#[error("An error while parsing JSON: {source} (raw: {raw:?})")]
InvalidJson {
#[source]
source: serde_json::Error,
/// The raw string JSON that couldn't been parsed
raw: Box<str>,
},
/// Occurs when trying to send a file to Telegram.
#[error("An I/O error: {0}")]

View file

@ -85,9 +85,12 @@ where
tokio::time::sleep(DELAY_ON_SERVER_ERROR).await;
}
serde_json::from_str::<TelegramResponse<T>>(
&response.text().await.map_err(RequestError::Network)?,
)
.map_err(RequestError::InvalidJson)?
.into()
let text = response.text().await.map_err(RequestError::Network)?;
serde_json::from_str::<TelegramResponse<T>>(&text)
.map_err(|source| RequestError::InvalidJson {
source,
raw: text.into(),
})?
.into()
}

View file

@ -32,7 +32,7 @@ impl ChatId {
&Self::Id(id @ MIN_CHAT_ID..=-1) => Chat(-id as _),
&Self::Id(id @ MIN_CHANNEL_ID..=MAX_CHANNEL_ID) => Channel((MAX_CHANNEL_ID - id) as _),
&Self::Id(id) => {
debug_assert!(0 < id && id < MAX_USER_ID, "malformed chat id");
debug_assert!(0 < id && id < MAX_USER_ID, "malformed chat id: {}", id);
User(id as _)
}
Self::ChannelUsername(_) => return None,

View file

@ -1442,4 +1442,18 @@ mod tests {
}
)
}
/// Regression test for <https://github.com/teloxide/teloxide/issues/475>
#[test]
fn issue_475() {
let json = r#"{"message_id":198295,"from":{"id":1087968824,"is_bot":true,"first_name":"Group","username":"GroupAnonymousBot"},"sender_chat":{"id":-1001331354980,"title":"C++ Together 2.0","username":"cpptogether","type":"supergroup"},"chat":{"id":-1001331354980,"title":"C++ Together 2.0","username":"cpptogether","type":"supergroup"},"date":1638236631,"voice_chat_started":{}}"#;
let message: Message = serde_json::from_str(json).unwrap();
assert!(matches!(message.kind, MessageKind::VoiceChatStarted { .. }));
// FIXME(waffle): it seems like we are losing `sender_chat` in some
// cases inclusing this
// assert!(message.sender_chat().is_some());
}
}

View file

@ -3,4 +3,4 @@ use serde::{Deserialize, Serialize};
/// This object represents a service message about a voice chat ended in the
/// chat.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct VoiceChatEnded;
pub struct VoiceChatEnded {}

View file

@ -3,4 +3,4 @@ use serde::{Deserialize, Serialize};
/// This object represents a service message about a voice chat started in the
/// chat. Currently holds no information.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub struct VoiceChatStarted;
pub struct VoiceChatStarted {}