Skip to main content

webui/
error.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3
4//! Error types for the WebUI library.
5
6use thiserror::Error;
7
8/// Errors that can occur during WebUI build, render, or inspection operations.
9#[derive(Debug, Error)]
10pub enum WebUIError {
11    /// I/O error (file read/write failures).
12    #[error("I/O error: {context}")]
13    Io {
14        /// What operation failed.
15        context: String,
16        /// The underlying I/O error.
17        #[source]
18        source: std::io::Error,
19    },
20
21    /// Component registration failure.
22    #[error("Component registration error: {0}")]
23    ComponentRegistration(String),
24
25    /// Component discovery failure (npm packages or local paths).
26    #[error("Component discovery error: {0}")]
27    ComponentDiscovery(String),
28
29    /// HTML/CSS parsing failure with context about which file failed.
30    #[error("{context}")]
31    Parse {
32        /// What was being parsed.
33        context: String,
34        /// The underlying parse error.
35        #[source]
36        source: webui_parser::ParserError,
37    },
38
39    /// Protocol serialization or deserialization failure.
40    #[error("protocol error")]
41    Protocol(#[from] webui_protocol::ProtocolError),
42
43    /// JSON serialization failure.
44    #[error("Serialization error: {0}")]
45    Serialization(String),
46
47    /// Handler rendering error.
48    #[error("rendering error")]
49    Rendering(#[from] webui_handler::HandlerError),
50
51    /// Invalid build-option configuration.
52    #[error("Invalid build options: {0}")]
53    InvalidBuildOptions(String),
54}
55
56impl WebUIError {
57    /// The full, single-line message including the source chain, e.g.
58    /// `Failed to parse index.html: Directive error: Invalid for each: x`.
59    ///
60    /// Each error layer's [`std::fmt::Display`] describes only its own level
61    /// (the `#[source]` chain carries the rest), so the CLI and dev server can
62    /// use anyhow's `{:#}` formatting without repetition. Hosts that surface a
63    /// flat string instead of walking the chain — Node, FFI — use this helper
64    /// to get the same complete message.
65    #[must_use]
66    pub fn chain_message(&self) -> String {
67        let mut message = self.to_string();
68        let mut source = std::error::Error::source(self);
69        while let Some(err) = source {
70            message.push_str(": ");
71            message.push_str(&err.to_string());
72            source = err.source();
73        }
74        message
75    }
76}