1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/// Error types for the Cencori SDK.
use thiserror::Error;
/// Base error type for all Cencori SDK errors.
#[derive(Error, Debug)]
pub enum CencoriError {
/// Raised when the API key is invalid or missing.
#[error("Invalid API key")]
Authentication,
/// Raised when rate limit is exceeded.
#[error("Rate limit exceeded")]
RateLimit,
/// Raised when content violates safety policies.
#[error("Content safety violation: {reasons:?}")]
Safety {
/// List of reasons why content was blocked.
reasons: Vec<String>,
},
/// Raised when account has insufficient credits.
#[error("Insufficient credits")]
InsufficientCredits,
/// Raised when the upstream provider returns an error.
#[error("Provider error: {0}")]
Provider(String),
/// Generic API error with status code.
#[error("{message} (status: {status_code})")]
Api {
/// Error message.
message: String,
/// HTTP status code.
status_code: u16,
},
/// Configuration error (e.g., missing API key).
#[error("{0}")]
Config(String),
/// HTTP transport error from reqwest.
#[error("HTTP request failed: {0}")]
Http(#[from] reqwest::Error),
/// JSON parsing error.
#[error("JSON parse error: {0}")]
Json(#[from] serde_json::Error),
/// Stream-related error.
#[error("Stream error: {0}")]
Stream(String),
/// Feature not yet implemented.
#[error("Coming soon: {0}")]
NotImplemented(String),
/// Retry limit exceeded (all retries exhausted on 5xx errors).
#[error("Request failed after {retries} retries: {message}")]
RetryExhausted {
/// Number of retries attempted.
retries: u32,
/// The last error message.
message: String,
},
}
impl CencoriError {
/// Create a Safety error with reasons.
pub fn safety(reasons: Vec<String>) -> Self {
Self::Safety { reasons }
}
/// Create an API error.
pub fn api(status_code: u16, message: impl Into<String>) -> Self {
Self::Api {
message: message.into(),
status_code,
}
}
/// Create a config error.
pub fn config(message: impl Into<String>) -> Self {
Self::Config(message.into())
}
}