Files
zclaw_openfang/desktop/src-tauri/src/browser/error.rs
2026-03-17 23:26:16 +08:00

90 lines
2.6 KiB
Rust

// Browser automation error types
// Note: Some variants are reserved for future error handling scenarios
#![allow(dead_code)]
use serde::Serialize;
use thiserror::Error;
#[derive(Debug, Error, Serialize)]
pub enum BrowserError {
#[error("WebDriver connection failed: {0}")]
ConnectionFailed(String),
#[error("Session not found: {0}")]
SessionNotFound(String),
#[error("Element not found: {selector}")]
ElementNotFound { selector: String },
#[error("Navigation failed: {url}")]
NavigationFailed { url: String },
#[error("Timeout waiting for element: {selector}")]
Timeout { selector: String },
#[error("Invalid selector: {selector}")]
InvalidSelector { selector: String },
#[error("JavaScript execution failed: {message}")]
ScriptError { message: String },
#[error("Screenshot failed: {reason}")]
ScreenshotFailed { reason: String },
#[error("Form interaction failed: {field}")]
FormError { field: String },
#[error("WebDriver not available: {reason}")]
DriverNotAvailable { reason: String },
#[error("Session already exists: {id}")]
SessionExists { id: String },
#[error("Operation cancelled by user")]
Cancelled,
#[error("Configuration error: {0}")]
ConfigError(String),
#[error("IO error: {0}")]
IoError(String),
#[error("WebDriver command failed: {0}")]
CommandFailed(String),
#[error("Unknown error: {0}")]
Unknown(String),
}
// Manual conversion from fantoccini errors since the enum variants differ between versions
impl From<fantoccini::error::NewSessionError> for BrowserError {
fn from(e: fantoccini::error::NewSessionError) -> Self {
BrowserError::ConnectionFailed(e.to_string())
}
}
impl From<fantoccini::error::CmdError> for BrowserError {
fn from(e: fantoccini::error::CmdError) -> Self {
// Convert to string and wrap in appropriate error type
let msg = e.to_string();
if msg.contains("not found") || msg.contains("no such element") {
BrowserError::ElementNotFound { selector: msg }
} else if msg.contains("timeout") || msg.contains("timed out") {
BrowserError::Timeout { selector: msg }
} else if msg.contains("script") || msg.contains("javascript") {
BrowserError::ScriptError { message: msg }
} else {
BrowserError::CommandFailed(msg)
}
}
}
impl From<std::io::Error> for BrowserError {
fn from(e: std::io::Error) -> Self {
BrowserError::IoError(e.to_string())
}
}
pub type Result<T> = std::result::Result<T, BrowserError>;