Improve error responses from apis

This commit is contained in:
Silas Marvin 2024-06-02 20:59:36 -07:00
parent cbaed9a0c4
commit b4ed5b8c96
3 changed files with 29 additions and 4 deletions

View File

@ -1,3 +1,5 @@
use std::collections::HashMap;
use anyhow::Context;
use serde::Deserialize;
use serde_json::{json, Value};
@ -52,6 +54,9 @@ struct AnthropicChatMessage {
struct AnthropicChatResponse {
content: Option<Vec<AnthropicChatMessage>>,
error: Option<Value>,
#[serde(default)]
#[serde(flatten)]
pub other: HashMap<String, Value>,
}
impl Anthropic {
@ -103,7 +108,10 @@ impl Anthropic {
} else if let Some(mut content) = res.content {
Ok(std::mem::take(&mut content[0].text))
} else {
anyhow::bail!("Uknown error while making request to OpenAI")
anyhow::bail!(
"Uknown error while making request to Anthropic: {:?}",
res.other
)
}
}

View File

@ -97,7 +97,10 @@ impl MistralFIM {
} else if let Some(choices) = res.choices {
Ok(choices[0].message.content.clone())
} else {
anyhow::bail!("Unknown error while making request to OpenAI")
anyhow::bail!(
"Unknown error while making request to MistralFIM: {:?}",
res.other
);
}
}
}

View File

@ -1,3 +1,5 @@
use std::collections::HashMap;
use anyhow::Context;
use serde::Deserialize;
use serde_json::{json, Value};
@ -64,6 +66,9 @@ struct OpenAICompletionsChoice {
struct OpenAICompletionsResponse {
choices: Option<Vec<OpenAICompletionsChoice>>,
error: Option<Value>,
#[serde(default)]
#[serde(flatten)]
pub other: HashMap<String, Value>,
}
#[derive(Deserialize)]
@ -75,6 +80,9 @@ pub struct OpenAIChatChoices {
pub struct OpenAIChatResponse {
pub choices: Option<Vec<OpenAIChatChoices>>,
pub error: Option<Value>,
#[serde(default)]
#[serde(flatten)]
pub other: HashMap<String, Value>,
}
impl OpenAI {
@ -130,7 +138,10 @@ impl OpenAI {
} else if let Some(mut choices) = res.choices {
Ok(std::mem::take(&mut choices[0].text))
} else {
anyhow::bail!("Uknown error while making request to OpenAI")
anyhow::bail!(
"Uknown error while making request to OpenAI: {:?}",
res.other
)
}
}
@ -170,7 +181,10 @@ impl OpenAI {
} else if let Some(choices) = res.choices {
Ok(choices[0].message.content.clone())
} else {
anyhow::bail!("Unknown error while making request to OpenAI")
anyhow::bail!(
"Unknown error while making request to OpenAI: {:?}",
res.other
)
}
}