Skip to content

Commit 4d81b47

Browse files
authored
Merge pull request #89 from mkantor/better-get-errors
Improve error messages from `get` helper.
2 parents 43e73d6 + 2aebb05 commit 4d81b47

File tree

1 file changed

+51
-40
lines changed
  • src/content/handlebars_helpers

1 file changed

+51
-40
lines changed

src/content/handlebars_helpers/get.rs

Lines changed: 51 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ where
8181
let param_1_as_object = match param_1.map(|value| {
8282
value.as_object().ok_or_else(|| {
8383
handlebars::RenderError::new(format!(
84-
"Custom context provided to the `get` helper must be an object, but it was `{}`.",
85-
value,
84+
"Custom context provided to `get \"{}\"` must be an object, but it was `{}`.",
85+
route, value,
8686
))
8787
})
8888
}) {
8989
Some(Ok(object)) => Some(object),
90-
Some(Err(error)) => { return Err(error) },
90+
Some(Err(error)) => return Err(error),
9191
None => None,
9292
};
9393

@@ -109,15 +109,13 @@ where
109109
});
110110

111111
let content_item = content_engine.get_internal(&route).ok_or_else(|| {
112-
handlebars::RenderError::new(format!(
113-
"No content found at route passed to `get` helper (\"{}\").",
114-
route,
115-
))
112+
handlebars::RenderError::new(format!("No content found for `get \"{}\"`.", route))
116113
})?;
117114

118115
let current_render_data = handlebars_context.data().as_object().ok_or_else(|| {
119116
handlebars::RenderError::new(format!(
120-
"The `get` helper call failed because the context JSON was not an object. It is `{}`.",
117+
"The `get \"{}\"` helper call failed because the immediate render data was not an object (it was `{}`).",
118+
route,
121119
handlebars_context.data(),
122120
))
123121
})?;
@@ -130,8 +128,9 @@ where
130128
serde_json::Value::Object(modified_context_data_as_json_map) => modified_context_data_as_json_map,
131129
_ => {
132130
return Err(handlebars::RenderError::new(format!(
133-
"The `get` helper call failed because the pre-existing handlebars render context was \
131+
"The `get \"{}\"` helper call failed because the pre-existing handlebars render context was \
134132
not an object (it was `{}`).",
133+
route,
135134
modified_context_data_as_json
136135
)))
137136
}
@@ -151,22 +150,21 @@ where
151150
serde_json::Value::Object(modified_context_data_as_json_map),
152151
));
153152

154-
let target_media_type = get_target_media_type(current_render_data)?;
155-
let optional_request_route = get_optional_request_route(current_render_data)?;
156-
let query_parameters = get_query_parameters(current_render_data)?;
157-
let request_headers = get_request_headers(current_render_data)?;
153+
let target_media_type = get_target_media_type(current_render_data, &route)?;
154+
let optional_request_route = get_optional_request_route(current_render_data, &route)?;
155+
let query_parameters = get_query_parameters(current_render_data, &route)?;
156+
let request_headers = get_request_headers(current_render_data, &route)?;
158157

159158
let context = content_engine
160159
.render_context(optional_request_route, query_parameters, request_headers)
161160
.with_handlebars_render_context(handlebars_render_context.clone());
162161

163162
let rendered = content_item
164-
.render(context, &[target_media_type.into_media_range()]).map_err(|render_error| {
163+
.render(context, &[target_media_type.into_media_range()])
164+
.map_err(|render_error| {
165165
handlebars::RenderError::new(format!(
166-
"The `get` helper call failed because the content item being retrieved (\"{}\") \
167-
could not be rendered: {}",
168-
route,
169-
render_error,
166+
"The `get \"{}\"` helper call failed because {} could not be rendered: {}",
167+
route, route, render_error,
170168
))
171169
})?;
172170

@@ -182,8 +180,9 @@ where
182180
))
183181
.map_err(|streaming_error| {
184182
handlebars::RenderError::new(format!(
185-
"The `get` helper call failed because there was an error collecting the rendered content \
186-
for \"{}\": {}",
183+
"The `get \"{}\"` helper call failed because there was an error collecting the rendered content \
184+
for {}: {}",
185+
route,
187186
route,
188187
streaming_error,
189188
))
@@ -197,16 +196,18 @@ where
197196

198197
fn get_target_media_type(
199198
render_data: &serde_json::value::Map<String, serde_json::Value>,
199+
route: &Route,
200200
) -> Result<MediaType, handlebars::RenderError> {
201201
let target_media_type = render_data
202202
.get(TARGET_MEDIA_TYPE_PROPERTY_NAME)
203203
.and_then(|value| value.as_str())
204204
.and_then(|media_type_essence| media_type_essence.parse::<MediaType>().ok())
205205
.ok_or_else(|| {
206206
handlebars::RenderError::new(format!(
207-
"The `get` helper call failed because a valid target media type could not be found \
208-
in the handlebars context. The context JSON must contain a property at `{}` \
209-
whose value is a valid media type essence string.",
207+
"The `get \"{}\"` helper call failed because a valid target media type could not be found \
208+
in the handlebars context. The context JSON must contain a property at `{}` whose value is \
209+
a valid media type essence string.",
210+
route,
210211
TARGET_MEDIA_TYPE_PROPERTY_NAME,
211212
))
212213
})?;
@@ -215,16 +216,18 @@ fn get_target_media_type(
215216

216217
fn get_optional_request_route(
217218
render_data: &serde_json::value::Map<String, serde_json::Value>,
219+
route: &Route,
218220
) -> Result<Option<Route>, handlebars::RenderError> {
219221
let optional_request_route = {
220222
let request_route_value = render_data
221223
.get(REQUEST_DATA_PROPERTY_NAME)
222224
.and_then(|request_data| request_data.get(ROUTE_PROPERTY_NAME))
223225
.ok_or_else(|| {
224226
handlebars::RenderError::new(format!(
225-
"The `get` helper call failed because the request route could not be found \
226-
in the handlebars context. The context JSON must contain a property at `{}.{}` \
227-
whose value is a string or null.",
227+
"The `get \"{}\"` helper call failed because the request route could not be found in the \
228+
handlebars context. The context JSON must contain a property at `{}.{}` whose value is a \
229+
string or null.",
230+
route,
228231
REQUEST_DATA_PROPERTY_NAME, ROUTE_PROPERTY_NAME
229232
))
230233
})?;
@@ -235,15 +238,18 @@ fn get_optional_request_route(
235238
let request_route = request_route_value.as_str()
236239
.ok_or_else(|| {
237240
handlebars::RenderError::new(format!(
238-
"The `get` helper call failed because the request route in the handlebars context was \
241+
"The `get \"{}\"` helper call failed because the request route in the handlebars context was \
239242
not a string or null (it was `{}`).",
243+
route,
240244
request_route_value,
241245
))
242246
})?
243247
.parse::<Route>()
244248
.map_err(|error| {
245249
handlebars::RenderError::new(format!(
246-
"The `get` helper call failed because the request route in the handlebars context was invalid ({}).",
250+
"The `get \"{}\"` helper call failed because the request route in the handlebars context was \
251+
invalid ({}).",
252+
route,
247253
error,
248254
))
249255
})?;
@@ -255,24 +261,26 @@ fn get_optional_request_route(
255261

256262
fn get_query_parameters(
257263
render_data: &serde_json::value::Map<String, serde_json::Value>,
264+
route: &Route,
258265
) -> Result<HashMap<String, String>, handlebars::RenderError> {
259266
let query_parameters = render_data
260267
.get(REQUEST_DATA_PROPERTY_NAME)
261268
.and_then(|request_data| request_data.get(QUERY_PARAMETERS_PROPERTY_NAME))
262269
.ok_or_else(|| {
263270
handlebars::RenderError::new(format!(
264-
"The `get` helper call failed because the query parameters could not be found \
265-
in the handlebars context. The context JSON must contain a property at `{}.{}` \
266-
whose value is a map.",
271+
"The `get \"{}\"` helper call failed because the query parameters could not be found in the \
272+
handlebars context. The context JSON must contain a property at `{}.{}` whose value is a map.",
273+
route,
267274
REQUEST_DATA_PROPERTY_NAME,
268275
QUERY_PARAMETERS_PROPERTY_NAME,
269276
))
270277
})?
271278
.as_object()
272279
.ok_or_else(|| {
273-
handlebars::RenderError::new(String::from(
274-
"The `get` helper call failed because the query parameters in the handlebars context \
275-
was not a map."
280+
handlebars::RenderError::new(format!(
281+
"The `get \"{}\"` helper call failed because the query parameters in the handlebars context \
282+
was not a map.",
283+
route,
276284
))
277285
})?
278286
.into_iter()
@@ -287,22 +295,25 @@ fn get_query_parameters(
287295

288296
fn get_request_headers(
289297
render_data: &serde_json::value::Map<String, serde_json::Value>,
298+
route: &Route,
290299
) -> Result<HashMap<String, String>, handlebars::RenderError> {
291300
let request_headers = render_data
292301
.get(REQUEST_DATA_PROPERTY_NAME)
293302
.and_then(|request_data| request_data.get(REQUEST_HEADERS_PROPERTY_NAME))
294303
.ok_or_else(|| {
295304
handlebars::RenderError::new(format!(
296-
"The `get` helper call failed because the request headers could not be found \
297-
in the handlebars context. The context JSON must contain a property at `{}.{}` \
298-
whose value is a map.",
305+
"The `get \"{}\"` helper call failed because the request headers could not be found in \
306+
the handlebars context. The context JSON must contain a property at `{}.{}` whose value \
307+
is a map.",
308+
route,
299309
REQUEST_DATA_PROPERTY_NAME,
300310
REQUEST_HEADERS_PROPERTY_NAME
301311
))
302312
})?.as_object().ok_or_else(|| {
303-
handlebars::RenderError::new(String::from(
304-
"The `get` helper call failed because the request headers in the handlebars context \
305-
was not a map."
313+
handlebars::RenderError::new(format!(
314+
"The `get \"{}\"` helper call failed because the request headers in the handlebars context \
315+
was not a map.",
316+
route,
306317
))
307318
})?
308319
.into_iter()

0 commit comments

Comments
 (0)