Merge pull request #150 from teloxide/invalid_json_raw

Add `RequestError::InvalidJson::raw` field
This commit is contained in:
Hirrolot 2021-12-25 14:03:58 +06:00 committed by GitHub
commit 54f4281754
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 7 deletions

View file

@ -37,10 +37,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use `url::Url` for urls, use `chrono::DateTime<Utc>` for dates in types ([#115][pr115])
- Mark `ApiError` as `non_exhaustive` ([#125][pr125])
- `InputFile` and related structures now do **not** implement `PartialEq`, `Eq` and `Hash` ([#133][pr133])
- `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

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()
}