improvements to error handling again

This commit is contained in:
2025-06-12 00:59:33 -04:00
parent 03beada825
commit 69ab6165bb
2 changed files with 95 additions and 10 deletions

View File

@ -51,7 +51,7 @@ fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
"code": "SCHEMA_RESOURCE_ADD_FAILED",
"message": format!("Failed to add schema resource '{}'", schema_id),
"details": {
"path": schema_path,
"schema": schema_id,
"cause": format!("{}", e)
}
}]
@ -72,7 +72,7 @@ fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
let mut error_list = Vec::new();
collect_errors(src, &mut error_list);
// Filter and format errors properly - no instance for schema compilation
format_errors(error_list, &schema_value)
format_errors(error_list, &schema_value, schema_id)
}
_ => {
// Other compilation errors
@ -80,7 +80,7 @@ fn cache_json_schema(schema_id: &str, schema: JsonB, strict: bool) -> JsonB {
"code": "SCHEMA_COMPILATION_FAILED",
"message": format!("Schema '{}' compilation failed", schema_id),
"details": {
"path": schema_path,
"schema": schema_id,
"cause": format!("{:?}", e)
}
})]
@ -126,6 +126,7 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
"code": "SCHEMA_NOT_FOUND",
"message": format!("Schema '{}' not found in cache", schema_id),
"details": {
"schema": schema_id,
"cause": "Schema must be cached before validation"
}
}]
@ -137,7 +138,7 @@ fn validate_json_schema(schema_id: &str, instance: JsonB) -> JsonB {
Err(validation_error) => {
let mut error_list = Vec::new();
collect_errors(&validation_error, &mut error_list);
let errors = format_errors(error_list, &instance_value);
let errors = format_errors(error_list, &instance_value, schema_id);
JsonB(json!({ "errors": errors }))
}
}
@ -332,7 +333,7 @@ fn convert_error_kind(kind: &ErrorKind) -> (String, String) {
}
// Formats errors according to DropError structure
fn format_errors(errors: Vec<Error>, instance: &Value) -> Vec<Value> {
fn format_errors(errors: Vec<Error>, instance: &Value, schema_id: &str) -> Vec<Value> {
// Deduplicate by instance_path and format as DropError
let mut unique_errors: HashMap<String, Value> = HashMap::new();
for error in errors {
@ -345,7 +346,8 @@ fn format_errors(errors: Vec<Error>, instance: &Value) -> Vec<Value> {
"details": {
"path": error.path,
"context": failing_value,
"cause": error.cause
"cause": error.cause,
"schema": schema_id
}
}));
}