Skip to main content

ibridge_server/
error_map.rs

1// SPDX-License-Identifier: Apache-2.0
2
3//! Maps [`ibridge_core::CoreError`] onto MCP errors.
4
5use std::borrow::Cow;
6
7use ibridge_core::CoreError;
8use rmcp::model::ErrorData;
9
10/// A provider failure the tool layer can return.
11#[derive(Debug)]
12#[non_exhaustive]
13pub enum MappedError {
14    /// A JSON-RPC error. The message has no secrets and no raw server text.
15    Rpc(ErrorData),
16    /// A tool error the model can act on. Not a protocol failure.
17    Tool(Cow<'static, str>),
18}
19
20/// Turns a provider error into an MCP error.
21///
22/// Bad input is `invalid_params`. Not found and conflict are tool errors that
23/// say what to do next. Authentication tells the user to run `ibridge login`.
24/// Every other failure is an internal error whose message drops protocol text.
25#[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        // Protocol text, and every other provider failure, stay off the wire.
44        _ => 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}