use thiserror::Error;
#[derive(Debug, Error)]
pub enum PulseHiveError {
#[error("Substrate error: {0}")]
Substrate(#[from] pulsedb::PulseDBError),
#[error("LLM error: {0}")]
Llm(String),
#[error("Tool error: {0}")]
Tool(String),
#[error("Agent error: {0}")]
Agent(String),
#[error("Configuration error: {0}")]
Config(String),
#[error("Validation error: {0}")]
Validation(String),
#[error("Embedding error: {0}")]
Embedding(String),
}
impl PulseHiveError {
pub fn llm(msg: impl Into<String>) -> Self {
Self::Llm(msg.into())
}
pub fn tool(msg: impl Into<String>) -> Self {
Self::Tool(msg.into())
}
pub fn agent(msg: impl Into<String>) -> Self {
Self::Agent(msg.into())
}
pub fn config(msg: impl Into<String>) -> Self {
Self::Config(msg.into())
}
pub fn validation(msg: impl Into<String>) -> Self {
Self::Validation(msg.into())
}
pub fn embedding(msg: impl Into<String>) -> Self {
Self::Embedding(msg.into())
}
}
pub type Result<T> = std::result::Result<T, PulseHiveError>;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_pulsedb_error_converts_via_from() {
let db_err = pulsedb::PulseDBError::config("test failure");
let hive_err: PulseHiveError = db_err.into();
assert!(matches!(hive_err, PulseHiveError::Substrate(_)));
assert!(hive_err.to_string().contains("test failure"));
}
#[test]
fn test_question_mark_propagation() {
fn inner() -> Result<()> {
Err(pulsedb::PulseDBError::config("missing collective"))?
}
let result = inner();
assert!(result.is_err());
assert!(matches!(result.unwrap_err(), PulseHiveError::Substrate(_)));
}
#[test]
fn test_convenience_constructors() {
assert_eq!(
PulseHiveError::llm("timeout").to_string(),
"LLM error: timeout"
);
assert_eq!(
PulseHiveError::tool("not found").to_string(),
"Tool error: not found"
);
assert_eq!(
PulseHiveError::agent("max iterations").to_string(),
"Agent error: max iterations"
);
assert_eq!(
PulseHiveError::config("no substrate").to_string(),
"Configuration error: no substrate"
);
assert_eq!(
PulseHiveError::validation("empty content").to_string(),
"Validation error: empty content"
);
assert_eq!(
PulseHiveError::embedding("dimension mismatch").to_string(),
"Embedding error: dimension mismatch"
);
}
#[test]
fn test_implements_std_error() {
let err = PulseHiveError::llm("test");
let _: &dyn std::error::Error = &err;
}
}