Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix telegram http errors #376

Merged
merged 1 commit into from
Sep 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading