refactor: consolidate recipeError (#6758)

This commit is contained in:
Lifei Zhou
2026-01-28 11:54:43 +11:00
committed by GitHub
parent d0e8112ac7
commit cee4b1bae6
3 changed files with 23 additions and 25 deletions
+6 -8
View File
@@ -13,10 +13,8 @@ use std::path::Path;
pub enum RecipeError { pub enum RecipeError {
#[error("Missing required parameters: {parameters:?}")] #[error("Missing required parameters: {parameters:?}")]
MissingParams { parameters: Vec<String> }, MissingParams { parameters: Vec<String> },
#[error("Template rendering failed: {source}")] #[error("Invalid recipe: {source}")]
TemplateRendering { source: anyhow::Error }, Invalid { source: anyhow::Error },
#[error("Recipe parsing failed: {source}")]
RecipeParsing { source: anyhow::Error },
} }
fn render_recipe_template<F>( fn render_recipe_template<F>(
@@ -57,7 +55,7 @@ where
{ {
let (rendered_content, missing_params) = let (rendered_content, missing_params) =
render_recipe_template(recipe_content, recipe_dir, params.clone(), user_prompt_fn) render_recipe_template(recipe_content, recipe_dir, params.clone(), user_prompt_fn)
.map_err(|source| RecipeError::TemplateRendering { source })?; .map_err(|source| RecipeError::Invalid { source })?;
if !missing_params.is_empty() { if !missing_params.is_empty() {
return Err(RecipeError::MissingParams { return Err(RecipeError::MissingParams {
@@ -66,7 +64,7 @@ where
} }
let mut recipe = Recipe::from_content(&rendered_content) let mut recipe = Recipe::from_content(&rendered_content)
.map_err(|source| RecipeError::RecipeParsing { source })?; .map_err(|source| RecipeError::Invalid { source })?;
if let Some(ref mut sub_recipes) = recipe.sub_recipes { if let Some(ref mut sub_recipes) = recipe.sub_recipes {
for sub_recipe in sub_recipes { for sub_recipe in sub_recipes {
@@ -90,7 +88,7 @@ where
let recipe_parameters = let recipe_parameters =
validate_recipe_template_from_content(&recipe_content, Some(recipe_dir_str.clone())) validate_recipe_template_from_content(&recipe_content, Some(recipe_dir_str.clone()))
.map_err(|source| RecipeError::TemplateRendering { source })? .map_err(|source| RecipeError::Invalid { source })?
.parameters; .parameters;
let param_pairs: Vec<(String, String)> = if let Some(recipe_params) = &recipe_parameters { let param_pairs: Vec<(String, String)> = if let Some(recipe_params) = &recipe_parameters {
@@ -165,7 +163,7 @@ fn resolve_sub_recipe_path(
parent_recipe_dir.join(sub_recipe_path) parent_recipe_dir.join(sub_recipe_path)
}; };
if !path.exists() { if !path.exists() {
return Err(RecipeError::RecipeParsing { return Err(RecipeError::Invalid {
source: anyhow::anyhow!("Sub-recipe file does not exist: {}", path.display()), source: anyhow::anyhow!("Sub-recipe file does not exist: {}", path.display()),
}); });
} }
+16 -16
View File
@@ -173,14 +173,14 @@ fn test_build_recipe_from_template_wrong_parameters_in_recipe_file() {
println!("{}", err); println!("{}", err);
match err { match err {
RecipeError::TemplateRendering { source } => { RecipeError::Invalid { source } => {
let err_str = source.to_string(); let err_str = source.to_string();
assert!(err_str.contains("Unnecessary parameter definitions: wrong_param_key.")); assert!(err_str.contains("Unnecessary parameter definitions: wrong_param_key."));
assert!(err_str.contains("Missing definitions for parameters in the recipe file:")); assert!(err_str.contains("Missing definitions for parameters in the recipe file:"));
assert!(err_str.contains("expected_param1")); assert!(err_str.contains("expected_param1"));
assert!(err_str.contains("expected_param2")); assert!(err_str.contains("expected_param2"));
} }
_ => panic!("Expected TemplateRendering error"), _ => panic!("Expected Invalid error"),
} }
} }
@@ -260,10 +260,10 @@ fn test_build_recipe_from_template_optional_parameters_without_default_values_in
let err = build_recipe_result.unwrap_err(); let err = build_recipe_result.unwrap_err();
println!("{}", err); println!("{}", err);
match err { match err {
RecipeError::TemplateRendering { source } => { RecipeError::Invalid { source } => {
assert!(source.to_string().to_lowercase().contains("missing")); assert!(source.to_string().to_lowercase().contains("missing"));
} }
_ => panic!("Expected TemplateRendering error"), _ => panic!("Expected Invalid error"),
} }
} }
@@ -287,12 +287,12 @@ fn test_build_recipe_from_template_wrong_input_type_in_recipe_file() {
assert!(build_recipe_result.is_err()); assert!(build_recipe_result.is_err());
let err = build_recipe_result.unwrap_err(); let err = build_recipe_result.unwrap_err();
match err { match err {
RecipeError::TemplateRendering { source } => { RecipeError::Invalid { source } => {
let err_msg = source.to_string(); let err_msg = source.to_string();
eprint!("Error: {}", err_msg); eprint!("Error: {}", err_msg);
assert!(err_msg.contains("unknown variant `some_invalid_type`")); assert!(err_msg.contains("unknown variant `some_invalid_type`"));
} }
_ => panic!("Expected TemplateRendering error, got: {:?}", err), _ => panic!("Expected Invalid error, got: {:?}", err),
} }
} }
@@ -322,13 +322,13 @@ fn test_build_recipe_from_template_missing_prompt_and_instructions() {
println!("{}", err); println!("{}", err);
match err { match err {
RecipeError::TemplateRendering { source } => { RecipeError::Invalid { source } => {
let err_str = source.to_string(); let err_str = source.to_string();
assert!( assert!(
err_str.contains("Recipe must specify at least one of `instructions` or `prompt`.") err_str.contains("Recipe must specify at least one of `instructions` or `prompt`.")
); );
} }
_ => panic!("Expected TemplateRendering error"), _ => panic!("Expected Invalid error"),
} }
} }
@@ -478,12 +478,12 @@ instructions: Absolute instructions"#;
assert!(result.is_err()); assert!(result.is_err());
match result { match result {
Err(RecipeError::RecipeParsing { source }) => { Err(RecipeError::Invalid { source }) => {
let error_msg = source.to_string(); let error_msg = source.to_string();
assert!(error_msg.contains("Sub-recipe file does not exist")); assert!(error_msg.contains("Sub-recipe file does not exist"));
assert!(error_msg.contains("nonexistent.yaml")); assert!(error_msg.contains("nonexistent.yaml"));
} }
_ => panic!("Expected RecipeError::RecipeParsing"), _ => panic!("Expected RecipeError::Invalid"),
} }
} }
@@ -601,10 +601,10 @@ parameters:
); );
assert!(result.is_err()); assert!(result.is_err());
if let Err(RecipeError::TemplateRendering { source }) = result { if let Err(RecipeError::Invalid { source }) = result {
assert!(source.to_string().contains("Failed to read parameter file")); assert!(source.to_string().contains("Failed to read parameter file"));
} else { } else {
panic!("Expected TemplateRendering error"); panic!("Expected Invalid error");
} }
} }
@@ -629,12 +629,12 @@ parameters:
); );
assert!(result.is_err()); assert!(result.is_err());
if let Err(RecipeError::TemplateRendering { source }) = result { if let Err(RecipeError::Invalid { source }) = result {
assert!(source assert!(source
.to_string() .to_string()
.contains("File parameters cannot have default values")); .contains("File parameters cannot have default values"));
} else { } else {
panic!("Expected TemplateRendering error for file parameter with default"); panic!("Expected Invalid error for file parameter with default");
} }
} }
} }
@@ -655,12 +655,12 @@ fn test_build_recipe_from_template_invalid_retry_config() {
let err = build_recipe_result.unwrap_err(); let err = build_recipe_result.unwrap_err();
match err { match err {
RecipeError::TemplateRendering { source } => { RecipeError::Invalid { source } => {
assert_eq!( assert_eq!(
source.to_string(), source.to_string(),
"Invalid retry configuration: max_retries must be greater than 0" "Invalid retry configuration: max_retries must be greater than 0"
); );
} }
_ => panic!("Expected TemplateRendering error, got: {:?}", err), _ => panic!("Expected Invalid error, got: {:?}", err),
} }
} }
+1 -1
View File
@@ -8,7 +8,7 @@ use anyhow::Result;
use minijinja::{Environment, UndefinedBehavior}; use minijinja::{Environment, UndefinedBehavior};
use regex::Regex; use regex::Regex;
const CURRENT_TEMPLATE_NAME: &str = "recipe_template"; const CURRENT_TEMPLATE_NAME: &str = "recipe";
const OPEN_BRACE: &str = "{{"; const OPEN_BRACE: &str = "{{";
const CLOSE_BRACE: &str = "}}"; const CLOSE_BRACE: &str = "}}";