Skip to main content

troupekit_ffi_core/
error.rs

1/// Stable numeric code of every failure the neutral API reports.
2/// The numbers are fixed forever: a binding may match on them, store
3/// them or send them over the wire. New codes only ever get new numbers.
4#[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/// The one error type of the neutral API: a stable `code`, a `message`
30/// that never holds a key or secret, and the `run_id` text when the
31/// failure belongs to a run that can be resumed or inspected.
32#[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    /// Builds an error with `code` and `message` and no run id.
42    /// Input: the code and a message free of secrets. Output: the error.
43    /// The message must not be blank.
44    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    /// Maps every kit error to its stable code, with the kit's message
57    /// (which never holds a key) and the run id for run outcomes. An
58    /// `InRun` failure takes its inner error's code and its own run id.
59    /// A run failed by a host model (`HostFailed`, set from the typed
60    /// error kind, never the message text) is `HostCallback`. A kit variant
61    /// added later maps to `Internal` until it gets its own code.
62    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}