rustysan_sword/error.rs
1use std::path::PathBuf;
2
3/// A Sword lookup, syntax, or rendering failure.
4#[derive(Debug, thiserror::Error)]
5#[non_exhaustive]
6pub enum Error {
7 /// The view name is empty or contains a path separator or traversal.
8 #[error("invalid view name: {0}")]
9 InvalidName(String),
10 /// No template root contains the view.
11 #[error("view not found: {0}")]
12 NotFound(String),
13 /// A template file could not be read.
14 #[error("failed reading {path}: {source}")]
15 Io {
16 /// The file that could not be read.
17 path: PathBuf,
18 /// The underlying I/O error.
19 #[source]
20 source: std::io::Error,
21 },
22 /// The template source is not valid Sword.
23 #[error("{template}:{line}:{column}: {message}")]
24 Syntax {
25 /// The logical template name.
26 template: String,
27 /// The one-based line in the original source.
28 line: usize,
29 /// The one-based column in the original source.
30 column: usize,
31 /// What is wrong.
32 message: String,
33 },
34 /// Rendering failed at a known line of the original source.
35 #[error("{template}:{line}: {message}")]
36 Render {
37 /// The logical template name.
38 template: String,
39 /// The one-based line in the original source.
40 line: usize,
41 /// What went wrong; it may include the file, column and call sites.
42 message: String,
43 },
44 /// A backend error whose original line is unavailable.
45 #[error("{template}: {message}")]
46 RenderUnlocated {
47 /// The logical template name.
48 template: String,
49 /// What went wrong.
50 message: String,
51 },
52 /// The render succeeded but never reached an `@fragment` with the requested name.
53 #[error("{template}: fragment `{fragment}` was not rendered")]
54 FragmentNotFound {
55 /// The logical template name.
56 template: String,
57 /// The requested fragment.
58 fragment: String,
59 },
60 /// Templates include or extend each other in a cycle.
61 #[error("template dependency cycle: {0}")]
62 Cycle(String),
63 /// A chain of includes, layouts and components is more than 64 templates deep.
64 #[error("template dependency depth exceeds 64 at {0}")]
65 DependencyDepth(String),
66 /// View data holds a number that cannot be represented.
67 #[error("view data contains a number outside the supported range: {0}")]
68 Number(String),
69 /// View data could not be serialized.
70 #[error("could not serialize view data: {0}")]
71 Data(#[from] serde_json::Error),
72}
73
74impl Error {
75 /// A short, data-free label for logs and metrics. It never contains template text,
76 /// paths, or request data.
77 pub(crate) fn kind(&self) -> &'static str {
78 match self {
79 Error::InvalidName(_) => "invalid_name",
80 Error::NotFound(_) => "not_found",
81 Error::Io { .. } => "io",
82 Error::Syntax { .. } => "syntax",
83 Error::Render { .. } | Error::RenderUnlocated { .. } => "render",
84 Error::FragmentNotFound { .. } => "fragment_not_found",
85 Error::Cycle(_) => "cycle",
86 Error::DependencyDepth(_) => "dependency_depth",
87 Error::Number(_) | Error::Data(_) => "data",
88 }
89 }
90}