Skip to content

Commit 5b3c848

Browse files
committed
improve dynamic component
1 parent 0cc7a7d commit 5b3c848

File tree

1 file changed

+22
-16
lines changed

1 file changed

+22
-16
lines changed

src/render.rs

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::templates::SplitTemplate;
22
use crate::AppState;
33
use actix_web::http::StatusCode;
44
use actix_web::HttpResponseBuilder;
5-
use anyhow::{format_err, Context as AnyhowContext};
5+
use anyhow::{bail, format_err, Context as AnyhowContext};
66
use async_recursion::async_recursion;
77
use handlebars::{BlockContext, Context, JsonValue, RenderError, Renderable};
88
use serde::Serialize;
@@ -135,7 +135,9 @@ impl<W: std::io::Write> RenderContext<W> {
135135
let current_component = SplitTemplateRenderer::name(&self.current_component);
136136
match (current_component, new_component) {
137137
(_current_component, Some("dynamic")) => {
138-
self.render_dynamic(data).await?;
138+
self.render_dynamic(data).await.with_context(|| {
139+
format!("Unable to render dynamic component with properties {data}")
140+
})?;
139141
}
140142
(_current_component, Some(new_component)) => {
141143
self.open_component_with_data(new_component, &data).await?;
@@ -152,20 +154,24 @@ impl<W: std::io::Write> RenderContext<W> {
152154
self.recursion_depth <= MAX_RECURSION_DEPTH,
153155
"Maximum recursion depth exceeded in the dynamic component."
154156
);
155-
let properties: Vec<Cow<JsonValue>> = data
156-
.get("properties")
157-
.and_then(|props| match props {
158-
Value::String(s) => match serde_json::from_str::<JsonValue>(s).ok()? {
159-
Value::Array(values) => Some(values.into_iter().map(Cow::Owned).collect()),
160-
obj @ Value::Object(_) => Some(vec![Cow::Owned(obj)]),
161-
_ => None,
162-
},
163-
obj @ Value::Object(_) => Some(vec![Cow::Borrowed(obj)]),
164-
_ => None,
165-
})
166-
.context(
167-
"The dynamic component requires a parameter called 'properties' that is a json ",
168-
)?;
157+
let properties_key = "properties";
158+
let properties_obj = data
159+
.get(properties_key)
160+
.with_context(|| format!("Missing '{properties_key}' key."))?;
161+
let properties: Vec<Cow<JsonValue>> = match properties_obj {
162+
Value::String(s) => match serde_json::from_str::<JsonValue>(s)
163+
.with_context(|| "parsing json properties")?
164+
{
165+
Value::Array(values) => values.into_iter().map(Cow::Owned).collect(),
166+
obj @ Value::Object(_) => vec![Cow::Owned(obj)],
167+
other => bail!(
168+
"Expected properties string to parse as array or object, got {other} instead."
169+
),
170+
},
171+
obj @ Value::Object(_) => vec![Cow::Borrowed(obj)],
172+
Value::Array(values) => values.into_iter().map(Cow::Borrowed).collect(),
173+
other => bail!("Expected properties of type array or object, got {other} instead."),
174+
};
169175
for p in properties {
170176
self.recursion_depth += 1;
171177
let res = self.handle_row(&p).await;

0 commit comments

Comments
 (0)