Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dimidumo/zk 909 improve error handling #81

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 46 additions & 2 deletions packages/apis/src/extract_substrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,38 @@ pub enum ExtractSubstrssError {
SubstringNotFound(Regex, String),
#[error(transparent)]
RegexError(#[from] fancy_regex::Error),
#[error("Invalid regex in parts, index {part_index}: '{regex_def}' - {error}")]
InvalidRegexPart {
part_index: usize,
regex_def: String,
error: fancy_regex::Error,
},
}

pub fn extract_substr_idxes(
input_str: &str,
regex_config: &DecomposedRegexConfig,
reveal_private: bool,
) -> Result<Vec<(usize, usize)>, ExtractSubstrssError> {
// Validate each regex part individually, to throw better errors
for (i, part) in regex_config.parts.iter().enumerate() {
Regex::new(&part.regex_def).map_err(|e| ExtractSubstrssError::InvalidRegexPart {
part_index: i,
regex_def: part.regex_def.clone(),
error: e,
})?;
}

// Construct the full regex pattern with groups for each part
let mut entire_regex_str = String::new();
for (_, part) in regex_config.parts.iter().enumerate() {
let adjusted_regex_def = part.regex_def.replace("(", "(?:");
entire_regex_str += &format!("({})", adjusted_regex_def); // Wrap each part in a group
entire_regex_str += &format!("({})", adjusted_regex_def);
}

// Compile the entire regex
let entire_regex = Regex::new(&entire_regex_str)?;
// This should be impossible to fail, since we tested the seperate regex parts before.
let entire_regex = Regex::new(&entire_regex_str).unwrap();

// Find the match for the entire regex
let entire_captures = entire_regex
Expand Down Expand Up @@ -267,6 +283,34 @@ mod test {
assert_eq!(idxes, vec![(21, 27)]);
}

#[test]
fn test_error_handling() {
let code_regex = DecomposedRegexConfig {
// max_byte_size: 1024,
parts: vec![
RegexPartConfig {
is_public: false,
regex_def: "Hello ".to_string(),
},
RegexPartConfig {
is_public: true,
regex_def: "[^,+".to_string(),
},
RegexPartConfig {
is_public: false,
regex_def: "!".to_string(),
},
],
};
let input_str = "Hello Mamba!";
let result = extract_substr_idxes(input_str, &code_regex, false);
assert!(result.is_err());
assert_eq!(
"Invalid regex in parts, index 1: '[^,+' - Parsing error at position 4: Invalid character class",
result.unwrap_err().to_string()
);
}

#[test]
fn test_body_hash_valid() {
let input_str = "dkim-signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1694989812; x=1695594612; dara=google.com; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=BWETwQ9JDReS4GyR2v2TTR8Bpzj9ayumsWQJ3q7vehs=; b=";
Expand Down
Loading