Skip to content

Commit

Permalink
refactor: simplify From<ureq::Error>
Browse files Browse the repository at this point in the history
  • Loading branch information
EdJoPaTo committed Aug 15, 2024
1 parent 0ca96f9 commit 3bcd66c
Showing 1 changed file with 13 additions and 24 deletions.
37 changes: 13 additions & 24 deletions src/api/telegram_api_impl.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::Error;
use super::HttpError;
use crate::api_traits::ErrorResponse;
use crate::api_traits::TelegramApi;
use multipart::client::lazy::Multipart;
use serde_json::Value;
Expand Down Expand Up @@ -61,29 +60,19 @@ impl From<ureq::Error> for Error {
fn from(error: ureq::Error) -> Self {
match error {
ureq::Error::Status(code, response) => match response.into_string() {
Ok(message) => {
let json_result: Result<ErrorResponse, serde_json::Error> =
serde_json::from_str(&message);

match json_result {
Ok(result) => Self::Api(result),
Err(_) => {
let error = HttpError { code, message };
Self::Http(error)
}
}
}
Err(_) => {
let message = "Failed to decode response".to_string();
let error = HttpError { code, message };
Self::Http(error)
}
Ok(message) => match serde_json::from_str(&message) {
Ok(json_result) => Self::Api(json_result),
Err(_) => Self::Http(HttpError { code, message }),
},
Err(_) => Self::Http(HttpError {
code,
message: "Failed to decode response".to_string(),
}),
},
ureq::Error::Transport(transport_error) => {
let message = format!("{transport_error:?}");
let error = HttpError { message, code: 500 };
Self::Http(error)
}
ureq::Error::Transport(transport_error) => Self::Http(HttpError {
message: format!("{transport_error:?}"),
code: 500,
}),
}
}
}
Expand All @@ -106,7 +95,6 @@ impl TelegramApi for Api {
None => prepared_request.call()?,
Some(data) => {
let json = Self::encode_params(&data)?;

prepared_request.send_string(&json)?
}
};
Expand Down Expand Up @@ -245,6 +233,7 @@ mod tests {
use crate::api_params::StopPollParams;
use crate::api_params::UnbanChatMemberParams;
use crate::api_params::UnpinChatMessageParams;
use crate::api_traits::ErrorResponse;
use crate::objects::BotCommand;
use crate::objects::ChatPermissions;
use crate::objects::InlineQueryResultVenue;
Expand Down

0 comments on commit 3bcd66c

Please sign in to comment.