Skip to content

Commit

Permalink
fix telegram http errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrat555 committed Sep 25, 2024
1 parent a3cab13 commit 11e8428
Showing 1 changed file with 26 additions and 42 deletions.
68 changes: 26 additions & 42 deletions src/bot/telegram_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,8 @@ pub struct Api {

#[derive(Debug)]
pub enum Error {
HttpError(HttpError),
ApiError(ErrorResponse),
}

#[derive(Eq, PartialEq, Debug)]
pub struct HttpError {
pub code: u16,
pub message: String,
Http { code: u16, message: String },
Api(ErrorResponse),
}

impl Default for Api {
Expand Down Expand Up @@ -174,29 +168,23 @@ impl From<isahc::http::Error> for Error {
fn from(error: isahc::http::Error) -> Self {
let message = format!("{error:?}");

let error = HttpError { code: 500, message };

Error::HttpError(error)
Self::Http { code: 500, message }
}
}

impl From<std::io::Error> for Error {
fn from(error: std::io::Error) -> Self {
let message = format!("{error:?}");

let error = HttpError { code: 500, message };

Error::HttpError(error)
Self::Http { code: 500, message }
}
}

impl From<isahc::Error> for Error {
fn from(error: isahc::Error) -> Self {
let message = format!("{error:?}");

let error = HttpError { code: 500, message };

Error::HttpError(error)
Self::Http { code: 500, message }
}
}

Expand Down Expand Up @@ -228,28 +216,28 @@ impl TelegramApi for Api {
let mut bytes = Vec::new();
response.copy_to(&mut bytes)?;

let parsed_result: Result<T2, serde_json::Error> = serde_json::from_slice(&bytes);

match parsed_result {
Ok(result) => Ok(result),
Err(serde_error) => {
log::error!("Failed to parse a response {serde_error:?}");

let parsed_error: Result<ErrorResponse, serde_json::Error> =
serde_json::from_slice(&bytes);

match parsed_error {
Ok(result) => Err(Error::ApiError(result)),
Err(error) => {
let message = format!("{:?} {error:?}", std::str::from_utf8(&bytes));

let error = HttpError { code: 500, message };

Err(Error::HttpError(error))
serde_json::from_slice(&bytes).map_err(|_| {
match serde_json::from_slice::<ErrorResponse>(&bytes) {
Ok(result) => {
log::error!(
"Failed to parse a response {result:?} - {:?}",
std::str::from_utf8(&bytes)
);
Error::Api(result)
}
Err(error) => {
log::error!(
"Failed to parse an error response {error:?} - {:?}",
std::str::from_utf8(&bytes)
);

Error::Http {
code: 500,
message: format!("{error:?}"),
}
}
}
}
})
}

// isahc doesn't support multipart uploads
Expand All @@ -261,12 +249,8 @@ impl TelegramApi for Api {
_params: T1,
_files: Vec<(&str, PathBuf)>,
) -> Result<T2, Error> {
let error = HttpError {
code: 500,
message: "isahc doesn't support form data requests".to_string(),
};

Err(Error::HttpError(error))
let message = "isahc doesn't support form data requests".to_string();
Err(Error::Http { code: 500, message })
}
}

Expand Down

0 comments on commit 11e8428

Please sign in to comment.