open_routerer/
error.rs

1#![allow(unused_variables)]
2#![allow(clippy::enum_variant_names)]
3
4use reqwest::Response;
5use serde_jsonc2::Value;
6use thiserror::Error;
7
8/// Centralized error type for the OpenRouter client library.
9/// Extended to include structured output errors.
10#[derive(Error, Debug)]
11pub enum Error {
12    #[error("HTTP error: {0}")]
13    HttpError(#[from] reqwest::Error),
14
15    #[error("API error (status {code}): {message}")]
16    ApiError {
17        code: u16,
18        message: String,
19        metadata: Option<Value>,
20    },
21
22    #[error("Invalid configuration: {0}")]
23    ConfigError(String),
24
25    #[error("Structured output not supported by the provider/model")]
26    StructuredOutputNotSupported,
27
28    #[error("Schema validation error: {0}")]
29    SchemaValidationError(String),
30
31    #[error("Unknown error")]
32    Unknown,
33}
34
35pub type Result<T> = std::result::Result<T, Error>;
36
37impl Error {
38    /// Creates an API error from a given HTTP response.
39    pub async fn from_response(response: Response) -> Result<Self> {
40        let status = response.status().as_u16();
41        let text = response.text().await.unwrap_or_default();
42        Ok(Error::ApiError {
43            code: status,
44            message: text,
45            metadata: None,
46        })
47    }
48}