Skip to content

Commit

Permalink
fix(object): Report remaining input on error
Browse files Browse the repository at this point in the history
Fixes #1099
  • Loading branch information
epage committed Nov 10, 2023
1 parent 5825250 commit 55729a5
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 11 deletions.
7 changes: 6 additions & 1 deletion gix-object/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,12 @@ pub mod decode {

impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.inner.fmt(f)
write!(f, "object parsing failed at `{}`", bstr::BStr::new(&self.remaining))?;
if self.inner.context().next().is_some() {
writeln!(f)?;
self.inner.fmt(f)?;
}
Ok(())
}
}

Expand Down
2 changes: 1 addition & 1 deletion gix-object/tests/commit/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ fn invalid() {
assert_eq!(
CommitRef::from_bytes(partial_commit).unwrap_err().to_string(),
if cfg!(feature = "verbose-object-parsing-errors") {
"expected `<timestamp>`, `<name> <<email>> <timestamp> <+|-><HHMM>`, `author <signature>`"
"object parsing failed at `1`\nexpected `<timestamp>`, `<name> <<email>> <timestamp> <+|-><HHMM>`, `author <signature>`"
} else {
"object parsing failed"
}
Expand Down
2 changes: 1 addition & 1 deletion gix-object/tests/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ fn invalid() {
assert_eq!(
TagRef::from_bytes(partial_tag).unwrap_err().to_string(),
if cfg!(feature = "verbose-object-parsing-errors") {
""
"object parsing failed at `tagger Sebasti`"
} else {
"object parsing failed"
}
Expand Down
14 changes: 6 additions & 8 deletions gix-object/tests/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,12 @@ mod from_bytes {
fn invalid() {
let fixture = fixture_name("tree", "definitely-special.tree");
let partial_tree = &fixture[..fixture.len() / 2];
assert_eq!(
TreeRef::from_bytes(partial_tree).unwrap_err().to_string(),
if cfg!(feature = "verbose-object-parsing-errors") {
""
} else {
"object parsing failed"
}
);
let err = TreeRef::from_bytes(partial_tree).unwrap_err().to_string();
if cfg!(feature = "verbose-object-parsing-errors") {
assert!(err.starts_with("object parsing failed at `100644"), "{err}");
} else {
assert_eq!(err, "object parsing failed");
}
assert_eq!(
TreeRefIter::from_bytes(partial_tree).take_while(Result::is_ok).count(),
9,
Expand Down

0 comments on commit 55729a5

Please sign in to comment.