Skip to main content

webui_desktop_build/
error.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3
4use std::path::PathBuf;
5
6/// Recoverable, plain-text build diagnostics with stable machine-readable codes.
7#[derive(Debug, thiserror::Error)]
8pub enum GenerateError {
9    /// Invalid schema, including its descriptor-qualified source context.
10    #[error("{code}: {context}: {message}\nhelp: {help}")]
11    Schema {
12        /// Stable diagnostic identifier.
13        code: &'static str,
14        /// Source file and fully qualified descriptor name.
15        context: String,
16        /// What failed.
17        message: String,
18        /// How to repair the schema.
19        help: String,
20    },
21    /// An external compiler is missing or failed.
22    #[error("ipc-tool: {tool}: {message}\nhelp: {help}")]
23    Tool {
24        /// Tool name.
25        tool: String,
26        /// Bounded compiler diagnostic.
27        message: String,
28        /// Installation or configuration guidance.
29        help: String,
30    },
31    /// Filesystem failure with the owning path.
32    #[error("ipc-io: {path}: {source}\nhelp: check the path and filesystem permissions")]
33    Io {
34        /// Path being accessed.
35        path: PathBuf,
36        /// Original I/O failure.
37        #[source]
38        source: std::io::Error,
39    },
40    /// Check mode found stale or absent output.
41    #[error("ipc-drift: generated artifacts differ: {paths:?}\nhelp: regenerate and commit all outputs together")]
42    Drift {
43        /// Stale or missing artifacts.
44        paths: Vec<PathBuf>,
45    },
46}
47
48impl GenerateError {
49    /// Stable code suitable for CLI JSON diagnostics.
50    #[must_use]
51    pub fn code(&self) -> &'static str {
52        match self {
53            Self::Schema { code, .. } => code,
54            Self::Tool { .. } => "ipc-tool",
55            Self::Io { .. } => "ipc-io",
56            Self::Drift { .. } => "ipc-drift",
57        }
58    }
59}
60
61#[cold]
62pub(crate) fn schema(
63    code: &'static str,
64    context: impl Into<String>,
65    message: impl Into<String>,
66    help: impl Into<String>,
67) -> GenerateError {
68    GenerateError::Schema {
69        code,
70        context: context.into(),
71        message: message.into(),
72        help: help.into(),
73    }
74}
75
76pub(crate) fn io(path: &std::path::Path, source: std::io::Error) -> GenerateError {
77    GenerateError::Io {
78        path: path.into(),
79        source,
80    }
81}