leptos_forms_rs/error/
handler.rs1use super::{ErrorContext, FormError};
7
8pub trait ErrorHandler {
10 fn handle_error(&self, error: &FormError, context: &ErrorContext);
12
13 fn should_log_error(&self, error: &FormError) -> bool;
15
16 fn should_report_error(&self, error: &FormError) -> bool;
18}
19
20pub 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 !error.is_field_error()
35 }
36}
37
38pub struct SilentErrorHandler;
40
41impl ErrorHandler for SilentErrorHandler {
42 fn handle_error(&self, _error: &FormError, _context: &ErrorContext) {
43 }
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
55pub 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 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
76pub 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 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 pub fn with_field_error_logging(mut self) -> Self {
105 self.log_field_errors = true;
106 self
107 }
108
109 pub fn without_validation_error_logging(mut self) -> Self {
111 self.log_validation_errors = false;
112 self
113 }
114
115 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, };
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, }
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, FormError::ValidationError { .. } => false, FormError::SubmissionError { .. } => true, FormError::StateError { .. } => true, FormError::PersistenceError { .. } => true, FormError::ConfigurationError { .. } => true, FormError::Unknown { .. } => true, FormError::SerializationError { .. } => true, }
174 }
175}