use oinkie::Error;
use rmcp::ErrorData;
pub fn to_mcp(e: Error) -> ErrorData {
let message = e.to_string();
if e.is_caller_fault() {
ErrorData::invalid_params(message, None)
} else {
ErrorData::internal_error(message, None)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_the_library_message_is_carried_through_unchanged() {
let e = Error::IncompatibleAnalysis(
oinkie::birthmarks::BirthmarkType::OpSeq,
oinkie::compare::Algorithm::Euclidean,
);
let expected = e.to_string();
assert!(expected.contains("op-freq-euclidean"), "{expected}");
assert_eq!(to_mcp(e).message, expected);
}
#[test]
fn test_a_bad_name_is_the_callers_fault() {
let e = Error::BirthmarkType("nonsense".to_string());
assert_eq!(
to_mcp(e).code,
rmcp::model::ErrorCode::INVALID_PARAMS,
"an unknown birthmark name is something the caller can fix"
);
}
#[test]
fn test_an_internal_failure_is_not_blamed_on_the_caller() {
let e = Error::InvalidPcode(9999);
assert_ne!(to_mcp(e).code, rmcp::model::ErrorCode::INVALID_PARAMS);
}
#[test]
fn test_a_group_keeps_its_numbering() {
let e = Error::Array(vec![
Error::Parse("first".to_string()),
Error::Parse("second".to_string()),
]);
let d = to_mcp(e);
assert!(d.message.contains("1. Parse error: first"), "{}", d.message);
assert!(
d.message.contains("2. Parse error: second"),
"{}",
d.message
);
}
}