lc_core/runnables/
error.rs1use crate::output_parsers::OutputParserError;
19use std::fmt;
20
21#[derive(Debug, Clone)]
26#[non_exhaustive]
27pub enum LcelError {
28 Provider(String),
30
31 Chain(String),
33
34 Agent(String),
36
37 Graph(String),
39
40 Tool(String),
42
43 OutputParser(String),
45
46 Stream(String),
48
49 Pipeline(String),
51
52 TypeMismatch(String),
54
55 Other(String),
57}
58
59impl fmt::Display for LcelError {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 match self {
62 LcelError::Provider(msg) => write!(f, "Provider error: {msg}"),
63 LcelError::Chain(msg) => write!(f, "Chain error: {msg}"),
64 LcelError::Agent(msg) => write!(f, "Agent error: {msg}"),
65 LcelError::Graph(msg) => write!(f, "Graph error: {msg}"),
66 LcelError::Tool(msg) => write!(f, "Tool error: {msg}"),
67 LcelError::OutputParser(msg) => write!(f, "Output parser error: {msg}"),
68 LcelError::Stream(msg) => write!(f, "Stream error: {msg}"),
69 LcelError::Pipeline(msg) => write!(f, "Pipeline error: {msg}"),
70 LcelError::TypeMismatch(msg) => write!(f, "Type mismatch: {msg}"),
71 LcelError::Other(msg) => write!(f, "{msg}"),
72 }
73 }
74}
75
76impl std::error::Error for LcelError {}
77
78impl From<std::convert::Infallible> for LcelError {
80 fn from(_: std::convert::Infallible) -> Self {
81 unreachable!()
82 }
83}
84
85impl From<OutputParserError> for LcelError {
89 fn from(e: OutputParserError) -> Self {
90 LcelError::OutputParser(e.to_string())
91 }
92}
93
94impl From<crate::tools::ToolError> for LcelError {
97 fn from(e: crate::tools::ToolError) -> Self {
98 LcelError::Tool(e.to_string())
99 }
100}
101
102#[cfg(test)]
103mod tests {
104 use super::*;
105
106 #[test]
107 fn display_formats_correctly() {
108 assert_eq!(
109 LcelError::Provider("openai timeout".to_string()).to_string(),
110 "Provider error: openai timeout"
111 );
112 assert_eq!(
113 LcelError::Chain("missing input".to_string()).to_string(),
114 "Chain error: missing input"
115 );
116 assert_eq!(
117 LcelError::TypeMismatch("expected String got i32".to_string()).to_string(),
118 "Type mismatch: expected String got i32"
119 );
120 }
121
122 #[test]
123 fn is_send_sync() {
124 fn assert_send_sync<T: Send + Sync + 'static>() {}
125 assert_send_sync::<LcelError>();
126 }
127
128 #[test]
129 fn from_output_parser_error() {
130 let e = OutputParserError::JsonError("bad json".to_string());
132 let lcel: LcelError = e.into();
133 assert!(matches!(
134 lcel,
135 LcelError::OutputParser(ref msg) if msg.contains("bad json")
136 ));
137 assert_eq!(
138 lcel.to_string(),
139 "Output parser error: JSON error: bad json"
140 );
141 }
142
143 #[test]
144 fn from_tool_error() {
145 use crate::tools::ToolError;
147 let e = ToolError::InvalidInput("bad input".to_string());
148 let lcel: LcelError = e.into();
149 assert!(matches!(
150 lcel,
151 LcelError::Tool(ref msg) if msg.contains("bad input")
152 ));
153 assert_eq!(lcel.to_string(), "Tool error: Invalid input: bad input");
154 }
155}