@@ -2,7 +2,7 @@ use crate::templates::SplitTemplate;
2
2
use crate :: AppState ;
3
3
use actix_web:: http:: StatusCode ;
4
4
use actix_web:: HttpResponseBuilder ;
5
- use anyhow:: { format_err, Context as AnyhowContext } ;
5
+ use anyhow:: { bail , format_err, Context as AnyhowContext } ;
6
6
use async_recursion:: async_recursion;
7
7
use handlebars:: { BlockContext , Context , JsonValue , RenderError , Renderable } ;
8
8
use serde:: Serialize ;
@@ -135,7 +135,9 @@ impl<W: std::io::Write> RenderContext<W> {
135
135
let current_component = SplitTemplateRenderer :: name ( & self . current_component ) ;
136
136
match ( current_component, new_component) {
137
137
( _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
+ } ) ?;
139
141
}
140
142
( _current_component, Some ( new_component) ) => {
141
143
self . open_component_with_data ( new_component, & data) . await ?;
@@ -152,20 +154,24 @@ impl<W: std::io::Write> RenderContext<W> {
152
154
self . recursion_depth <= MAX_RECURSION_DEPTH ,
153
155
"Maximum recursion depth exceeded in the dynamic component."
154
156
) ;
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
+ } ;
169
175
for p in properties {
170
176
self . recursion_depth += 1 ;
171
177
let res = self . handle_row ( & p) . await ;
0 commit comments