portkey_sdk/
error.rs

1//! Error types for the Portkey SDK.
2
3use crate::builder::PortkeyBuilderError;
4
5/// Error type for Portkey API operations.
6///
7/// This enum represents all possible errors that can occur when using the Portkey SDK,
8/// from HTTP transport errors to API-specific failures and configuration issues.
9///
10/// # Examples
11///
12/// Handling different error types:
13///
14/// ```no_run
15/// use portkey_sdk::{Error, Result, PortkeyClient};
16/// use portkey_sdk::service::ModelsService;
17///
18/// # async fn example() -> Result<()> {
19/// let client: PortkeyClient = PortkeyClient::from_env()?;
20///
21/// // Example error handling
22/// match client.list_models(None).await {
23///     Ok(models) => println!("Found {} models", models.data.len()),
24///     Err(Error::Http(e)) => println!("Network error: {}", e),
25///     Err(Error::Config(e)) => println!("Configuration error: {}", e),
26///     Err(e) => println!("Other error: {}", e),
27/// }
28/// # Ok(())
29/// # }
30/// ```
31#[non_exhaustive]
32#[derive(Debug, thiserror::Error)]
33pub enum Error {
34    /// HTTP transport error from the underlying HTTP client.
35    ///
36    /// This includes network connectivity issues, DNS resolution failures,
37    /// timeout errors, and other transport-layer problems.
38    #[error("HTTP error: {0}")]
39    Http(#[from] reqwest::Error),
40
41    /// JSON serialization/deserialization error.
42    ///
43    /// This occurs when the SDK fails to parse API responses or serialize
44    /// request payloads to/from JSON.
45    #[error("Serialization error: {0}")]
46    Serialization(#[from] serde_json::Error),
47
48    /// Configuration error.
49    ///
50    /// This occurs when configuration parameters are invalid or when using
51    /// the configuration builder and validation fails during the build process.
52    #[error("Configuration error: {0}")]
53    Config(#[from] PortkeyBuilderError),
54
55    /// URL parsing error.
56    ///
57    /// This occurs when a provided URL string is invalid or cannot be parsed.
58    #[error("URL parse error: {0}")]
59    UrlParse(#[from] url::ParseError),
60}
61
62/// Result type for Portkey API operations.
63///
64/// This is a convenience type alias for `std::result::Result<T, Error>` that is used
65/// throughout the Portkey SDK. All SDK methods that can fail return this Result type.
66pub type Result<T, E = Error> = std::result::Result<T, E>;