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