Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
 
Improved error message on parameters decode and "" case. (#222)

* Improved error message on decode and "" case.

* Add error handling to other eldritch_run calls.

* Fix test and update imix -> eldritch.
  • Loading branch information
hulto authored Jun 22, 2023
1 parent 2e745d2 commit bb6712b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions implants/eldritch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,38 @@ impl PrintHandler for StdPrintHandler {

pub fn eldritch_run(tome_filename: String, tome_contents: String, tome_parameters: Option<String>, print_handler: &(dyn PrintHandler)) -> anyhow::Result<String> {
// Boilder plate
let ast: AstModule;
match AstModule::parse(
let ast = match AstModule::parse(
&tome_filename,
tome_contents.as_str().to_owned(),
&Dialect::Standard
) {
Ok(res) => ast = res,
Err(err) => return Err(err),
}
Ok(res) => res,
Err(err) => return Err(anyhow::anyhow!("[eldritch] Unable to parse eldritch tome: {}: {} {}", err.to_string(), tome_filename.as_str(), tome_contents.as_str())),
};

let tome_params_str: String = match tome_parameters {
Some(param_string) => param_string,
Some(local_param_string) => match local_param_string.as_str() {
"" => "{}".to_string(), // If we get "" as our params update it to "{}"
_ => local_param_string // Otherwise return our string.
},
None => "{}".to_string(),
};

let globals = get_eldritch()?;
let globals = match get_eldritch() {
Ok(local_globals) => local_globals,
Err(local_error) => return Err(anyhow::anyhow!("[eldritch] Failed to get_eldritch globals: {}", local_error.to_string())),
};

let module: Module = Module::new();

let res: SmallMap<Value, Value> = SmallMap::new();
let mut input_params: Dict = Dict::new(res);

let parsed: serde_json::Value = serde_json::from_str(&tome_params_str)?;
let parsed: serde_json::Value = match serde_json::from_str(&tome_params_str){
Ok(local_value) => local_value,
Err(local_err) => return Err(anyhow::anyhow!("[eldritch] Error decoding tome_params to JSON: {}: {}", local_err.to_string(), tome_params_str)),
};

let param_map: serde_json::Map<String, serde_json::Value> = match parsed.as_object() {
Some(tmp_param_map) => tmp_param_map.clone(),
None => Map::new(),
Expand Down Expand Up @@ -125,7 +134,10 @@ pub fn eldritch_run(tome_filename: String, tome_contents: String, tome_parameter
};
new_value = Value::new_int(tmp_value);
}
let hashed_key = new_key.to_value().get_hashed()?;
let hashed_key = match new_key.to_value().get_hashed() {
Ok(local_hashed_key) => local_hashed_key,
Err(local_error) => return Err(anyhow::anyhow!("[eldritch] Failed to create hashed key for key {}: {}", new_key.to_string(), local_error.to_string())),
};
input_params.insert_hashed(hashed_key, new_value);
}

Expand All @@ -136,7 +148,7 @@ pub fn eldritch_run(tome_filename: String, tome_contents: String, tome_parameter

let res: Value = match eval.eval_module(ast, &globals) {
Ok(eval_val) => eval_val,
Err(eval_error) => return Err(anyhow::anyhow!("Eldritch eval_module failed:\n{}", eval_error)),
Err(eval_error) => return Err(anyhow::anyhow!("[eldritch] Eldritch eval_module failed:\n{}", eval_error)),
};

Ok(res.to_str())
Expand Down
2 changes: 1 addition & 1 deletion implants/golem/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ fn test_golem_main_syntax_fail() -> anyhow::Result<()> {
cmd.arg("../../tests/golem_cli_test/syntax_fail.tome");
cmd.assert()
.failure()
.stderr(predicate::str::contains("[TASK ERROR] ../../tests/golem_cli_test/syntax_fail.tome: error: Parse error: unexpected string literal 'win' here"));
.stderr(predicate::str::contains("[TASK ERROR] ../../tests/golem_cli_test/syntax_fail.tome: [eldritch] Unable to parse eldritch tome: error: Parse error: unexpected string literal \'win\' here"));

Ok(())
}
Expand Down

0 comments on commit bb6712b

Please sign in to comment.