Skip to main content

webui_tokens/
error.rs

1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3
4use thiserror::Error;
5
6/// Errors that can occur during token loading, resolution, or CSS generation.
7#[derive(Debug, Error)]
8pub enum TokenError {
9    /// Token file could not be read from disk.
10    #[error("Failed to read token file {path}: {source}")]
11    Io {
12        path: String,
13        source: std::io::Error,
14    },
15
16    /// Token file contained invalid JSON.
17    #[error("Invalid token JSON in {path}: {source}")]
18    InvalidJson {
19        path: String,
20        source: serde_json::Error,
21    },
22
23    /// Token file schema is invalid (e.g., missing `themes` key, wrong types).
24    #[error("Invalid token file schema: {0}")]
25    Schema(String),
26
27    /// A flat token required by a caller is missing from a theme.
28    #[error(
29        "Token --{token} required by caller but not found in theme '{theme}'. Add --{token} to the theme or remove it from the required list."
30    )]
31    MissingToken {
32        /// Theme that is missing the token.
33        theme: String,
34        /// Token name without the `--` prefix.
35        token: String,
36    },
37}
38
39impl TokenError {
40    /// Create an IO error with the file path context.
41    pub(crate) fn io(path: &std::path::Path, source: std::io::Error) -> Self {
42        Self::Io {
43            path: path.display().to_string(),
44            source,
45        }
46    }
47
48    /// Create a JSON parse error with the file path context.
49    pub(crate) fn json(path: &std::path::Path, source: serde_json::Error) -> Self {
50        Self::InvalidJson {
51            path: path.display().to_string(),
52            source,
53        }
54    }
55}
56
57/// Result type alias for token operations.
58pub type Result<T> = std::result::Result<T, TokenError>;