Improves error handling

This commit is contained in:
2024-02-06 21:23:04 +00:00
parent 67c6ba878b
commit 450fe84d8b
3 changed files with 40 additions and 4 deletions

View File

@@ -7,6 +7,7 @@ pub enum Error {
Env(std::env::VarError), Env(std::env::VarError),
HttpClient(reqwest::Error), HttpClient(reqwest::Error),
Token(gcp_auth::Error), Token(gcp_auth::Error),
Serde(serde_json::Error),
} }
impl Display for Error { impl Display for Error {
@@ -15,6 +16,7 @@ impl Display for Error {
Error::Env(e) => write!(f, "Environment variable error: {}", e), Error::Env(e) => write!(f, "Environment variable error: {}", e),
Error::HttpClient(e) => write!(f, "HTTP Client error: {}", e), Error::HttpClient(e) => write!(f, "HTTP Client error: {}", e),
Error::Token(e) => write!(f, "Token error: {}", e), Error::Token(e) => write!(f, "Token error: {}", e),
Error::Serde(e) => write!(f, "Serde error: {}", e),
} }
} }
} }
@@ -38,3 +40,9 @@ impl From<gcp_auth::Error> for Error {
Error::Token(e) Error::Token(e)
} }
} }
impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Serde(e)
}
}

View File

@@ -66,8 +66,9 @@ pub struct GenerateContentResponse(pub Vec<ResponseStreamChunk>);
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ResponseStreamChunk { pub struct ResponseStreamChunk {
pub candidates: Vec<Candidate>, pub candidates: Option<Vec<Candidate>>,
pub usage_metadata: Option<UsageMetadata>, pub usage_metadata: Option<UsageMetadata>,
pub error: Option<Error>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@@ -128,3 +129,24 @@ pub struct FunctionParametersProperty {
pub r#type: String, pub r#type: String,
pub description: String, pub description: String,
} }
#[derive(Debug, Serialize, Deserialize)]
pub struct Error {
pub code: i32,
pub message: String,
pub status: String,
pub details: Vec<ErrorDetail>,
}
// TODO: Make ErrorDetail an enum and map to the different types of errors.
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorDetail {
#[serde(rename = "@type")]
pub r#type: String,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Link {
pub description: String,
pub url: String,
}

View File

@@ -66,7 +66,13 @@ impl<T: TokenProvider + Clone> VertexClient<T> {
let txt_json = resp.text().await?; let txt_json = resp.text().await?;
tracing::debug!("Vertex API Response: {}", txt_json); tracing::debug!("Vertex API Response: {}", txt_json);
Ok(serde_json::from_str(&txt_json).unwrap()) match serde_json::from_str(&txt_json) {
Ok(response) => Ok(response),
Err(e) => {
eprintln!("Failed to parse response: {} / {}", txt_json, e);
Err(e.into())
}
}
} }
/// Prompts a conversation to the model. /// Prompts a conversation to the model.
@@ -92,7 +98,7 @@ impl<T: TokenProvider + Clone> VertexClient<T> {
.0 .0
.into_iter() .into_iter()
.flat_map(|chunk| { .flat_map(|chunk| {
chunk.candidates.into_iter().flat_map(|candidate| { chunk.candidates.unwrap().into_iter().flat_map(|candidate| {
candidate candidate
.content .content
.parts .parts
@@ -133,7 +139,7 @@ impl<T: TokenProvider + Clone> VertexClient<T> {
.0 .0
.into_iter() .into_iter()
.flat_map(|chunk| { .flat_map(|chunk| {
chunk.candidates.into_iter().flat_map(|candidate| { chunk.candidates.unwrap().into_iter().flat_map(|candidate| {
candidate candidate
.content .content
.parts .parts