sw_errors/errors/
raw.rs

1use crate::errors::errors_type::ErrorsType;
2
3/// A struct serving as a wrapper for `ErrorRaw`. Provides a convenient way
4/// to handle raw error data.
5pub struct Raw {
6    /// The raw error data stored in this struct instance.
7    pub data: ErrorRaw,
8}
9
10/// Handles error triggers, controlling whether an error should
11/// cause a panic or just be displayed.
12
13pub struct ErrorTrigger;
14
15/// Represents raw error information, defining all necessary details to describe an error.
16pub struct ErrorRaw {
17    /// The type of the error (e.g., Error, Warning, Debug, etc.).
18    pub error_type: ErrorsType,
19    /// The main error message.
20    pub message: String,
21    /// A detailed description of the error (optional).
22    pub description: Option<String>,
23    /// An optional error code associated with this specific error.
24    pub code: Option<u64>,
25    /// The line in the file where the error occurred (if applicable).
26    pub line: Option<u64>,
27    /// The column in the file where the error occurred (if applicable).
28    pub column: Option<u64>,
29    /// The name of the file where the error occurred (optional).
30    pub file: Option<String>,
31    /// A hint or possible solution for the error (optional).
32    pub hint: Option<String>,
33    /// Whether the error should trigger a panic.
34    pub should_panic: bool,
35}
36
37impl Raw {
38    /// Creates a new instance of `Raw`, wrapping an instance of `ErrorRaw`.
39    ///
40    /// # Arguments
41    /// * `data` - An instance of `ErrorRaw` containing error information.
42    ///
43    /// # Returns
44    /// A new `Raw` struct containing the provided `ErrorRaw` data.
45    ///
46    pub fn new(data: ErrorRaw) -> Self {
47        Self { data }
48    }
49}
50
51impl ErrorTrigger {
52    /// Processes an error and takes appropriate action based on its severity.
53    /// If `should_panic` is `true`, the function will trigger a panic.
54    /// Otherwise, the error details will simply be logged or displayed.
55    ///
56    /// # Arguments
57    /// * `error` - An instance of `ErrorRaw` containing full information about the error.
58    ///
59    /// # Panics
60    /// This function will panic if `error.should_panic == true`.
61    pub fn throw_error(error: ErrorRaw) {
62        let mut line_column_info = String::new();
63
64        // Se `line` e `column` existirem, adiciona ao formato
65        if let Some(line) = error.line {
66            line_column_info.push_str(&format!("Line: {}", line));
67        }
68        if let Some(column) = error.column {
69            if !line_column_info.is_empty() {
70                line_column_info.push_str(", ");
71            }
72            line_column_info.push_str(&format!("Column: {}", column));
73        }
74
75        // Monta a mensagem
76        let message = if let Some(hint) = &error.hint {
77            format!(
78                "{}! [{:?}]: {}.\n\n {}\n\n Hint: {}",
79                get_error_type(error.error_type),
80                error.code,
81                error.message,
82                line_column_info,
83                hint,
84            )
85        } else {
86            format!(
87                "{}! [{:?}]: {}.\n\n {}",
88                get_error_type(error.error_type),
89                error.code,
90                error.message,
91                line_column_info,
92            )
93        };
94
95        // Dependendo de should_panic, decide lançar panic ou apenas mostrar a mensagem
96        if error.should_panic {
97            panic!("{}", message);
98        } else {
99            println!("{}", message); // Ou log algo no lugar de println
100        }
101    }
102}
103
104/// Maps the error type to a string representation.
105///
106/// # Arguments
107/// * `err` - The type of the error from `ErrorsType` enum.
108///
109/// # Returns
110/// A string representing the error type.
111
112fn get_error_type(err: ErrorsType) -> &'static str {
113    match err {
114        ErrorsType::Error => "Error",
115        ErrorsType::Info => "Info",
116        ErrorsType::Warn => "Warn",
117        ErrorsType::Note => "Note",
118        ErrorsType::Debug => "Debug",
119    }
120}
121
122#[cfg(test)]
123mod tests {
124    use crate::errors::errors_type::ErrorsType;
125    use crate::errors::raw::{ErrorRaw, ErrorTrigger, get_error_type};
126
127    #[test]
128    fn test_get_error_type() {
129        assert_eq!(get_error_type(ErrorsType::Error), "Error");
130        assert_eq!(get_error_type(ErrorsType::Warn), "Warn");
131        assert_eq!(get_error_type(ErrorsType::Note), "Note");
132        assert_eq!(get_error_type(ErrorsType::Debug), "Debug");
133        assert_eq!(get_error_type(ErrorsType::Info), "Info");
134    }
135
136    #[test]
137    fn test_throw_error_with_panic() {
138        let error = ErrorRaw {
139            error_type: ErrorsType::Error,
140            message: "Critical Error!".to_string(),
141            description: Some("A major issue occurred".to_string()),
142            code: Some(500),
143            line: Some(42),
144            column: Some(24),
145            file: None,
146            hint: None,
147            should_panic: true, // Este erro deve causar panic
148        };
149
150        // Usa catch_unwind para verificar se houve um panic
151        let result = std::panic::catch_unwind(|| {
152            ErrorTrigger::throw_error(error);
153        });
154
155        // Verifica se houve panic
156        assert!(result.is_err(), "Expected error, but no panic.");
157    }
158
159    #[test]
160    fn test_throw_error_without_panic() {
161        let error = ErrorRaw {
162            error_type: ErrorsType::Warn,
163            message: "This is just a warning!".to_string(),
164            description: None,
165            code: None,
166            line: Some(99),
167            column: None,
168            file: None,
169            hint: Some("This can be ignored.".to_string()),
170            should_panic: false, // Este erro não deve causar panic
171        };
172
173        // Captura a saída padrão (opcional: para verificar mensagens formatadas)
174        let result = std::panic::catch_unwind(|| {
175            ErrorTrigger::throw_error(error);
176        });
177
178        // Verifica se NÃO houve panic
179        assert!(result.is_ok(), "No panic expected, but it happened.");
180    }
181}