1use std::fmt;
4
5#[derive(Debug)]
7pub enum SDKError {
8 Authentication(AuthenticationError),
10 Api(ApiError),
12 Network(NetworkError),
14 Streaming(StreamingError),
16 Tool(ToolError),
18 Validation(ValidationError),
20}
21
22impl fmt::Display for SDKError {
23 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24 match self {
25 Self::Authentication(e) => write!(f, "Authentication error: {e}"),
26 Self::Api(e) => write!(f, "API error: {e}"),
27 Self::Network(e) => write!(f, "Network error: {e}"),
28 Self::Streaming(e) => write!(f, "Streaming error: {e}"),
29 Self::Tool(e) => write!(f, "Tool error: {e}"),
30 Self::Validation(e) => write!(f, "Validation error: {e}"),
31 }
32 }
33}
34
35impl std::error::Error for SDKError {
36 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
37 match self {
38 Self::Authentication(e) => Some(e),
39 Self::Api(e) => Some(e),
40 Self::Network(e) => Some(e),
41 Self::Streaming(e) => Some(e),
42 Self::Tool(e) => Some(e),
43 Self::Validation(e) => Some(e),
44 }
45 }
46}
47
48macro_rules! impl_from_for_sdk_error {
49 ($variant:ident, $err_type:ty) => {
50 impl From<$err_type> for SDKError {
51 fn from(err: $err_type) -> Self {
52 Self::$variant(err)
53 }
54 }
55 };
56}
57
58impl_from_for_sdk_error!(Authentication, AuthenticationError);
59impl_from_for_sdk_error!(Api, ApiError);
60impl_from_for_sdk_error!(Network, NetworkError);
61impl_from_for_sdk_error!(Streaming, StreamingError);
62impl_from_for_sdk_error!(Tool, ToolError);
63impl_from_for_sdk_error!(Validation, ValidationError);
64
65#[derive(Debug)]
67pub struct AuthenticationError {
68 pub message: String,
70}
71
72impl AuthenticationError {
73 #[must_use]
75 pub fn new(message: impl Into<String>) -> Self {
76 Self {
77 message: message.into(),
78 }
79 }
80}
81
82impl fmt::Display for AuthenticationError {
83 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
84 write!(f, "{}", self.message)
85 }
86}
87
88impl std::error::Error for AuthenticationError {}
89
90#[derive(Debug)]
92pub struct ApiError {
93 pub message: String,
95 pub status_code: Option<u16>,
97}
98
99impl ApiError {
100 #[must_use]
102 pub fn new(message: impl Into<String>) -> Self {
103 Self {
104 message: message.into(),
105 status_code: None,
106 }
107 }
108
109 #[must_use]
111 pub fn with_status(message: impl Into<String>, status_code: u16) -> Self {
112 Self {
113 message: message.into(),
114 status_code: Some(status_code),
115 }
116 }
117}
118
119impl fmt::Display for ApiError {
120 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121 if let Some(code) = self.status_code {
122 write!(f, "[HTTP {}] {}", code, self.message)
123 } else {
124 write!(f, "{}", self.message)
125 }
126 }
127}
128
129impl std::error::Error for ApiError {}
130
131#[derive(Debug)]
133pub struct NetworkError {
134 pub message: String,
136}
137
138impl NetworkError {
139 #[must_use]
141 pub fn new(message: impl Into<String>) -> Self {
142 Self {
143 message: message.into(),
144 }
145 }
146}
147
148impl fmt::Display for NetworkError {
149 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
150 write!(f, "{}", self.message)
151 }
152}
153
154impl std::error::Error for NetworkError {}
155
156#[derive(Debug)]
158pub struct StreamingError {
159 pub message: String,
161}
162
163impl StreamingError {
164 #[must_use]
166 pub fn new(message: impl Into<String>) -> Self {
167 Self {
168 message: message.into(),
169 }
170 }
171}
172
173impl fmt::Display for StreamingError {
174 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
175 write!(f, "{}", self.message)
176 }
177}
178
179impl std::error::Error for StreamingError {}
180
181#[derive(Debug)]
183pub struct ToolError {
184 pub message: String,
186}
187
188impl ToolError {
189 #[must_use]
191 pub fn new(message: impl Into<String>) -> Self {
192 Self {
193 message: message.into(),
194 }
195 }
196}
197
198impl fmt::Display for ToolError {
199 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
200 write!(f, "{}", self.message)
201 }
202}
203
204impl std::error::Error for ToolError {}
205
206#[derive(Debug)]
208pub struct ValidationError {
209 pub message: String,
211}
212
213impl ValidationError {
214 #[must_use]
216 pub fn new(message: impl Into<String>) -> Self {
217 Self {
218 message: message.into(),
219 }
220 }
221}
222
223impl fmt::Display for ValidationError {
224 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
225 write!(f, "{}", self.message)
226 }
227}
228
229impl std::error::Error for ValidationError {}
230
231#[cfg(test)]
232mod tests;