Skip to main content

rig_cat/
error.rs

1//! Project-wide error type.
2
3/// All errors in rig-cat.
4#[derive(Debug)]
5pub enum Error {
6    /// HTTP request failed.
7    Http(ureq::Error),
8    /// JSON serialization/deserialization failed.
9    Json(serde_json::Error),
10    /// IO operation failed.
11    Io(std::io::Error),
12    /// Provider returned an error response.
13    Provider { status: u16, message: String },
14    /// Missing required configuration.
15    Config { field: String },
16    /// Embedding dimension mismatch.
17    DimensionMismatch { expected: usize, got: usize },
18}
19
20impl std::fmt::Display for Error {
21    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
22        match self {
23            Self::Http(e) => write!(f, "HTTP error: {e}"),
24            Self::Json(e) => write!(f, "JSON error: {e}"),
25            Self::Io(e) => write!(f, "IO error: {e}"),
26            Self::Provider { status, message } => {
27                write!(f, "provider error ({status}): {message}")
28            }
29            Self::Config { field } => write!(f, "missing config: {field}"),
30            Self::DimensionMismatch { expected, got } => {
31                write!(f, "dimension mismatch: expected {expected}, got {got}")
32            }
33        }
34    }
35}
36
37impl std::error::Error for Error {
38    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
39        match self {
40            Self::Http(e) => Some(e),
41            Self::Json(e) => Some(e),
42            Self::Io(e) => Some(e),
43            Self::Provider { .. } | Self::Config { .. } | Self::DimensionMismatch { .. } => None,
44        }
45    }
46}
47
48impl From<ureq::Error> for Error {
49    fn from(e: ureq::Error) -> Self { Self::Http(e) }
50}
51
52impl From<serde_json::Error> for Error {
53    fn from(e: serde_json::Error) -> Self { Self::Json(e) }
54}
55
56impl From<std::io::Error> for Error {
57    fn from(e: std::io::Error) -> Self { Self::Io(e) }
58}