use thiserror::Error;
pub type Result<T> = std::result::Result<T, ChatError>;
#[derive(Debug, Error)]
pub enum ChatError {
#[error("RAG retrieval failed: {0}")]
RagRetrievalError(String),
#[error("LLM generation failed: {0}")]
LlmGenerationError(String),
#[error("SPARQL generation failed: {0}")]
SparqlGenerationError(String),
#[error("SPARQL execution failed: {0}")]
SparqlExecutionError(String),
#[error("Session not found: {0}")]
SessionNotFound(String),
#[error("Session error: {0}")]
SessionError(String),
#[error("Configuration error: {0}")]
ConfigError(String),
#[error("Storage error: {0}")]
StorageError(String),
#[error("Network error: {0}")]
NetworkError(String),
#[error("Operation timed out: {0}")]
TimeoutError(String),
#[error("Validation error: {0}")]
ValidationError(String),
#[error("Serialization error: {0}")]
SerializationError(String),
#[error("Deserialization error: {0}")]
DeserializationError(String),
#[error("Internal error: {0}")]
InternalError(String),
#[error("Quantum processing error: {0}")]
QuantumProcessingError(String),
#[error("Consciousness processing error: {0}")]
ConsciousnessProcessingError(String),
#[error("Reasoning error: {0}")]
ReasoningError(String),
#[error("Schema introspection error: {0}")]
SchemaIntrospectionError(String),
#[error(transparent)]
Other(#[from] anyhow::Error),
#[error(transparent)]
JsonError(#[from] serde_json::Error),
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error("OxiRS core error: {0}")]
CoreError(String),
}
impl From<String> for ChatError {
fn from(err: String) -> Self {
ChatError::CoreError(err)
}
}
impl ChatError {
pub fn is_recoverable(&self) -> bool {
matches!(
self,
ChatError::NetworkError(_)
| ChatError::TimeoutError(_)
| ChatError::LlmGenerationError(_)
)
}
pub fn category(&self) -> ErrorCategory {
match self {
ChatError::RagRetrievalError(_) => ErrorCategory::Retrieval,
ChatError::LlmGenerationError(_) => ErrorCategory::Generation,
ChatError::SparqlGenerationError(_) | ChatError::SparqlExecutionError(_) => {
ErrorCategory::Query
}
ChatError::SessionNotFound(_) | ChatError::SessionError(_) => ErrorCategory::Session,
ChatError::NetworkError(_) | ChatError::TimeoutError(_) => ErrorCategory::Network,
ChatError::ValidationError(_) => ErrorCategory::Validation,
_ => ErrorCategory::Internal,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ErrorCategory {
Retrieval,
Generation,
Query,
Session,
Network,
Validation,
Internal,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_error_display() {
let error = ChatError::SessionNotFound("test-session".to_string());
assert_eq!(error.to_string(), "Session not found: test-session");
}
#[test]
fn test_error_recoverability() {
let recoverable = ChatError::TimeoutError("timeout".to_string());
assert!(recoverable.is_recoverable());
let not_recoverable = ChatError::ValidationError("invalid".to_string());
assert!(!not_recoverable.is_recoverable());
}
#[test]
fn test_error_category() {
let error = ChatError::LlmGenerationError("failed".to_string());
assert_eq!(error.category(), ErrorCategory::Generation);
}
}