webui_parser/error.rs
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT license.
3
4//! Error handling for the WebUI parser.
5
6use crate::diagnostic::Diagnostic;
7use thiserror::Error;
8
9/// Result type for WebUI parser operations.
10pub type Result<T> = std::result::Result<T, ParserError>;
11
12/// Error type for WebUI parser.
13#[derive(Debug, Error)]
14pub enum ParserError {
15 /// Generic error.
16 #[error("Generic error: {0}")]
17 Generic(String),
18
19 /// I/O error with context.
20 #[error("I/O error: {context}")]
21 IO {
22 /// What operation failed.
23 context: String,
24 /// The underlying I/O error.
25 #[source]
26 source: std::io::Error,
27 },
28
29 /// Parse error.
30 #[error("Parse error: {0}")]
31 Parse(String),
32
33 /// Component error.
34 #[error("Component error: {0}")]
35 Component(String),
36
37 /// A component supplied its own `<template>` wrapper under
38 /// `CssStrategy::Module` but did not declare a
39 /// `shadowrootadoptedstylesheets` attribute. The framework cannot
40 /// silently inject the attribute without overriding developer intent
41 /// on the wrapper, so authoring help is surfaced at build time.
42 #[error(
43 "Component <{tag}> supplies its own <template> wrapper under --css=module but is \
44 missing the `shadowrootadoptedstylesheets` attribute, which is required to wire \
45 the component's CSS module into its shadow root. Add \
46 `shadowrootadoptedstylesheets=\"{tag}\"` to the <template> tag, or remove the \
47 <template> wrapper to let the framework manage it automatically."
48 )]
49 MissingAdoptedStylesheets {
50 /// The component tag name (e.g. `mp-app`).
51 tag: String,
52 },
53
54 /// CSS error.
55 #[error("CSS error: {0}")]
56 Css(String),
57
58 /// HTML error.
59 #[error("HTML error: {0}")]
60 Html(String),
61
62 /// Directive error.
63 #[error("Directive error: {0}")]
64 Directive(String),
65
66 /// Entity not found error.
67 #[error("Not found: {0}")]
68 NotFound(String),
69
70 /// TypeScript parse error.
71 #[error("TypeScript parse error: {0}")]
72 TsParseError(String),
73
74 /// A build-time template-authoring mistake (e.g. an invalid `@event`
75 /// handler or a non-braced `w-ref`). Carries a structured [`Diagnostic`]
76 /// whose [`std::fmt::Display`] is an actionable, color-free report. The CLI
77 /// colorizes it; FFI/Node/WASM forward the plain text to their host error
78 /// channel so the application can handle it.
79 ///
80 /// The diagnostic is boxed so this cold error path does not enlarge
81 /// `Result<_, ParserError>` on the common success path.
82 #[error("{0}")]
83 Template(Box<Diagnostic>),
84}
85
86impl From<Diagnostic> for ParserError {
87 fn from(diagnostic: Diagnostic) -> Self {
88 ParserError::Template(Box::new(diagnostic))
89 }
90}