Skip to main content

tonic_rest_openapi/
error.rs

1//! Typed error enum for the `tonic-rest-openapi` library API.
2//!
3//! Library consumers can match on specific variants. The CLI (`main.rs`)
4//! converts these to `anyhow::Error` at the binary boundary for richer
5//! context messages.
6
7/// Errors produced by `tonic-rest-openapi` library operations.
8#[derive(Debug, thiserror::Error)]
9#[non_exhaustive]
10pub enum Error {
11    /// File I/O failure (reading config, descriptor, or spec files).
12    #[error(transparent)]
13    Io(#[from] std::io::Error),
14
15    /// YAML parsing or serialization failure.
16    #[error(transparent)]
17    Yaml(#[from] serde_yaml_ng::Error),
18
19    /// Proto `FileDescriptorSet` decoding failure.
20    #[error("failed to decode proto descriptor: {0}")]
21    ProtoDecode(#[from] prost::DecodeError),
22
23    /// A proto method name was not found in the descriptor set.
24    ///
25    /// Check spelling or verify the method has a `google.api.http` annotation.
26    #[error(
27        "method '{method}' not found in proto descriptors; \
28         check spelling or verify it has a google.api.http annotation"
29    )]
30    MethodNotFound {
31        /// The unresolved method name.
32        method: String,
33    },
34
35    /// A bare method name matches multiple services.
36    ///
37    /// Use qualified `Service.Method` syntax to disambiguate
38    /// (e.g., `"AuthService.Delete"` instead of `"Delete"`).
39    #[error(
40        "ambiguous method name '{method}' matches multiple services: {candidates:?}; \
41         use qualified 'Service.Method' syntax to disambiguate"
42    )]
43    AmbiguousMethodName {
44        /// The ambiguous bare method name.
45        method: String,
46        /// All matching operation IDs.
47        candidates: Vec<String>,
48    },
49}
50
51/// Convenience alias used throughout the library's public API.
52pub type Result<T> = std::result::Result<T, Error>;
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    /// Compile-time assertion that `Error` is `Send + Sync`.
59    /// Required for use in async contexts and across thread boundaries.
60    const _: () = {
61        const fn assert_send_sync<T: Send + Sync>() {}
62        assert_send_sync::<Error>();
63    };
64}