use crate::output_parsers::OutputParserError;
use std::fmt;
#[derive(Debug, Clone)]
#[non_exhaustive]
pub enum LcelError {
Provider(String),
Chain(String),
Agent(String),
Graph(String),
Tool(String),
OutputParser(String),
Stream(String),
Pipeline(String),
TypeMismatch(String),
Other(String),
}
impl fmt::Display for LcelError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
LcelError::Provider(msg) => write!(f, "Provider error: {msg}"),
LcelError::Chain(msg) => write!(f, "Chain error: {msg}"),
LcelError::Agent(msg) => write!(f, "Agent error: {msg}"),
LcelError::Graph(msg) => write!(f, "Graph error: {msg}"),
LcelError::Tool(msg) => write!(f, "Tool error: {msg}"),
LcelError::OutputParser(msg) => write!(f, "Output parser error: {msg}"),
LcelError::Stream(msg) => write!(f, "Stream error: {msg}"),
LcelError::Pipeline(msg) => write!(f, "Pipeline error: {msg}"),
LcelError::TypeMismatch(msg) => write!(f, "Type mismatch: {msg}"),
LcelError::Other(msg) => write!(f, "{msg}"),
}
}
}
impl std::error::Error for LcelError {}
impl From<std::convert::Infallible> for LcelError {
fn from(_: std::convert::Infallible) -> Self {
unreachable!()
}
}
impl From<OutputParserError> for LcelError {
fn from(e: OutputParserError) -> Self {
LcelError::OutputParser(e.to_string())
}
}
impl From<crate::tools::ToolError> for LcelError {
fn from(e: crate::tools::ToolError) -> Self {
LcelError::Tool(e.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn display_formats_correctly() {
assert_eq!(
LcelError::Provider("openai timeout".to_string()).to_string(),
"Provider error: openai timeout"
);
assert_eq!(
LcelError::Chain("missing input".to_string()).to_string(),
"Chain error: missing input"
);
assert_eq!(
LcelError::TypeMismatch("expected String got i32".to_string()).to_string(),
"Type mismatch: expected String got i32"
);
}
#[test]
fn is_send_sync() {
fn assert_send_sync<T: Send + Sync + 'static>() {}
assert_send_sync::<LcelError>();
}
#[test]
fn from_output_parser_error() {
let e = OutputParserError::JsonError("bad json".to_string());
let lcel: LcelError = e.into();
assert!(matches!(
lcel,
LcelError::OutputParser(ref msg) if msg.contains("bad json")
));
assert_eq!(
lcel.to_string(),
"Output parser error: JSON error: bad json"
);
}
#[test]
fn from_tool_error() {
use crate::tools::ToolError;
let e = ToolError::InvalidInput("bad input".to_string());
let lcel: LcelError = e.into();
assert!(matches!(
lcel,
LcelError::Tool(ref msg) if msg.contains("bad input")
));
assert_eq!(lcel.to_string(), "Tool error: Invalid input: bad input");
}
}