xsd_schema/compose/error.rs
1//! Everything that can go wrong while composing a document.
2
3use std::io;
4
5use crate::document::{BufferDocumentError, CopyError, SerializeError};
6use crate::xpath::XPathError;
7
8/// Why a composition step failed.
9///
10/// Every variant either wraps an error from the layer that produced it —
11/// the XPath engine, the document builder, the subtree copy, the serializer,
12/// the file system — or reports a mistake in the composition itself: a prefix
13/// nothing binds, a name that is not an XML name, a value of the wrong shape.
14///
15/// `From` conversions exist for each wrapped error, so `?` works throughout a
16/// composition body. The two variants that carry context —
17/// [`XPath`](ComposeError::XPath) and [`Copy`](ComposeError::Copy) — get it
18/// filled in by the code that has it: the conversion leaves the expression
19/// text and the form path empty, and
20/// [`Composer::eval`](crate::compose::Composer::eval) and the emitter set
21/// them.
22///
23/// ```
24/// use xsd_schema::compose::ComposeError;
25///
26/// let err = ComposeError::NotSingleton { len: 3 };
27/// assert!(err.to_string().contains('3'));
28/// ```
29#[derive(Debug, thiserror::Error)]
30pub enum ComposeError {
31 /// An expression could not be compiled or evaluated.
32 ///
33 /// `expr` is the expression text, so a failure names the call site rather
34 /// than just the error code.
35 ///
36 /// ```
37 /// use bumpalo::Bump;
38 /// use xsd_schema::compose::{ComposeError, Composer};
39 /// use xsd_schema::namespace::NameTable;
40 ///
41 /// let arena = Bump::new();
42 /// let names = NameTable::new();
43 /// let c = Composer::new(&arena, &names);
44 ///
45 /// // `$missing` was never declared.
46 /// match c.eval("$missing", &[], None, Vec::new()) {
47 /// Err(ComposeError::XPath { expr, .. }) => assert_eq!(expr, "$missing"),
48 /// other => panic!("expected a refusal, got {other:?}"),
49 /// }
50 /// ```
51 #[error("the expression {expr:?} failed: {source}")]
52 XPath {
53 /// What the engine reported.
54 #[source]
55 source: XPathError,
56 /// The expression text, empty when the error was converted with `?`
57 /// before reaching a call site that knows it.
58 expr: String,
59 },
60
61 /// The document builder refused a node, or a document could not be parsed.
62 #[error("the document could not be built: {0}")]
63 Document(#[from] BufferDocumentError),
64
65 /// A spliced value could not be copied into the result.
66 ///
67 /// `at` is the path of the form whose content failed, spelled like an
68 /// XPath step list — `result/item_tuple[3]`.
69 #[error("the content of {at} could not be copied: {source}")]
70 Copy {
71 /// What the copy reported.
72 #[source]
73 source: CopyError,
74 /// The failing form's path, empty when the error was converted with
75 /// `?` outside the emitter.
76 at: String,
77 },
78
79 /// The result could not be written as XML.
80 #[error("the document could not be written as XML: {0}")]
81 Serialize(#[from] SerializeError),
82
83 /// A file could not be read, or a writer failed.
84 #[error("input/output failed: {0}")]
85 Io(#[from] io::Error),
86
87 /// A form used a prefix that neither the form nor the composer binds.
88 #[error("no namespace is bound to the prefix {prefix:?}, used at {at}")]
89 UnboundPrefix {
90 /// The unbound prefix.
91 prefix: String,
92 /// The path of the form that used it.
93 at: String,
94 },
95
96 /// A literal name is not an XML `NCName` or `prefix:NCName`.
97 #[error("{0:?} is not an XML name")]
98 InvalidName(String),
99
100 /// An atomic value turned up where a node was required.
101 #[error("an atomic value appeared where a node was required")]
102 NotANode,
103
104 /// A sequence of several items turned up where one was required.
105 #[error("{len} items appeared where a single item was required")]
106 NotSingleton {
107 /// How many items there were.
108 len: usize,
109 },
110
111 /// [`Composer::build`](crate::compose::Composer::build) was given
112 /// something other than exactly one top-level element.
113 ///
114 /// Use [`Composer::build_sequence`](crate::compose::Composer::build_sequence)
115 /// for a result with none, or several.
116 #[error("a document needs exactly one top-level element, this build has {top_level}")]
117 BuildShape {
118 /// How many top-level elements the build would produce.
119 top_level: usize,
120 },
121}
122
123impl From<XPathError> for ComposeError {
124 /// Wraps an engine error with no expression text; the evaluating call site
125 /// attaches it.
126 fn from(source: XPathError) -> Self {
127 Self::XPath {
128 source,
129 expr: String::new(),
130 }
131 }
132}
133
134impl From<CopyError> for ComposeError {
135 /// Wraps a copy error with no form path; the emitter attaches it.
136 fn from(source: CopyError) -> Self {
137 Self::Copy {
138 source,
139 at: String::new(),
140 }
141 }
142}
143
144#[cfg(test)]
145mod tests {
146 use super::*;
147
148 #[test]
149 fn xpath_conversion_leaves_the_expression_text_empty() {
150 let err: ComposeError = XPathError::more_than_one_item().into();
151 match err {
152 ComposeError::XPath { expr, .. } => assert!(expr.is_empty()),
153 other => panic!("expected an XPath error, got {other:?}"),
154 }
155 }
156
157 #[test]
158 fn copy_conversion_leaves_the_path_empty() {
159 let err: ComposeError = CopyError::Unsupported("nope").into();
160 match err {
161 ComposeError::Copy { at, .. } => assert!(at.is_empty()),
162 other => panic!("expected a copy error, got {other:?}"),
163 }
164 }
165
166 #[test]
167 fn messages_name_the_offending_value() {
168 assert!(ComposeError::NotSingleton { len: 2 }
169 .to_string()
170 .contains('2'));
171 assert!(ComposeError::BuildShape { top_level: 0 }
172 .to_string()
173 .contains('0'));
174 assert!(ComposeError::InvalidName("a b".to_string())
175 .to_string()
176 .contains("a b"));
177 assert!(ComposeError::UnboundPrefix {
178 prefix: "p".to_string(),
179 at: "result/item".to_string(),
180 }
181 .to_string()
182 .contains("result/item"));
183 }
184}