Skip to content

Commit

Permalink
Return TemplateError on json parse failure
Browse files Browse the repository at this point in the history
  • Loading branch information
nickvollmar committed May 8, 2023
1 parent b813288 commit 21341df
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ impl Template {
Rule::literal => {
// Parse the parameter as a JSON literal
let param_literal = it.next().unwrap();
match param_literal.as_rule() {
let json_result = match param_literal.as_rule() {
Rule::string_literal
if it.peek().unwrap().as_rule() == Rule::string_inner_single_quote =>
{
Expand All @@ -305,9 +305,16 @@ impl Template {
.replace("\\'", "'")
.replace('"', "\\\"")
);
Parameter::Literal(Json::from_str(&double_quoted).unwrap())
Json::from_str(&double_quoted)
}
_ => Parameter::Literal(Json::from_str(param_span.as_str()).unwrap()),
_ => Json::from_str(param_span.as_str()),
};
if let Ok(json) = json_result {
Parameter::Literal(json)
} else {
return Err(TemplateError::of(TemplateErrorReason::InvalidParam(
param_span.as_str().to_owned(),
)));
}
}
Rule::subexpression => {
Expand Down

0 comments on commit 21341df

Please sign in to comment.