Skip to main content

objectiveai_api/chat/completions/upstream/
error.rs

1//! Error types for upstream provider operations.
2
3/// Errors that can occur when communicating with upstream providers.
4#[derive(thiserror::Error, Debug)]
5pub enum Error {
6    /// Error from the OpenRouter provider.
7    #[error("openrouter error: {0}")]
8    OpenRouter(#[from] super::openrouter::Error),
9    /// Failed to fetch a BYOK API key.
10    #[error("fetch BYOK error: {0}")]
11    FetchByok(objectiveai::error::ResponseError),
12    /// Multiple errors occurred during fallback attempts.
13    #[error("multiple upstream errors: {0:?}")]
14    MultipleErrors(Vec<Error>),
15    /// The upstream returned an empty stream.
16    #[error("empty upstream stream")]
17    EmptyStream,
18}
19
20impl objectiveai::error::StatusError for Error {
21    fn status(&self) -> u16 {
22        match self {
23            Error::OpenRouter(e) => e.status(),
24            Error::FetchByok(e) => e.status(),
25            Error::MultipleErrors(_) => 500,
26            Error::EmptyStream => 500,
27        }
28    }
29
30    fn message(&self) -> Option<serde_json::Value> {
31        match self {
32            Error::OpenRouter(e) => e.message(),
33            Error::FetchByok(e) => e.message(),
34            Error::MultipleErrors(errors) => Some(serde_json::json!({
35                "kind": "multiple_upstream_errors",
36                "errors": errors.iter().map(|e| {
37                    serde_json::json!({
38                        "status": e.status(),
39                        "message": e.message(),
40                    })
41                }).collect::<Vec<_>>(),
42            })),
43            Error::EmptyStream => Some(serde_json::json!({
44                "kind": "empty_upstream_stream",
45            })),
46        }
47    }
48}