Skip to main content

leptos_forms_rs/error/
handler.rs

1//! Error handler module - Error handling strategies and implementations
2//!
3//! This module provides the ErrorHandler trait and DefaultErrorHandler implementation,
4//! error handling strategies, logging and reporting decisions.
5
6use super::{ErrorContext, FormError};
7
8/// Error handler trait for custom error handling
9pub trait ErrorHandler {
10    /// Handle a form error
11    fn handle_error(&self, error: &FormError, context: &ErrorContext);
12
13    /// Check if an error should be logged
14    fn should_log_error(&self, error: &FormError) -> bool;
15
16    /// Check if an error should be reported to external services
17    fn should_report_error(&self, error: &FormError) -> bool;
18}
19
20/// Default error handler implementation
21pub struct DefaultErrorHandler;
22
23impl ErrorHandler for DefaultErrorHandler {
24    fn handle_error(&self, error: &FormError, context: &ErrorContext) {
25        log::error!("Form error: {} (context: {:?})", error, context);
26    }
27
28    fn should_log_error(&self, _error: &FormError) -> bool {
29        true
30    }
31
32    fn should_report_error(&self, error: &FormError) -> bool {
33        // Report all errors except field validation errors
34        !error.is_field_error()
35    }
36}
37
38/// Silent error handler that doesn't log or report errors
39pub struct SilentErrorHandler;
40
41impl ErrorHandler for SilentErrorHandler {
42    fn handle_error(&self, _error: &FormError, _context: &ErrorContext) {
43        // Do nothing - silent handler
44    }
45
46    fn should_log_error(&self, _error: &FormError) -> bool {
47        false
48    }
49
50    fn should_report_error(&self, _error: &FormError) -> bool {
51        false
52    }
53}
54
55/// Verbose error handler that logs and reports all errors
56pub struct VerboseErrorHandler;
57
58impl ErrorHandler for VerboseErrorHandler {
59    fn handle_error(&self, error: &FormError, context: &ErrorContext) {
60        log::error!("Form error: {} (context: {:?})", error, context);
61
62        // Also log to debug level for more details
63        log::debug!("Error details: {:?}", error);
64        log::debug!("Context details: {:?}", context);
65    }
66
67    fn should_log_error(&self, _error: &FormError) -> bool {
68        true
69    }
70
71    fn should_report_error(&self, _error: &FormError) -> bool {
72        true
73    }
74}
75
76/// Selective error handler that only handles certain types of errors
77pub struct SelectiveErrorHandler {
78    log_field_errors: bool,
79    log_validation_errors: bool,
80    log_submission_errors: bool,
81    log_state_errors: bool,
82    log_persistence_errors: bool,
83    log_configuration_errors: bool,
84    log_unknown_errors: bool,
85    report_critical_errors: bool,
86}
87
88impl SelectiveErrorHandler {
89    /// Create a new selective error handler
90    pub fn new() -> Self {
91        Self {
92            log_field_errors: false,
93            log_validation_errors: true,
94            log_submission_errors: true,
95            log_state_errors: true,
96            log_persistence_errors: true,
97            log_configuration_errors: true,
98            log_unknown_errors: true,
99            report_critical_errors: true,
100        }
101    }
102
103    /// Enable logging for field errors
104    pub fn with_field_error_logging(mut self) -> Self {
105        self.log_field_errors = true;
106        self
107    }
108
109    /// Disable logging for validation errors
110    pub fn without_validation_error_logging(mut self) -> Self {
111        self.log_validation_errors = false;
112        self
113    }
114
115    /// Disable reporting for critical errors
116    pub fn without_critical_error_reporting(mut self) -> Self {
117        self.report_critical_errors = false;
118        self
119    }
120}
121
122impl Default for SelectiveErrorHandler {
123    fn default() -> Self {
124        Self::new()
125    }
126}
127
128impl ErrorHandler for SelectiveErrorHandler {
129    fn handle_error(&self, error: &FormError, context: &ErrorContext) {
130        let should_log = match error {
131            FormError::FieldError { .. } => self.log_field_errors,
132            FormError::ValidationError { .. } => self.log_validation_errors,
133            FormError::SubmissionError { .. } => self.log_submission_errors,
134            FormError::StateError { .. } => self.log_state_errors,
135            FormError::PersistenceError { .. } => self.log_persistence_errors,
136            FormError::ConfigurationError { .. } => self.log_configuration_errors,
137            FormError::Unknown { .. } => self.log_unknown_errors,
138            FormError::SerializationError { .. } => true, // Always log serialization errors
139        };
140
141        if should_log {
142            log::error!("Form error: {} (context: {:?})", error, context);
143        }
144    }
145
146    fn should_log_error(&self, error: &FormError) -> bool {
147        match error {
148            FormError::FieldError { .. } => self.log_field_errors,
149            FormError::ValidationError { .. } => self.log_validation_errors,
150            FormError::SubmissionError { .. } => self.log_submission_errors,
151            FormError::StateError { .. } => self.log_state_errors,
152            FormError::PersistenceError { .. } => self.log_persistence_errors,
153            FormError::ConfigurationError { .. } => self.log_configuration_errors,
154            FormError::Unknown { .. } => self.log_unknown_errors,
155            FormError::SerializationError { .. } => true, // Always log serialization errors
156        }
157    }
158
159    fn should_report_error(&self, error: &FormError) -> bool {
160        if !self.report_critical_errors {
161            return false;
162        }
163
164        match error {
165            FormError::FieldError { .. } => false, // Don't report field errors
166            FormError::ValidationError { .. } => false, // Don't report validation errors
167            FormError::SubmissionError { .. } => true, // Report submission errors
168            FormError::StateError { .. } => true,  // Report state errors
169            FormError::PersistenceError { .. } => true, // Report persistence errors
170            FormError::ConfigurationError { .. } => true, // Report configuration errors
171            FormError::Unknown { .. } => true,     // Report unknown errors
172            FormError::SerializationError { .. } => true, // Report serialization errors
173        }
174    }
175}