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    /// Generic error with message.
56    #[error("{0}")]
57    Other(String),
58}
59
60impl Error {
61    /// Create a new API error.
62    pub fn api(status: u16, message: impl Into<String>) -> Self {
63        Self::Api {
64            status,
65            message: message.into(),
66        }
67    }
68
69    /// Create a missing config error.
70    pub fn missing_config(key: impl Into<String>) -> Self {
71        Self::MissingConfig(key.into())
72    }
73
74    /// Create an unsupported provider error.
75    pub fn unsupported_provider(provider: impl Into<String>) -> Self {
76        Self::UnsupportedProvider(provider.into())
77    }
78
79    /// Create an unable to infer provider error.
80    pub fn unable_to_infer_provider(model: impl Into<String>) -> Self {
81        Self::UnableToInferProvider(model.into())
82    }
83
84    /// Create a generic error.
85    pub fn other(message: impl Into<String>) -> Self {
86        Self::Other(message.into())
87    }
88}