troupekit_ffi_core/
error.rs1#[non_exhaustive]
5#[repr(u32)]
6#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
7pub enum ErrorCode {
8 Config = 1,
9 MissingKey = 2,
10 Catalog = 3,
11 Persona = 4,
12 Agent = 5,
13 Tool = 6,
14 Workflow = 7,
15 Graph = 8,
16 Store = 9,
17 Context = 10,
18 Failed = 11,
19 Paused = 12,
20 Retries = 13,
21 Cancelled = 14,
22 HostCallback = 15,
23 InvalidArgument = 16,
24 Unsupported = 17,
25 Panic = 18,
26 Internal = 99,
27}
28
29#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
33#[error("{message}")]
34pub struct FfiError {
35 pub code: ErrorCode,
36 pub message: String,
37 pub run_id: Option<String>,
38}
39
40impl FfiError {
41 pub fn new(code: ErrorCode, message: impl Into<String>) -> FfiError {
45 let message = message.into();
46 assert!(!message.trim().is_empty());
47 FfiError {
48 code,
49 message,
50 run_id: None,
51 }
52 }
53}
54
55impl From<troupekit_kit::KitError> for FfiError {
56 fn from(err: troupekit_kit::KitError) -> FfiError {
63 use troupekit_kit::KitError as K;
64 let message = err.to_string();
65 if let K::InRun { run_id, source } = err {
66 let inner = FfiError::from(*source);
67 return FfiError {
68 code: inner.code,
69 message,
70 run_id: Some(run_id.to_string()),
71 };
72 }
73 let (code, run_id) = match &err {
74 K::Config { .. } => (ErrorCode::Config, None),
75 K::MissingKey { .. } => (ErrorCode::MissingKey, None),
76 K::Catalog { .. } => (ErrorCode::Catalog, None),
77 K::Persona(_) => (ErrorCode::Persona, None),
78 K::Agent(_) => (ErrorCode::Agent, None),
79 K::Tool(_) => (ErrorCode::Tool, None),
80 K::Workflow(_) => (ErrorCode::Workflow, None),
81 K::Graph(_) => (ErrorCode::Graph, None),
82 K::Store(_) => (ErrorCode::Store, None),
83 #[cfg(feature = "knowledge")]
84 K::Context(_) => (ErrorCode::Context, None),
85 K::HostFailed { run_id, .. } => (ErrorCode::HostCallback, Some(run_id.to_string())),
86 K::Failed { run_id, .. } => (ErrorCode::Failed, Some(run_id.to_string())),
87 K::Paused { run_id, .. } => (ErrorCode::Paused, Some(run_id.to_string())),
88 K::Retries { run_id, .. } => (ErrorCode::Retries, Some(run_id.to_string())),
89 K::Cancelled { run_id } => (ErrorCode::Cancelled, Some(run_id.to_string())),
90 #[allow(unreachable_patterns)]
91 _ => (ErrorCode::Internal, None),
92 };
93 assert!(!message.is_empty());
94 FfiError {
95 code,
96 message,
97 run_id,
98 }
99 }
100}