Skip to content

Commit

Permalink
improve AI Studio error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
jelni committed Jul 8, 2024
1 parent 2c41393 commit b367e3b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
33 changes: 15 additions & 18 deletions src/apis/makersuite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use url::Url;
use crate::commands::CommandError;

pub enum GenerationError {
NetworkError(reqwest::Error),
GoogleError(Error),
Network(reqwest::Error),
Google(Vec<Error>),
}

#[derive(Serialize)]
Expand Down Expand Up @@ -120,21 +120,13 @@ pub struct ErrorResponse {

#[derive(Deserialize)]
pub struct Error {
pub code: Option<u32>,
pub code: u16,
pub message: String,
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Google error").unwrap();

if let Some(code) = self.code {
write!(f, " {code}").unwrap();
}

write!(f, ": {}", self.message).unwrap();

Ok(())
write!(f, "Google error {}: {}", self.code, self.message)
}
}

Expand Down Expand Up @@ -176,19 +168,24 @@ pub async fn stream_generate_content(
let response = match response {
Ok(response) => response,
Err(err) => {
tx.send(Err(GenerationError::NetworkError(err))).unwrap();
tx.send(Err(GenerationError::Network(err))).unwrap();
return;
}
};

if response.status() != StatusCode::OK {
let error_response = response.json::<ErrorResponse>().await;
let status = response.status();

if status != StatusCode::OK {
let error_response = response.json::<Vec<ErrorResponse>>().await;

match error_response {
Ok(error_response) => {
tx.send(Err(GenerationError::GoogleError(error_response.error))).unwrap();
tx.send(Err(GenerationError::Google(
error_response.into_iter().map(|error| error.error).collect(),
)))
.unwrap();
}
Err(err) => tx.send(Err(GenerationError::NetworkError(err))).unwrap(),
Err(err) => tx.send(Err(GenerationError::Network(err))).unwrap(),
}

return;
Expand All @@ -201,7 +198,7 @@ pub async fn stream_generate_content(
let part = match part {
Ok(part) => part,
Err(err) => {
tx.send(Err(GenerationError::NetworkError(err))).unwrap();
tx.send(Err(GenerationError::Network(err))).unwrap();
return;
}
};
Expand Down
6 changes: 4 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ impl From<ConversionError> for CommandError {
impl From<GenerationError> for CommandError {
fn from(value: GenerationError) -> Self {
match value {
GenerationError::NetworkError(err) => Self::Reqwest(err),
GenerationError::GoogleError(err) => Self::Custom(err.to_string()),
GenerationError::Network(err) => Self::Reqwest(err),
GenerationError::Google(err) => Self::Custom(
err.into_iter().map(|error| error.to_string()).collect::<Vec<_>>().join("\n"),
),
}
}
}
Expand Down
10 changes: 8 additions & 2 deletions src/utilities/command_dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::error::Error;
use std::sync::Arc;
use std::time::{Duration, Instant};

Expand Down Expand Up @@ -110,8 +111,13 @@ async fn report_command_error(
.await?
}
CommandError::Reqwest(err) => {
log::error!("HTTP error in the {command} command: {err}");
context.reply(err.without_url().to_string()).await?
let err = err.without_url();

let text =
err.source().map_or_else(|| err.to_string(), |source| format!("{err}: {source}"));

log::error!("HTTP error in the {command} command: {text}");
context.reply(text).await?
}
};

Expand Down

0 comments on commit b367e3b

Please sign in to comment.