Skip to content

Commit ab6e46c

Browse files
committed
fix: improve error descriptions and provide details when decoding of crate versions fails.
It was suggested in this comment: rust-lang/docs.rs#1807 (comment) , and if marker references aren't as expected it's possible to diff lines that are not actually crate versions.
1 parent 83e91f5 commit ab6e46c

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ git-repository = { version = "0.23.1", default-features = false, features = ["ma
1919
similar = { version = "2.2.0", features = ["bytes"] }
2020
serde = { version = "1", features = ["std", "derive"] }
2121
serde_json = "1"
22+
bstr = "0.2.17"
2223
thiserror = "1.0.32"
2324

2425
[dependencies.git2]

src/index/diff/delegate.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<'repo> Delegate<'repo> {
4141
Addition { entry_mode, oid } => {
4242
if let Some(obj) = entry_data(self.repo, entry_mode, oid)? {
4343
for line in (&obj.data).lines() {
44-
let version: CrateVersion = serde_json::from_slice(line)?;
44+
let version = version_from_json_line(line)?;
4545
self.changes.push(if version.yanked {
4646
Change::Yanked(version)
4747
} else {
@@ -70,8 +70,7 @@ impl<'repo> Delegate<'repo> {
7070
for change in diff.iter_all_changes() {
7171
match change.tag() {
7272
ChangeTag::Delete | ChangeTag::Insert => {
73-
let version =
74-
serde_json::from_slice::<CrateVersion>(change.value())?;
73+
let version = version_from_json_line(change.value())?;
7574
if change.tag() == ChangeTag::Insert {
7675
self.changes.push(if version.yanked {
7776
Change::Yanked(version)
@@ -129,3 +128,10 @@ impl git::diff::tree::Visit for Delegate<'_> {
129128
}
130129
}
131130
}
131+
132+
fn version_from_json_line(line: &[u8]) -> Result<CrateVersion, Error> {
133+
serde_json::from_slice(line).map_err(|err| Error::VersionDecode {
134+
source: err,
135+
line: line.into(),
136+
})
137+
}

src/index/diff/mod.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,23 @@ use delegate::Delegate;
1111
#[derive(Debug, thiserror::Error)]
1212
#[allow(missing_docs)]
1313
pub enum Error {
14-
#[error(transparent)]
14+
#[error("Failed to fetch crates.io index repository")]
1515
Fetch(#[from] git2::Error),
16-
#[error(transparent)]
16+
#[error("Couldn't update marker reference")]
1717
ReferenceEdit(#[from] git::reference::edit::Error),
18-
#[error(transparent)]
18+
#[error("Failed to parse rev-spec to determine which revisions to diff")]
1919
RevParse(#[from] git::revision::spec::parse::Error),
20-
#[error(transparent)]
20+
#[error("Couldn't find blob that showed up when diffing trees")]
2121
FindObject(#[from] git::object::find::existing::Error),
22-
#[error(transparent)]
22+
#[error("Couldn't get the tree of a commit for diffing purposes")]
2323
PeelToTree(#[from] git::object::peel::to_kind::Error),
24-
#[error(transparent)]
24+
#[error("Failed to diff two trees to find changed crates")]
2525
Diff(#[from] git::diff::tree::changes::Error),
26-
#[error(transparent)]
27-
VersionDecode(#[from] serde_json::Error),
26+
#[error("Failed to decode {line:?} as crate version")]
27+
VersionDecode {
28+
source: serde_json::Error,
29+
line: bstr::BString,
30+
},
2831
}
2932

3033
/// Find changes without modifying the underling repository

0 commit comments

Comments
 (0)