Skip to main content

bytes_radar/core/
error.rs

1use std::io;
2use thiserror::Error;
3
4#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
5pub struct DownloadUrlError {
6    pub url: String,
7    pub error_message: String,
8    pub error_type: String,
9    pub http_status_code: Option<u16>,
10    pub retry_count: u32,
11}
12
13#[derive(Error, Debug)]
14pub enum AnalysisError {
15    #[error("Failed to read file: {path}")]
16    FileReadError {
17        path: String,
18        #[source]
19        source: io::Error,
20    },
21
22    #[error("Unsupported file extension: {extension}")]
23    UnsupportedExtension { extension: String },
24
25    #[error("Language not found: {language}")]
26    LanguageNotFound { language: String },
27
28    #[error("Invalid file statistics: {reason}")]
29    InvalidStatistics { reason: String },
30
31    #[error("Directory traversal failed: {path}")]
32    DirectoryTraversalError {
33        path: String,
34        #[source]
35        source: io::Error,
36    },
37
38    #[error("Language detection failed for file: {file_path}")]
39    LanguageDetectionError { file_path: String },
40
41    #[error("Configuration error: {message}")]
42    ConfigurationError { message: String },
43
44    #[error("JSON serialization error")]
45    JsonSerializationError {
46        #[from]
47        source: serde_json::Error,
48    },
49
50    #[error("XML serialization error: {message}")]
51    XmlSerializationError { message: String },
52
53    #[error("Aggregation error: {operation}")]
54    AggregationError { operation: String },
55
56    #[error("Network error: {message}")]
57    NetworkError { message: String },
58
59    #[error("Archive processing error: {message}")]
60    ArchiveError { message: String },
61
62    #[error("URL parsing error: {url}")]
63    UrlParsingError { url: String },
64}
65
66pub type Result<T> = std::result::Result<T, AnalysisError>;
67
68impl AnalysisError {
69    pub fn file_read<P: AsRef<str>>(path: P, source: io::Error) -> Self {
70        Self::FileReadError {
71            path: path.as_ref().to_string(),
72            source,
73        }
74    }
75
76    pub fn unsupported_extension<E: AsRef<str>>(extension: E) -> Self {
77        Self::UnsupportedExtension {
78            extension: extension.as_ref().to_string(),
79        }
80    }
81
82    pub fn language_not_found<L: AsRef<str>>(language: L) -> Self {
83        Self::LanguageNotFound {
84            language: language.as_ref().to_string(),
85        }
86    }
87
88    pub fn invalid_statistics<R: AsRef<str>>(reason: R) -> Self {
89        Self::InvalidStatistics {
90            reason: reason.as_ref().to_string(),
91        }
92    }
93
94    pub fn directory_traversal<P: AsRef<str>>(path: P, source: io::Error) -> Self {
95        Self::DirectoryTraversalError {
96            path: path.as_ref().to_string(),
97            source,
98        }
99    }
100
101    pub fn language_detection<P: AsRef<str>>(file_path: P) -> Self {
102        Self::LanguageDetectionError {
103            file_path: file_path.as_ref().to_string(),
104        }
105    }
106
107    pub fn configuration<M: AsRef<str>>(message: M) -> Self {
108        Self::ConfigurationError {
109            message: message.as_ref().to_string(),
110        }
111    }
112
113    pub fn aggregation<O: AsRef<str>>(operation: O) -> Self {
114        Self::AggregationError {
115            operation: operation.as_ref().to_string(),
116        }
117    }
118
119    pub fn network<M: AsRef<str>>(message: M) -> Self {
120        Self::NetworkError {
121            message: message.as_ref().to_string(),
122        }
123    }
124
125    pub fn archive<M: AsRef<str>>(message: M) -> Self {
126        Self::ArchiveError {
127            message: message.as_ref().to_string(),
128        }
129    }
130
131    pub fn url_parsing<U: AsRef<str>>(url: U) -> Self {
132        Self::UrlParsingError {
133            url: url.as_ref().to_string(),
134        }
135    }
136
137    pub fn xml_serialization<M: AsRef<str>>(message: M) -> Self {
138        Self::XmlSerializationError {
139            message: message.as_ref().to_string(),
140        }
141    }
142}