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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#![allow(missing_docs)]
use crate::i18n::TranslationsManagerError;
use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
ClientError(#[from] ClientError),
#[error(transparent)]
ServerError(#[from] ServerError),
}
#[derive(Error, Debug)]
pub enum ClientError {
#[error("locale '{locale}' is not supported")]
LocaleNotSupported { locale: String },
#[error("the following error occurred while interfacing with JavaScript: {0}")]
Js(String),
#[error(transparent)]
FetchError(#[from] FetchError),
#[error("invalid frozen state provided")]
ThawFailed {
#[source]
source: serde_json::Error,
},
#[error("tried to deserialize invalid state")]
StateInvalid {
#[source]
source: serde_json::Error,
},
}
#[derive(Error, Debug)]
pub enum ServerError {
#[error("render function '{fn_name}' in template '{template_name}' failed (cause: {cause:?})")]
RenderFnFailed {
fn_name: String,
template_name: String,
cause: ErrorCause,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error(transparent)]
GlobalStateError(#[from] GlobalStateError),
#[error(transparent)]
StoreError(#[from] StoreError),
#[error(transparent)]
TranslationsManagerError(#[from] TranslationsManagerError),
#[error(transparent)]
BuildError(#[from] BuildError),
#[error(transparent)]
ExportError(#[from] ExportError),
#[error(transparent)]
ServeError(#[from] ServeError),
}
pub fn err_to_status_code(err: &ServerError) -> u16 {
match err {
ServerError::ServeError(ServeError::PageNotFound { .. }) => 404,
ServerError::RenderFnFailed { cause, .. } => match cause {
ErrorCause::Client(code) => code.unwrap_or(400),
ErrorCause::Server(code) => code.unwrap_or(500),
},
_ => 500,
}
}
#[derive(Error, Debug)]
pub enum GlobalStateError {
#[error("couldn't generate global state at build time")]
BuildGenerationFailed {
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
}
#[derive(Error, Debug)]
pub enum StoreError {
#[error("asset '{name}' not found in store")]
NotFound { name: String },
#[error("asset '{name}' couldn't be read from store")]
ReadFailed {
name: String,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
#[error("asset '{name}' couldn't be written to store")]
WriteFailed {
name: String,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
}
#[derive(Error, Debug)]
pub enum FetchError {
#[error("asset fetched from '{url}' wasn't a string")]
NotString { url: String },
#[error("asset fetched from '{url}' returned status code '{status}' (expected 200)")]
NotOk {
url: String,
status: u16,
err: String,
},
#[error("asset fetched from '{url}' couldn't be serialized")]
SerFailed {
url: String,
#[source]
source: Box<dyn std::error::Error + Send + Sync>,
},
}
#[derive(Error, Debug)]
pub enum BuildError {
#[error("template '{template_name}' is missing feature '{feature_name}' (required due to its properties)")]
TemplateFeatureNotEnabled {
template_name: String,
feature_name: String,
},
#[error("html shell couldn't be found at '{path}'")]
HtmlShellNotFound {
path: String,
#[source]
source: std::io::Error,
},
#[error(
"invalid indicator '{indicator}' in time string (must be one of: s, m, h, d, w, M, y)"
)]
InvalidDatetimeIntervalIndicator { indicator: String },
#[error("asset 'render_cfg.json' invalid or corrupted (try cleaning all assets)")]
RenderCfgInvalid {
#[from]
source: serde_json::Error,
},
}
#[derive(Error, Debug)]
pub enum ExportError {
#[error("template '{template_name}' can't be exported because it depends on strategies that can't be run at build-time (only build state and build paths can be use din exportable templates)")]
TemplateNotExportable { template_name: String },
#[error("template '{template_name}' wasn't found in built artifacts (run `perseus clean --dist` if this persists)")]
TemplateNotFound { template_name: String },
}
#[derive(Error, Debug)]
pub enum ServeError {
#[error("page at '{path}' not found")]
PageNotFound { path: String },
#[error("both build and request states were defined for a template when only one or fewer were expected (should it be able to amalgamate states?)")]
BothStatesDefined,
#[error("couldn't parse revalidation datetime (try cleaning all assets)")]
BadRevalidate {
#[from]
source: chrono::ParseError,
},
}
#[derive(Debug)]
pub enum ErrorCause {
Client(Option<u16>),
Server(Option<u16>),
}
#[derive(Debug)]
pub struct GenericErrorWithCause {
pub error: Box<dyn std::error::Error + Send + Sync>,
pub cause: ErrorCause,
}
impl<E: std::error::Error + Send + Sync + 'static> From<E> for GenericErrorWithCause {
fn from(error: E) -> Self {
Self {
error: error.into(),
cause: ErrorCause::Server(None),
}
}
}
#[macro_export]
macro_rules! blame_err {
(client, $err:expr) => {
return ::std::result::Result::Err(::perseus::GenericErrorWithCause {
error: $err.into(),
cause: $crate::ErrorCause::Client(::std::option::Option::None),
})
};
(client, $code:literal, $err:expr) => {
return ::std::result::Result::Err(::perseus::GenericErrorWithCause {
error: $err.into(),
cause: $crate::ErrorCause::Client(::std::option::Option::Some($code)),
})
};
(server, $err:expr) => {
return ::std::result::Result::Err(::perseus::GenericErrorWithCause {
error: $err.into(),
cause: $crate::ErrorCause::Server(::std::option::Option::None),
})
};
(server, $code:literal, $err:expr) => {
return ::std::result::Result::Err(::perseus::GenericErrorWithCause {
error: $err.into(),
cause: $crate::ErrorCause::Server(::std::option::Option::Some($code)),
})
};
}