mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-03 17:52:12 +01:00
Merge pull request #241 from teloxide/download_error_and_from
Implement `From<_> for RequestError` for more types
This commit is contained in:
commit
61d36e3ac9
2 changed files with 48 additions and 19 deletions
15
CHANGELOG.md
15
CHANGELOG.md
|
@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
## unreleased
|
||||
|
||||
### Added
|
||||
|
||||
- `From<ApiError>`, `From<DownloadError>` and `From<std::io::Error>` impls for `RequestError` ([#241][pr241])
|
||||
|
||||
[pr241]: https://github.com/teloxide/teloxide-core/pull/241
|
||||
|
||||
## 0.7.0 - 2022-07-19
|
||||
|
||||
### Added
|
||||
|
@ -451,7 +457,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
### Changed
|
||||
|
||||
- `NonStrictVec` -> `SemiparsedVec`.
|
||||
- `NonStrictVec` -> `SemiparsedVec`.
|
||||
|
||||
## 0.1.1 - 2020-02-17
|
||||
|
||||
|
@ -523,8 +529,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
- Make `net` module public
|
||||
- Move `Bot::download_file{,_stream}` methods to a new `Download` trait
|
||||
- Impl `Download` for all bot adaptors & the `Bot` itself
|
||||
- Change return type of `download_file_stream` — return `Stream<Result<Bytes>>``,
|
||||
instead of `Future<Result<Stream<Result<Bytes>>>>``
|
||||
- Change return type of `download_file_stream` — return ` Stream<Result<Bytes>>``, instead of `Future<Result<Stream<Result<Bytes>>>>``
|
||||
- Add `api_url` param to standalone versions of `download_file{,_stream}`
|
||||
- Make `net::{TELEGRAM_API_URL, download_file{,_stream}}` pub
|
||||
- Refactor `Bot` ([#29][pr29]):
|
||||
|
@ -565,7 +570,5 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||
|
||||
[pr4]: https://github.com/teloxide/teloxide-core/pull/4
|
||||
[pr44]: https://github.com/teloxide/teloxide-core/pull/44
|
||||
|
||||
|
||||
[`teloxide`]: https://github.com/teloxide/teloxide
|
||||
[`CHANGELOG.md`]: https://github.com/teloxide/teloxide/blob/master/CHANGELOG.md
|
||||
[`changelog.md`]: https://github.com/teloxide/teloxide/blob/master/CHANGELOG.md
|
||||
|
|
|
@ -12,7 +12,7 @@ use crate::types::ResponseParameters;
|
|||
pub enum RequestError {
|
||||
/// A Telegram API error.
|
||||
#[error("A Telegram's error: {0}")]
|
||||
Api(#[source] ApiError),
|
||||
Api(#[from] ApiError),
|
||||
|
||||
/// The group has been migrated to a supergroup with the specified
|
||||
/// identifier.
|
||||
|
@ -26,7 +26,7 @@ pub enum RequestError {
|
|||
|
||||
/// Network error while sending a request to Telegram.
|
||||
#[error("A network error: {0}")]
|
||||
// NOTE: this variant must not be created by anything except the From impl
|
||||
// NOTE: this variant must not be created by anything except the explicit From impl
|
||||
Network(#[source] reqwest::Error),
|
||||
|
||||
/// Error while parsing a response from Telegram.
|
||||
|
@ -45,7 +45,7 @@ pub enum RequestError {
|
|||
|
||||
/// Occurs when trying to send a file to Telegram.
|
||||
#[error("An I/O error: {0}")]
|
||||
Io(#[source] io::Error),
|
||||
Io(#[from] io::Error),
|
||||
}
|
||||
|
||||
/// An error caused by downloading a file.
|
||||
|
@ -53,7 +53,7 @@ pub enum RequestError {
|
|||
pub enum DownloadError {
|
||||
/// A network error while downloading a file from Telegram.
|
||||
#[error("A network error: {0}")]
|
||||
// NOTE: this variant must not be created by anything except the From impl
|
||||
// NOTE: this variant must not be created by anything except the explicit From impl
|
||||
Network(#[source] reqwest::Error),
|
||||
|
||||
/// An I/O error while writing a file to destination.
|
||||
|
@ -762,6 +762,32 @@ pub enum ApiError {
|
|||
Unknown(String),
|
||||
}
|
||||
|
||||
/// This impl allows to use `?` to propagate [`DownloadError`]s in function
|
||||
/// returning [`RequestError`]s. For example:
|
||||
///
|
||||
/// ```rust
|
||||
/// # use teloxide_core::errors::{DownloadError, RequestError};
|
||||
///
|
||||
/// async fn handler() -> Result<(), RequestError> {
|
||||
/// download_file().await?; // `?` just works
|
||||
///
|
||||
/// Ok(())
|
||||
/// }
|
||||
///
|
||||
/// async fn download_file() -> Result<(), DownloadError> {
|
||||
/// /* download file here */
|
||||
/// Ok(())
|
||||
/// }
|
||||
/// ```
|
||||
impl From<DownloadError> for RequestError {
|
||||
fn from(download_err: DownloadError) -> Self {
|
||||
match download_err {
|
||||
DownloadError::Network(err) => RequestError::Network(err),
|
||||
DownloadError::Io(err) => RequestError::Io(err),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<reqwest::Error> for DownloadError {
|
||||
fn from(error: reqwest::Error) -> Self {
|
||||
DownloadError::Network(hide_token(error))
|
||||
|
|
Loading…
Reference in a new issue