agent_chain_core/
error.rs

1//! Error types for agent-chain.
2//!
3//! This module provides error types used across the crate for handling
4//! various failure modes in chat model operations.
5
6use thiserror::Error;
7
8/// Result type alias for agent-chain operations.
9pub type Result<T> = std::result::Result<T, Error>;
10
11/// Main error type for agent-chain operations.
12#[derive(Debug, Error)]
13pub enum Error {
14    /// Error from HTTP requests.
15    #[error("HTTP error: {0}")]
16    Http(#[from] reqwest::Error),
17
18    /// Error parsing JSON.
19    #[error("JSON error: {0}")]
20    Json(#[from] serde_json::Error),
21
22    /// IO error.
23    #[error("IO error: {0}")]
24    Io(#[from] std::io::Error),
25
26    /// API returned an error response.
27    #[error("API error ({status}): {message}")]
28    Api {
29        /// HTTP status code.
30        status: u16,
31        /// Error message from the API.
32        message: String,
33    },
34
35    /// Missing required configuration.
36    #[error("Missing configuration: {0}")]
37    MissingConfig(String),
38
39    /// Unsupported provider.
40    #[error("Unsupported provider: {0}")]
41    UnsupportedProvider(String),
42
43    /// Unable to infer provider from model name.
44    #[error("Unable to infer provider for model '{0}'. Please specify model_provider explicitly.")]
45    UnableToInferProvider(String),
46
47    /// Invalid model configuration.
48    #[error("Invalid configuration: {0}")]
49    InvalidConfig(String),
50
51    /// Tool invocation error.
52    #[error("Tool invocation error: {0}")]
53    ToolInvocation(String),
54
55    /// Feature or method not implemented.
56    #[error("Not implemented: {0}")]
57    NotImplemented(String),
58
59    /// Generic error with message.
60    #[error("{0}")]
61    Other(String),
62}
63
64impl Error {
65    /// Create a new API error.
66    pub fn api(status: u16, message: impl Into<String>) -> Self {
67        Self::Api {
68            status,
69            message: message.into(),
70        }
71    }
72
73    /// Create a missing config error.
74    pub fn missing_config(key: impl Into<String>) -> Self {
75        Self::MissingConfig(key.into())
76    }
77
78    /// Create an unsupported provider error.
79    pub fn unsupported_provider(provider: impl Into<String>) -> Self {
80        Self::UnsupportedProvider(provider.into())
81    }
82
83    /// Create an unable to infer provider error.
84    pub fn unable_to_infer_provider(model: impl Into<String>) -> Self {
85        Self::UnableToInferProvider(model.into())
86    }
87
88    /// Create a generic error.
89    pub fn other(message: impl Into<String>) -> Self {
90        Self::Other(message.into())
91    }
92}