#[non_exhaustive]
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum ErrorCode {
Config = 1,
MissingKey = 2,
Catalog = 3,
Persona = 4,
Agent = 5,
Tool = 6,
Workflow = 7,
Graph = 8,
Store = 9,
Context = 10,
Failed = 11,
Paused = 12,
Retries = 13,
Cancelled = 14,
HostCallback = 15,
InvalidArgument = 16,
Unsupported = 17,
Panic = 18,
Internal = 99,
}
#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)]
#[error("{message}")]
pub struct FfiError {
pub code: ErrorCode,
pub message: String,
pub run_id: Option<String>,
}
impl FfiError {
pub fn new(code: ErrorCode, message: impl Into<String>) -> FfiError {
let message = message.into();
assert!(!message.trim().is_empty());
FfiError {
code,
message,
run_id: None,
}
}
}
impl From<troupekit_kit::KitError> for FfiError {
fn from(err: troupekit_kit::KitError) -> FfiError {
use troupekit_kit::KitError as K;
let message = err.to_string();
if let K::InRun { run_id, source } = err {
let inner = FfiError::from(*source);
return FfiError {
code: inner.code,
message,
run_id: Some(run_id.to_string()),
};
}
let (code, run_id) = match &err {
K::Config { .. } => (ErrorCode::Config, None),
K::MissingKey { .. } => (ErrorCode::MissingKey, None),
K::Catalog { .. } => (ErrorCode::Catalog, None),
K::Persona(_) => (ErrorCode::Persona, None),
K::Agent(_) => (ErrorCode::Agent, None),
K::Tool(_) => (ErrorCode::Tool, None),
K::Workflow(_) => (ErrorCode::Workflow, None),
K::Graph(_) => (ErrorCode::Graph, None),
K::Store(_) => (ErrorCode::Store, None),
#[cfg(feature = "knowledge")]
K::Context(_) => (ErrorCode::Context, None),
K::HostFailed { run_id, .. } => (ErrorCode::HostCallback, Some(run_id.to_string())),
K::Failed { run_id, .. } => (ErrorCode::Failed, Some(run_id.to_string())),
K::Paused { run_id, .. } => (ErrorCode::Paused, Some(run_id.to_string())),
K::Retries { run_id, .. } => (ErrorCode::Retries, Some(run_id.to_string())),
K::Cancelled { run_id } => (ErrorCode::Cancelled, Some(run_id.to_string())),
#[allow(unreachable_patterns)]
_ => (ErrorCode::Internal, None),
};
assert!(!message.is_empty());
FfiError {
code,
message,
run_id,
}
}
}