ibridge_server/
error_map.rs1use std::borrow::Cow;
6
7use ibridge_core::CoreError;
8use rmcp::model::ErrorData;
9
10#[derive(Debug)]
12#[non_exhaustive]
13pub enum MappedError {
14 Rpc(ErrorData),
16 Tool(Cow<'static, str>),
18}
19
20#[must_use]
26pub fn map_core_error(error: &CoreError) -> MappedError {
27 match error {
28 CoreError::InvalidInput(message) => {
29 MappedError::Rpc(ErrorData::invalid_params(message.clone(), None))
30 }
31 CoreError::NotFound => {
32 MappedError::Tool(Cow::Borrowed("not found; check the id and fetch it again"))
33 }
34 CoreError::Conflict => {
35 MappedError::Tool(Cow::Borrowed("changed since you read it; fetch it again"))
36 }
37 CoreError::Forbidden => MappedError::Tool(Cow::Borrowed(
38 "iCloud refused this change; the calendar or address book may be read-only, or the item may not be valid",
39 )),
40 CoreError::Auth => {
41 MappedError::Tool(Cow::Borrowed("authentication failed; run ibridge login"))
42 }
43 _ => MappedError::Rpc(ErrorData::internal_error("internal error", None)),
45 }
46}
47
48#[cfg(test)]
49mod tests {
50 use ibridge_core::CoreError;
51 use rmcp::model::ErrorCode;
52
53 use super::{MappedError, map_core_error};
54
55 #[test]
56 fn forbidden_is_a_tool_error_that_does_not_ask_to_reconnect() {
57 let MappedError::Tool(message) = map_core_error(&CoreError::Forbidden) else {
58 panic!("a refused change is a tool error the model can read");
59 };
60 assert!(
61 message.contains("read-only") && !message.contains("login"),
62 "the message names the cause and not the password, got {message}"
63 );
64 }
65
66 #[test]
67 fn error_map_hides_server_text() {
68 let raw = "NO [ALERT] password=hunter2 imap banner from the server";
69 let mapped = map_core_error(&CoreError::Protocol(raw.to_owned()));
70 let MappedError::Rpc(data) = mapped else {
71 panic!("a protocol failure is an internal error");
72 };
73 let rendered = format!("{data} {:?}", data.data);
74 assert!(
75 !rendered.contains("hunter2") && !rendered.contains("imap banner"),
76 "raw server text leaked into the MCP error: {rendered}"
77 );
78 assert_eq!(
79 data.code,
80 ErrorCode::INTERNAL_ERROR,
81 "a protocol failure is an internal error, got {}",
82 data.code.0
83 );
84 }
85}