Skip to main content

turbomcp_openapi/
error.rs

1//! Error types for OpenAPI operations.
2
3use thiserror::Error;
4
5/// Result type for OpenAPI operations.
6pub type Result<T> = std::result::Result<T, OpenApiError>;
7
8/// Errors that can occur during OpenAPI operations.
9#[derive(Debug, Error)]
10pub enum OpenApiError {
11    /// Failed to fetch OpenAPI spec from URL.
12    #[error("failed to fetch OpenAPI spec: {0}")]
13    FetchError(#[from] reqwest::Error),
14
15    /// Failed to parse OpenAPI spec.
16    #[error("failed to parse OpenAPI spec: {0}")]
17    ParseError(String),
18
19    /// Failed to read OpenAPI spec from file.
20    #[error("failed to read OpenAPI spec file: {0}")]
21    IoError(#[from] std::io::Error),
22
23    /// Invalid URL.
24    #[error("invalid URL: {0}")]
25    InvalidUrl(#[from] url::ParseError),
26
27    /// Invalid regex pattern.
28    #[error("invalid regex pattern: {0}")]
29    InvalidPattern(#[from] regex::Error),
30
31    /// API call failed.
32    #[error("API call failed: {0}")]
33    ApiError(String),
34
35    /// Missing required parameter.
36    #[error("missing required parameter: {0}")]
37    MissingParameter(String),
38
39    /// Invalid parameter value.
40    #[error("invalid parameter value for '{0}': {1}")]
41    InvalidParameter(String, String),
42
43    /// Operation not found.
44    #[error("operation not found: {0}")]
45    OperationNotFound(String),
46
47    /// Base URL not configured.
48    #[error("base URL not configured - call with_base_url() before making API calls")]
49    NoBaseUrl,
50
51    /// SSRF protection blocked the request.
52    #[error("SSRF protection: {0}")]
53    SsrfBlocked(String),
54
55    /// Request timed out.
56    #[error("request timed out after {0} seconds")]
57    Timeout(u64),
58}
59
60impl From<serde_json::Error> for OpenApiError {
61    fn from(err: serde_json::Error) -> Self {
62        Self::ParseError(err.to_string())
63    }
64}
65
66impl From<serde_yaml::Error> for OpenApiError {
67    fn from(err: serde_yaml::Error) -> Self {
68        Self::ParseError(err.to_string())
69    }
70}