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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// use std::collections::HashMap;
// use std::error::Error;
// use std::fmt;
//
// #[derive(Debug)]
// pub struct OpenAIError;
//
// impl fmt::Display for OpenAIError {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// write!(f, "OpenAIError")
// }
// }
//
// impl Error for OpenAIError {}
//
// #[derive(Debug)]
// pub struct APIError {
// status: Option<u16>,
// headers: Option<HashMap<String, String>>,
// error: Option<HashMap<String, String>>,
// code: Option<String>,
// param: Option<String>,
// error_type: Option<String>,
// request_id: Option<String>,
// }
//
// impl APIError {
// fn new(
// status: Option<u16>,
// error: Option<HashMap<String, String>>,
// message: Option<String>,
// headers: Option<HashMap<String, String>>,
// ) -> Self {
// let request_id = headers.as_ref().and_then(|h| h.get("x-request-id").cloned());
// let error_map = error.as_ref().map(|e| e.clone());
// let code = error_map.as_ref().and_then(|e| e.get("code").cloned());
// let param = error_map.as_ref().and_then(|e| e.get("param").cloned());
// let error_type = error_map.as_ref().and_then(|e| e.get("type").cloned());
//
// APIError {
// status,
// headers,
// error: error_map,
// code,
// param,
// error_type,
// request_id,
// }
// }
//
// fn make_message(status: Option<u16>, error: Option<&HashMap<String, String>>, message: Option<&String>) -> String {
// let msg = error
// .and_then(|e| e.get("message"))
// .map(|m| m.clone())
// .unwrap_or_else(|| message.cloned().unwrap_or_else(|| "".to_string()));
//
// match (status, msg.as_str()) {
// (Some(status), msg) if !msg.is_empty() => format!("{} {}", status, msg),
// (Some(status), _) => format!("{} status code (no body)", status),
// (_, msg) if !msg.is_empty() => msg.to_string(),
// _ => "(no status code or body)".to_string(),
// }
// }
//
// fn generate(
// status: Option<u16>,
// error_response: Option<HashMap<String, String>>,
// message: Option<String>,
// headers: Option<HashMap<String, String>>,
// ) -> Box<dyn Error> {
// if status.is_none() {
// return Box::new(APIConnectionError::new(cast_to_error(error_response)));
// }
//
// let error = error_response.as_ref().and_then(|e| e.get("error")).cloned();
//
// match status {
// Some(400) => Box::new(BadRequestError::new(status, error, message, headers)),
// Some(401) => Box::new(AuthenticationError::new(status, error, message, headers)),
// Some(403) => Box::new(PermissionDeniedError::new(status, error, message, headers)),
// Some(404) => Box::new(NotFoundError::new(status, error, message, headers)),
// Some(409) => Box::new(ConflictError::new(status, error, message, headers)),
// Some(422) => Box::new(UnprocessableEntityError::new(status, error, message, headers)),
// Some(429) => Box::new(RateLimitError::new(status, error, message, headers)),
// Some(status) if status >= 500 => Box::new(InternalServerError::new(status, error, message, headers)),
// _ => Box::new(APIError::new(status, error_response, message, headers)),
// }
// }
// }
//
// #[derive(Debug)]
// pub struct APIUserAbortError {
// status: Option<u16>,
// message: Option<String>,
// }
//
// impl APIUserAbortError {
// fn new(message: Option<String>) -> Self {
// APIUserAbortError {
// status: None,
// message: Some(message.unwrap_or_else(|| "Request was aborted.".to_string())),
// }
// }
// }
//
// impl fmt::Display for APIUserAbortError {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// write!(f, "APIUserAbortError: {:?}", self.message)
// }
// }
//
// impl Error for APIUserAbortError {}
//
// #[derive(Debug)]
// pub struct APIConnectionError {
// status: Option<u16>,
// message: Option<String>,
// cause: Option<Box<dyn Error>>,
// }
//
// impl APIConnectionError {
// fn new(cause: Option<Box<dyn Error>>) -> Self {
// APIConnectionError {
// status: None,
// message: Some("Connection error.".to_string()),
// cause,
// }
// }
// }
//
// impl fmt::Display for APIConnectionError {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// write!(f, "APIConnectionError: {:?}", self.message)
// }
// }
//
// impl Error for APIConnectionError {}
//
// #[derive(Debug)]
// pub struct APIConnectionTimeoutError {
// message: Option<String>,
// }
//
// impl APIConnectionTimeoutError {
// fn new(message: Option<String>) -> Self {
// APIConnectionTimeoutError {
// message: Some(message.unwrap_or_else(|| "Request timed out.".to_string())),
// }
// }
// }
//
// impl fmt::Display for APIConnectionTimeoutError {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// write!(f, "APIConnectionTimeoutError: {:?}", self.message)
// }
// }
//
// impl Error for APIConnectionTimeoutError {}
//
// macro_rules! define_error {
// ($name:ident, $status:expr) => {
// #[derive(Debug)]
// pub struct $name {
// status: u16,
// error: Option<HashMap<String, String>>,
// message: Option<String>,
// headers: Option<HashMap<String, String>>,
// }
//
// impl $name {
// fn new(
// status: Option<u16>,
// error: Option<String>,
// message: Option<String>,
// headers: Option<HashMap<String, String>>,
// ) -> Self {
// $name {
// status: $status,
// error: error.map(|e| [("error".to_string(), e)].iter().cloned().collect()),
// message,
// headers,
// }
// }
// }
//
// impl fmt::Display for $name {
// fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// write!(f, "{}: {:?}", stringify!($name), self.message)
// }
// }
//
// impl Error for $name {}
// };
// }
//
// define_error!(BadRequestError, 400);
// define_error!(AuthenticationError, 401);
// define_error!(PermissionDeniedError, 403);
// define_error!(NotFoundError, 404);
// define_error!(ConflictError, 409);
// define_error!(UnprocessableEntityError, 422);
// define_error!(RateLimitError, 429);
// define_error!(InternalServerError, 500);
//
// fn cast_to_error(_error_response: Option<HashMap<String, String>>) -> Option<Box<dyn Error>> {
// // Implement the logic to cast error_response to an error here.
// None
// }