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 /// CSS error.
38 #[error("CSS error: {0}")]
39 Css(String),
40
41 /// HTML error.
42 #[error("HTML error: {0}")]
43 Html(String),
44
45 /// Directive error.
46 #[error("Directive error: {0}")]
47 Directive(String),
48
49 /// Entity not found error.
50 #[error("Not found: {0}")]
51 NotFound(String),
52
53 /// TypeScript parse error.
54 #[error("TypeScript parse error: {0}")]
55 TsParseError(String),
56
57 /// A build-time template-authoring mistake (e.g. an invalid `@event`
58 /// handler or a non-braced `w-ref`). Carries a structured [`Diagnostic`]
59 /// whose [`std::fmt::Display`] is an actionable, color-free report. The CLI
60 /// colorizes it; FFI/Node/WASM forward the plain text to their host error
61 /// channel so the application can handle it.
62 ///
63 /// The diagnostic is boxed so this cold error path does not enlarge
64 /// `Result<_, ParserError>` on the common success path.
65 #[error("{0}")]
66 Template(Box<Diagnostic>),
67}
68
69impl From<Diagnostic> for ParserError {
70 fn from(diagnostic: Diagnostic) -> Self {
71 ParserError::Template(Box::new(diagnostic))
72 }
73}