use crate::error::ConductorError;
#[must_use]
pub fn exception_label(err: &ConductorError) -> &'static str {
match err {
ConductorError::Http(_) => "Http",
ConductorError::Json(_) => "Json",
ConductorError::Config(_) => "Config",
ConductorError::Auth(_) => "Auth",
ConductorError::TaskExecution(_) => "TaskExecution",
ConductorError::TaskNotFound(_) => "TaskNotFound",
ConductorError::WorkflowNotFound(_) => "WorkflowNotFound",
ConductorError::Workflow(_) => "Workflow",
ConductorError::Worker(_) => "Worker",
ConductorError::Timeout(_) => "Timeout",
ConductorError::Server { .. } => "Server",
ConductorError::Api { .. } => "Api",
ConductorError::Internal(_) => "Internal",
ConductorError::Io(_) => "Io",
ConductorError::Channel(_) => "Channel",
#[cfg(feature = "agents")]
ConductorError::Agent(_) => "Agent",
#[cfg(feature = "agents")]
ConductorError::CredentialNotFound(_) => "CredentialNotFound",
#[cfg(feature = "agents")]
ConductorError::TerminalTool(_) => "TerminalTool",
}
}
pub fn type_name_of<T>(_value: &T) -> &'static str
where
T: ?Sized,
{
last_type_segment(std::any::type_name::<T>())
}
pub fn exception_label_for_panic(_payload: &(dyn std::any::Any + Send)) -> &'static str {
"Panic"
}
fn last_type_segment(full: &'static str) -> &'static str {
let base = match full.find('<') {
Some(idx) => &full[..idx],
None => full,
};
match base.rfind("::") {
Some(idx) => &base[idx + 2..],
None => base,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn conductor_error_variant_names() {
assert_eq!(
exception_label(&ConductorError::Auth("bad creds".into())),
"Auth"
);
assert_eq!(
exception_label(&ConductorError::Worker("oops".into())),
"Worker"
);
assert_eq!(
exception_label(&ConductorError::Server {
status: 500,
message: "boom".into(),
}),
"Server"
);
#[cfg(feature = "agents")]
assert_eq!(
exception_label(&ConductorError::TerminalTool("boom".into())),
"TerminalTool"
);
}
#[test]
fn strips_module_path() {
assert_eq!(last_type_segment("std::io::Error"), "Error");
assert_eq!(last_type_segment("reqwest::Error"), "Error");
assert_eq!(last_type_segment("Foo"), "Foo");
assert_eq!(last_type_segment("core::option::Option<String>"), "Option");
}
#[test]
fn panic_payload_label() {
let payload: Box<dyn std::any::Any + Send> = Box::new("panicked");
assert_eq!(exception_label_for_panic(payload.as_ref()), "Panic");
}
}