Skip to content

Commit

Permalink
Merge pull request #53 from codecov/joseph/add-error-info
Browse files Browse the repository at this point in the history
feat: add buffer location to error message
  • Loading branch information
joseph-sentry authored Nov 27, 2024
2 parents 3348c31 + 3d4a26d commit 02db067
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/junit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,31 @@ fn populate(
pub fn parse_junit_xml(file_bytes: &[u8]) -> PyResult<ParsingInfo> {
let mut reader = Reader::from_reader(file_bytes);
reader.config_mut().trim_text(true);
let thing = use_reader(&mut reader).map_err(|e| {
let pos = reader.buffer_position();
let (line, col) = get_position_info(file_bytes, pos.try_into().unwrap());
ParserError::new_err(format!("Error at {}:{}: {}", line, col, e))
})?;
Ok(thing)
}

fn get_position_info(input: &[u8], byte_offset: usize) -> (usize, usize) {
let mut line = 1;
let mut last_newline = 0;

for (i, &byte) in input.iter().take(byte_offset).enumerate() {
if byte == b'\n' {
line += 1;
last_newline = i + 1;
}
}

let column = byte_offset - last_newline + 1;

(line, column)
}

fn use_reader(reader: &mut Reader<&[u8]>) -> PyResult<ParsingInfo> {
let mut testruns: Vec<Testrun> = Vec::new();
let mut saved_testrun: Option<Testrun> = None;

Expand Down

0 comments on commit 02db067

Please sign in to comment.