Skip to main content

github_mcp/core/
errors.rs

1// GitHub v3 REST API MCP server — generated by mcpify. Do not hand-edit.
2
3use thiserror::Error;
4
5/// Mirrors `targets::typescript`'s `McpifyError` hierarchy (`errors.ts`):
6/// one variant per error family, each carrying the literal `code()` string
7/// callers match on instead of downcasting.
8#[derive(Debug, Error)]
9pub enum McpifyError {
10    #[error("{0}")]
11    Configuration(String),
12
13    #[error("{0}")]
14    Authentication(String),
15
16    #[error("{message}")]
17    Validation {
18        message: String,
19        details: Option<serde_json::Value>,
20    },
21
22    #[error("{0}")]
23    NotFound(String),
24
25    #[error("circuit breaker is open")]
26    CircuitBreakerOpen,
27
28    #[error("rate limit exceeded")]
29    RateLimitExceeded,
30}
31
32impl McpifyError {
33    pub fn code(&self) -> &'static str {
34        match self {
35            Self::Configuration(_) => "CONFIGURATION_ERROR",
36            Self::Authentication(_) => "AUTHENTICATION_ERROR",
37            Self::Validation { .. } => "VALIDATION_ERROR",
38            Self::NotFound(_) => "NOT_FOUND",
39            Self::CircuitBreakerOpen => "CIRCUIT_BREAKER_OPEN",
40            Self::RateLimitExceeded => "RATE_LIMIT_EXCEEDED",
41        }
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn every_variant_reports_its_error_code() {
51        assert_eq!(
52            McpifyError::Configuration("x".into()).code(),
53            "CONFIGURATION_ERROR"
54        );
55        assert_eq!(
56            McpifyError::Authentication("x".into()).code(),
57            "AUTHENTICATION_ERROR"
58        );
59        assert_eq!(
60            McpifyError::Validation {
61                message: "x".into(),
62                details: None
63            }
64            .code(),
65            "VALIDATION_ERROR"
66        );
67        assert_eq!(McpifyError::NotFound("x".into()).code(), "NOT_FOUND");
68        assert_eq!(
69            McpifyError::CircuitBreakerOpen.code(),
70            "CIRCUIT_BREAKER_OPEN"
71        );
72        assert_eq!(McpifyError::RateLimitExceeded.code(), "RATE_LIMIT_EXCEEDED");
73    }
74}