use std::collections::{BTreeMap, HashMap};
use std::sync::{Arc, Mutex};
use anyhow::{Result, Error};
use std::time::Instant;
#[derive(Debug, Clone, Default)]
pub struct ErrorStats {
pub total_errors: usize,
pub compilation_errors: usize,
pub runtime_errors: usize,
pub last_error_time: Option<Instant>,
pub avg_error_rate: f64,
}
pub struct ErrorHandler {
stats: Arc<Mutex<ErrorStats>>,
verbose: bool,
}
impl ErrorHandler {
pub fn new(verbose: bool) -> Self {
Self {
stats: Arc::new(Mutex::new(ErrorStats::default()))
verbose,
}
}
pub fn handle_compilation_error(&self, error_msg: &str) -> anyhow::Error {
let mut stats = self.stats.lock().unwrap();
stats.compilation_errors += 1;
stats.total_errors += 1;
stats.last_error_time = Some(Instant::now());
if self.verbose {
println!("⚠️ Compilation error: {}", error_msg);
}
anyhow::anyhow!("JavaScript compilation error: {}", error_msg)
}
pub fn safe_cleanup(&self) {
if self.verbose {
println!("✅ V8 Isolate state cleaned up safely");
}
}
pub fn get_stats(&self) -> ErrorStats {
let stats: _ = self.stats.lock().unwrap();
stats.clone()
}
pub fn reset_stats(&self) {
let mut stats = self.stats.lock().unwrap();
*stats = ErrorStats::default();
}
pub fn is_error_rate_high(&self, threshold: f64) -> bool {
let stats: _ = self.stats.lock().unwrap();
stats.avg_error_rate > threshold
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_error_handler_creation() {
let handler: _ = ErrorHandler::new(true);
let stats: _ = handler.get_stats();
assert_eq!(stats.total_errors, 0);
assert_eq!(stats.compilation_errors, 0);
assert_eq!(stats.runtime_errors, 0);
}
#[test]
fn test_error_stats_reset() {
let handler: _ = ErrorHandler::new(false);
let mut stats = handler.stats.lock().unwrap();
stats.total_errors = 5;
stats.compilation_errors = 2;
stats.runtime_errors = 3;
drop(stats);
handler.reset_stats();
let new_stats: _ = handler.get_stats();
assert_eq!(new_stats.total_errors, 0);
assert_eq!(new_stats.compilation_errors, 0);
assert_eq!(new_stats.runtime_errors, 0);
}
}