Skip to main content

troupekit_ffi/
error.rs

1use std::fmt;
2use troupekit_ffi_core as neutral;
3
4/// Stable numeric code of every failure, mirrored from the neutral `ErrorCode`.
5/// Swift and Kotlin compare `TroupekitError.code` against these numbers.
6/// The numbers are fixed forever and checked by `error_code_survives_conversion`.
7#[derive(Clone, Copy, Debug, PartialEq, Eq, uniffi::Enum)]
8#[repr(u32)]
9pub enum ErrorCode {
10    Config = 1,
11    MissingKey = 2,
12    Catalog = 3,
13    Persona = 4,
14    Agent = 5,
15    Tool = 6,
16    Workflow = 7,
17    Graph = 8,
18    Store = 9,
19    Context = 10,
20    Failed = 11,
21    Paused = 12,
22    Retries = 13,
23    Cancelled = 14,
24    HostCallback = 15,
25    InvalidArgument = 16,
26    Unsupported = 17,
27    Panic = 18,
28    Internal = 99,
29}
30
31/// The one error Swift and Kotlin see: the neutral `FfiError` with its code
32/// as a number, so a code added later still reaches the app unchanged.
33/// Input: a neutral error. Output: `Failure { code, detail, run_id }`; the
34/// text is `detail` because a Kotlin exception field named `message` clashes
35/// with `Throwable.message` in the generated code.
36#[derive(Clone, Debug, PartialEq, Eq, uniffi::Error)]
37pub enum TroupekitError {
38    Failure {
39        code: u32,
40        detail: String,
41        run_id: Option<String>,
42    },
43}
44
45impl fmt::Display for TroupekitError {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        let TroupekitError::Failure { code, detail, .. } = self;
48        write!(f, "troupekit error {code}: {detail}")
49    }
50}
51
52impl std::error::Error for TroupekitError {}
53
54impl From<neutral::FfiError> for TroupekitError {
55    fn from(error: neutral::FfiError) -> Self {
56        TroupekitError::Failure {
57            code: error.code as u32,
58            detail: error.message,
59            run_id: error.run_id,
60        }
61    }
62}
63
64/// Error a Swift or Kotlin host callback returns; the neutral layer turns it
65/// into a tool error output, a model failure or a transport failure.
66/// Input: the foreign text as `detail`, named so for the same Kotlin clash.
67/// Output: the neutral `HostError` with that text.
68#[derive(Clone, Debug, PartialEq, Eq, uniffi::Error)]
69pub enum HostError {
70    Failed { detail: String },
71}
72
73impl fmt::Display for HostError {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        let HostError::Failed { detail } = self;
76        f.write_str(detail)
77    }
78}
79
80impl std::error::Error for HostError {}
81
82impl From<uniffi::UnexpectedUniFFICallbackError> for HostError {
83    fn from(error: uniffi::UnexpectedUniFFICallbackError) -> Self {
84        HostError::Failed {
85            detail: error.reason,
86        }
87    }
88}
89
90impl From<HostError> for neutral::HostError {
91    fn from(error: HostError) -> Self {
92        let HostError::Failed { detail } = error;
93        neutral::HostError::new(detail)
94    }
95}