swiftide_agents/tasks/
errors.rs1use std::{any::Any, sync::Arc};
2
3use super::transition::TransitionPayload;
4
5#[derive(thiserror::Error, Debug)]
6pub enum TaskError {
7 #[error(transparent)]
8 NodeError(#[from] NodeError),
9
10 #[error("MissingTransition: {0}")]
11 MissingTransition(String),
12
13 #[error("MissingNode: {0}")]
14 MissingNode(String),
15
16 #[error("Task failed with wrong output")]
17 TypeError(String),
18
19 #[error("MissingInput: {0}")]
20 MissingInput(String),
21
22 #[error("MissingOutput: {0}")]
23 MissingOutput(String),
24
25 #[error("Task is missing steps")]
26 NoSteps,
27}
28
29impl TaskError {
30 pub fn missing_transition(node_id: usize) -> Self {
31 TaskError::MissingTransition(format!("Node {node_id} is missing a transition"))
32 }
33
34 pub fn missing_node(node_id: usize) -> Self {
35 TaskError::MissingNode(format!("Node {node_id} is missing"))
36 }
37
38 pub fn missing_input(node_id: usize) -> Self {
39 TaskError::MissingInput(format!("Node {node_id} is missing input"))
40 }
41
42 pub fn missing_output(node_id: usize) -> Self {
43 TaskError::MissingOutput(format!("Node {node_id} is missing output"))
44 }
45
46 pub fn type_error<T: Any + Send>(output: &T) -> Self {
47 let message = format!(
48 "Expected output of type {}, but got {:?}",
49 std::any::type_name::<T>(),
50 output.type_id()
51 );
52 TaskError::TypeError(message)
53 }
54}
55
56#[derive(Debug, thiserror::Error)]
57pub struct NodeError {
58 pub node_error: Box<dyn std::error::Error + Send + Sync>,
59 pub transition_payload: Option<Arc<TransitionPayload>>,
60 pub node_id: usize,
61}
62
63impl std::fmt::Display for NodeError {
64 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
65 write!(
66 f,
67 "Node error in node {}: {:?}",
68 self.node_id, self.node_error
69 )
70 }
71}
72
73impl NodeError {
74 pub fn new(
75 node_error: impl Into<Box<dyn std::error::Error + Send + Sync>>,
76 node_id: usize,
77 transition_payload: Option<TransitionPayload>,
78 ) -> Self {
79 Self {
80 node_error: node_error.into(),
81 transition_payload: transition_payload.map(Arc::new),
82 node_id,
83 }
84 }
85}