1use dtt::error::DateTimeError;
11use std::string::FromUtf8Error;
12use thiserror::Error;
13
14#[derive(Error, Debug)]
23#[non_exhaustive]
24pub enum SitemapError {
25 #[error("XML writing error: {0}")]
27 XmlWriteError(#[from] oxml::tree::NodeError),
28
29 #[error("XML parsing error: {0}")]
31 XmlParseError(#[from] oxml::Error),
32
33 #[error("Date error: {0}")]
35 DateError(#[from] DateTimeError),
36
37 #[error("URL error: {0}")]
39 UrlError(#[from] url::ParseError),
40
41 #[error("I/O error: {0}")]
43 IoError(#[from] std::io::Error),
44
45 #[error("Encoding error: {0}")]
47 EncodingError(#[from] FromUtf8Error),
48
49 #[error("Invalid change frequency: {0}")]
51 InvalidChangeFreq(String),
52
53 #[error("Custom error: {0}")]
55 CustomError(String),
56
57 #[error("Sitemap size exceeds the maximum allowed (10MB)")]
59 SitemapTooLarge,
60
61 #[error("Number of URLs ({0}) exceeds the maximum allowed limit (50,000)")]
63 MaxUrlLimitExceeded(usize),
64}
65
66impl SitemapError {
67 pub fn context(&self) -> &'static str {
76 match self {
77 SitemapError::XmlWriteError(_) => "Error occurred while writing XML data",
78 SitemapError::XmlParseError(_) => "Error occurred while parsing XML data",
79 SitemapError::DateError(_) => "Error occurred while parsing or formatting dates",
80 SitemapError::UrlError(_) => "Error occurred while parsing URLs",
81 SitemapError::IoError(_) => "Error occurred during file or network operations",
82 SitemapError::EncodingError(_) => "Error occurred during UTF-8 string encoding or decoding",
83 SitemapError::InvalidChangeFreq(_) => "An invalid change frequency value was provided",
84 SitemapError::CustomError(_) => "An unexpected error occurred",
85 SitemapError::SitemapTooLarge => "The generated sitemap exceeds the maximum allowed size",
86 SitemapError::MaxUrlLimitExceeded(_) => "The number of URLs exceeds the maximum allowed limit",
87 }
88 }
89}
90
91pub type SitemapResult<T> = Result<T, SitemapError>;
96
97#[cfg(test)]
98mod tests {
99 use super::*;
100 use std::io;
101
102 #[test]
103 fn test_error_creation_and_formatting() {
104 let mut doc = oxml::Document::empty();
106 let root = doc.root();
107 let _ = doc.append_element(root, None, "first").unwrap();
108 let xml_write_error =
109 doc.append_element(root, None, "second").unwrap_err();
110 let sitemap_error =
111 SitemapError::XmlWriteError(xml_write_error);
112 assert!(sitemap_error
113 .to_string()
114 .contains("XML writing error"));
115 assert_eq!(
116 sitemap_error.context(),
117 "Error occurred while writing XML data"
118 );
119
120 let custom_error =
121 SitemapError::CustomError("Test error".to_string());
122 assert_eq!(
123 custom_error.to_string(),
124 "Custom error: Test error"
125 );
126 }
127
128 #[test]
129 fn test_error_context() {
130 let url_error =
131 SitemapError::UrlError(url::ParseError::EmptyHost);
132 assert_eq!(
133 url_error.context(),
134 "Error occurred while parsing URLs"
135 );
136
137 let io_error =
138 SitemapError::IoError(io::Error::other("I/O Error"));
139 assert_eq!(
140 io_error.context(),
141 "Error occurred during file or network operations"
142 );
143
144 let invalid_change_freq =
145 SitemapError::InvalidChangeFreq("invalid".to_string());
146 assert_eq!(
147 invalid_change_freq.context(),
148 "An invalid change frequency value was provided"
149 );
150 }
151
152 #[test]
153 fn test_error_display() {
154 let date_error =
155 SitemapError::DateError(DateTimeError::InvalidFormat);
156 assert_eq!(
157 date_error.to_string(),
158 "Date error: Invalid date format"
159 );
160
161 let url_error =
162 SitemapError::UrlError(url::ParseError::EmptyHost);
163 assert_eq!(url_error.to_string(), "URL error: empty host");
164
165 let io_error =
166 SitemapError::IoError(io::Error::other("I/O Error"));
167 assert_eq!(io_error.to_string(), "I/O error: I/O Error");
168
169 let custom_error = SitemapError::CustomError(
170 "Custom error message".to_string(),
171 );
172 assert_eq!(
173 custom_error.to_string(),
174 "Custom error: Custom error message"
175 );
176
177 let sitemap_too_large = SitemapError::SitemapTooLarge;
178 assert_eq!(
179 sitemap_too_large.to_string(),
180 "Sitemap size exceeds the maximum allowed (10MB)"
181 );
182
183 let max_url_limit_exceeded =
184 SitemapError::MaxUrlLimitExceeded(60000);
185 assert_eq!(
186 max_url_limit_exceeded.to_string(),
187 "Number of URLs (60000) exceeds the maximum allowed limit (50,000)"
188 );
189 }
190
191 #[test]
192 fn test_result_type_alias() {
193 fn demo_function() -> SitemapResult<()> {
194 Err(SitemapError::CustomError("Test error".to_string()))
195 }
196
197 let result = demo_function();
198 assert!(result.is_err());
199 assert!(matches!(
200 result.unwrap_err(),
201 SitemapError::CustomError(_)
202 ));
203 }
204
205 #[test]
206 fn test_xml_parse_error() {
207 let xml = "<invalid>";
208 let xml_parse_error = oxml::parse(xml).unwrap_err();
209 let sitemap_error =
210 SitemapError::XmlParseError(xml_parse_error);
211 assert!(sitemap_error
212 .to_string()
213 .contains("XML parsing error"));
214 assert_eq!(
215 sitemap_error.context(),
216 "Error occurred while parsing XML data"
217 );
218 }
219
220 #[test]
221 fn test_date_error() {
222 let date_error =
223 SitemapError::DateError(DateTimeError::InvalidFormat);
224 assert_eq!(
225 date_error.context(),
226 "Error occurred while parsing or formatting dates"
227 );
228 }
229
230 #[test]
231 fn test_encoding_error() {
232 let invalid_utf8 = vec![0xFF, 0xFE, 0xFD];
233 let encoding_error =
234 String::from_utf8(invalid_utf8).unwrap_err();
235 let sitemap_error = SitemapError::EncodingError(encoding_error);
236 assert!(sitemap_error.to_string().contains("Encoding error"));
237 assert_eq!(
238 sitemap_error.context(),
239 "Error occurred during UTF-8 string encoding or decoding"
240 );
241 }
242
243 #[test]
244 fn test_sitemap_size_errors() {
245 let sitemap_too_large = SitemapError::SitemapTooLarge;
246 assert_eq!(
247 sitemap_too_large.to_string(),
248 "Sitemap size exceeds the maximum allowed (10MB)"
249 );
250 assert_eq!(
251 sitemap_too_large.context(),
252 "The generated sitemap exceeds the maximum allowed size"
253 );
254
255 let max_url_limit_exceeded =
256 SitemapError::MaxUrlLimitExceeded(60000);
257 assert_eq!(
258 max_url_limit_exceeded.to_string(),
259 "Number of URLs (60000) exceeds the maximum allowed limit (50,000)"
260 );
261 assert_eq!(
262 max_url_limit_exceeded.context(),
263 "The number of URLs exceeds the maximum allowed limit"
264 );
265 }
266
267 #[test]
268 fn test_error_propagation() {
269 fn parse_url() -> SitemapResult<()> {
270 Err(SitemapError::UrlError(url::ParseError::EmptyHost))
271 }
272
273 fn handle_url() -> SitemapResult<()> {
274 parse_url()?;
275 Ok(())
276 }
277
278 let result = handle_url();
279 assert!(result.is_err());
280 assert!(matches!(
281 result.unwrap_err(),
282 SitemapError::UrlError(_)
283 ));
284 }
285
286 #[test]
287 fn test_url_parsing_errors() {
288 let empty_host =
289 SitemapError::UrlError(url::ParseError::EmptyHost);
290 assert_eq!(empty_host.to_string(), "URL error: empty host");
291
292 let invalid_port =
293 SitemapError::UrlError(url::ParseError::InvalidPort);
294 assert_eq!(
295 invalid_port.to_string(),
296 "URL error: invalid port number"
297 ); let relative_url = SitemapError::UrlError(
300 url::ParseError::RelativeUrlWithoutBase,
301 );
302 assert_eq!(
303 relative_url.to_string(),
304 "URL error: relative URL without a base"
305 );
306 }
307
308 #[test]
309 fn test_invalid_change_freq_edge_cases() {
310 let empty_string =
311 SitemapError::InvalidChangeFreq("".to_string());
312 assert_eq!(
313 empty_string.to_string(),
314 "Invalid change frequency: "
315 );
316
317 let long_string =
318 SitemapError::InvalidChangeFreq("a".repeat(1000));
319 assert!(long_string
320 .to_string()
321 .contains("Invalid change frequency"));
322 }
323
324 #[test]
325 fn test_max_url_limit_exceeded_edge_cases() {
326 let just_under_limit = SitemapError::MaxUrlLimitExceeded(49999);
327 assert_eq!(just_under_limit.to_string(), "Number of URLs (49999) exceeds the maximum allowed limit (50,000)");
328
329 let at_limit = SitemapError::MaxUrlLimitExceeded(50000);
330 assert_eq!(at_limit.to_string(), "Number of URLs (50000) exceeds the maximum allowed limit (50,000)");
331
332 let over_limit = SitemapError::MaxUrlLimitExceeded(50001);
333 assert_eq!(over_limit.to_string(), "Number of URLs (50001) exceeds the maximum allowed limit (50,000)");
334 }
335}