Skip to main content

dioxus_docs_kit/
error.rs

1//! Error types for registry construction.
2
3#[cfg(feature = "openapi")]
4use dioxus_mdx::OpenApiError;
5
6/// Errors produced when building a [`DocsRegistry`](crate::DocsRegistry) or
7/// [`BlogRegistry`](crate::blog::BlogRegistry) from configuration.
8#[derive(Debug)]
9#[non_exhaustive]
10pub enum DocsKitError {
11    /// `_nav.json` failed to parse.
12    NavParse(serde_json::Error),
13    /// `_blog.json` failed to parse.
14    BlogManifestParse(serde_json::Error),
15    /// Invalid blog configuration (for example, ambiguous category slugs).
16    BlogConfig(String),
17    /// An OpenAPI spec failed to parse. Only present with the `openapi` feature.
18    #[cfg(feature = "openapi")]
19    OpenApi {
20        /// URL prefix the spec was registered under.
21        prefix: String,
22        /// The underlying parse error.
23        error: OpenApiError,
24    },
25}
26
27impl std::fmt::Display for DocsKitError {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            Self::NavParse(e) => write!(f, "failed to parse _nav.json: {e}"),
31            Self::BlogManifestParse(e) => write!(f, "failed to parse _blog.json: {e}"),
32            Self::BlogConfig(message) => write!(f, "invalid blog configuration: {message}"),
33            #[cfg(feature = "openapi")]
34            Self::OpenApi { prefix, error } => write!(
35                f,
36                "failed to parse OpenAPI spec for prefix \"{prefix}\": {error}"
37            ),
38        }
39    }
40}
41
42impl std::error::Error for DocsKitError {
43    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
44        match self {
45            Self::NavParse(e) | Self::BlogManifestParse(e) => Some(e),
46            Self::BlogConfig(_) => None,
47            #[cfg(feature = "openapi")]
48            Self::OpenApi { error, .. } => Some(error),
49        }
50    }
51}