use switchyard_protocol::LlmClientError;
use crate::LibsyError;
pub(crate) fn safe_error_summary(error: &LibsyError) -> String {
match error {
LibsyError::ClientCall { target, source } => {
format!(
"client call to target {target:?} failed: {}",
safe_client_error(source)
)
}
LibsyError::AlgorithmError { .. } => "algorithm error".to_string(),
LibsyError::Driver(_) => "algorithm driver failed".to_string(),
LibsyError::External { operation, .. } => format!("{operation} failed"),
LibsyError::TargetNotFound { .. }
| LibsyError::NoTargets
| LibsyError::MissingFinalResponse => error.to_string(),
}
}
pub(crate) fn safe_client_error(error: &LlmClientError) -> String {
match error {
LlmClientError::UpstreamHttp { status, .. } => format!("upstream HTTP {status}"),
LlmClientError::ContextWindowExceeded { model, .. } => {
format!("context window exceeded for model {model}")
}
LlmClientError::Timeout { .. } => "upstream request timed out".to_string(),
LlmClientError::Transport { .. } => "upstream transport error".to_string(),
LlmClientError::InvalidResponse { .. } => "invalid upstream response".to_string(),
LlmClientError::Ffi { .. } => "foreign function interface error".to_string(),
LlmClientError::InvalidRequest { .. } => "invalid request".to_string(),
LlmClientError::RequestTranslation(_) => "request translation failed".to_string(),
LlmClientError::RequestEncoding(_) => "outbound request encoding failed".to_string(),
LlmClientError::ResponseTranslation(_) => "response translation failed".to_string(),
LlmClientError::Configuration { .. } => "client configuration error".to_string(),
LlmClientError::General(_) => "client call failed".to_string(),
_ => "client call failed".to_string(),
}
}
#[cfg(test)]
mod tests {
use switchyard_protocol::ModelId;
use super::*;
const SECRET: &str = "patient name is Jane Doe";
#[test]
fn an_upstream_body_never_reaches_the_summary() {
let error = LibsyError::ClientCall {
target: ModelId::new("weak"),
source: LlmClientError::UpstreamHttp {
status: http::StatusCode::BAD_REQUEST,
body: format!(r#"{{"error":{{"message":"bad request: {SECRET}"}}}}"#),
},
};
let summary = safe_error_summary(&error);
assert!(summary.contains("upstream HTTP 400"), "{summary}");
assert!(summary.contains("weak"), "{summary}");
assert!(!summary.contains(SECRET), "{summary}");
assert!(error.to_string().contains(SECRET));
}
#[test]
fn boxed_sources_are_reduced_to_their_class() {
for source in [
LlmClientError::Transport {
source: std::io::Error::other(SECRET).into(),
},
LlmClientError::InvalidResponse {
source: std::io::Error::other(SECRET).into(),
},
LlmClientError::Timeout {
source: std::io::Error::other(SECRET).into(),
},
] {
let summary = safe_client_error(&source);
assert!(!summary.contains(SECRET), "{summary}");
assert!(!summary.is_empty());
}
}
#[test]
fn context_overflow_keeps_the_model_but_not_the_message() {
let summary = safe_client_error(&LlmClientError::ContextWindowExceeded {
model: ModelId::new("weak"),
message: SECRET.to_string(),
});
assert!(summary.contains("weak"), "{summary}");
assert!(!summary.contains(SECRET), "{summary}");
}
#[test]
fn an_algorithm_error_message_is_reduced_to_its_class() {
let summary = safe_error_summary(&LibsyError::AlgorithmError {
message: format!("judge reply did not parse: {SECRET}"),
});
assert_eq!(summary, "algorithm error");
assert!(!summary.contains(SECRET), "{summary}");
}
#[test]
fn a_client_configuration_message_is_reduced_to_its_class() {
let summary = safe_client_error(&LlmClientError::Configuration {
message: SECRET.to_string(),
});
assert_eq!(summary, "client configuration error");
}
#[test]
fn a_general_error_is_reduced_to_its_class() {
let summary = safe_client_error(&LlmClientError::General(SECRET.to_string()));
assert!(!summary.contains(SECRET), "{summary}");
}
#[test]
fn an_extension_failure_keeps_its_label_but_not_its_source() {
let error = LibsyError::external("loading extension", std::io::Error::other(SECRET));
let summary = safe_error_summary(&error);
assert_eq!(summary, "loading extension failed");
assert!(error.to_string().contains(SECRET));
}
#[test]
fn structural_variants_keep_their_detail() {
let summary = safe_error_summary(&LibsyError::TargetNotFound {
target: ModelId::new("strong"),
});
assert!(summary.contains("strong"), "{summary}");
}
}