Skip to content

Commit 4eb9b6b

Browse files
Skglandrami3l
authored andcommitted
consistently add context with file path when parsing fails
1 parent 09a9c45 commit 4eb9b6b

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

src/config.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,15 @@ impl Cfg {
641641
};
642642

643643
if let Ok(contents) = contents {
644-
let add_file_context = || format!("in {}", toolchain_file.to_string_lossy());
645644
// XXX Should not return the unvalidated contents; but a new
646645
// internal only safe struct
647-
let override_file = Cfg::parse_override_file(contents, parse_mode)
648-
.with_context(add_file_context)?;
646+
let override_file =
647+
Cfg::parse_override_file(contents, parse_mode).with_context(|| {
648+
RustupError::ParsingFile {
649+
name: "override",
650+
path: toolchain_file.clone(),
651+
}
652+
})?;
649653
if let Some(toolchain_name_str) = &override_file.toolchain.channel {
650654
let toolchain_name = ResolvableToolchainName::try_from(toolchain_name_str)?;
651655
let default_host_triple = get_default_host_triple(settings);

src/dist/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ impl Config {
4343
}
4444

4545
pub(crate) fn parse(data: &str) -> Result<Self> {
46-
let value = toml::from_str(data).context("error parsing manifest")?;
46+
let value = toml::from_str(data).context("error parsing config")?;
4747
Self::from_toml(value, "")
4848
}
4949

src/dist/dist.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,11 @@ pub(crate) async fn dl_v2_manifest(
10961096
return Ok(None);
10971097
};
10981098
let manifest_str = utils::read_file("manifest", &manifest_file)?;
1099-
let manifest = ManifestV2::parse(&manifest_str)?;
1099+
let manifest =
1100+
ManifestV2::parse(&manifest_str).with_context(|| RustupError::ParsingFile {
1101+
name: "manifest",
1102+
path: manifest_file.to_path_buf(),
1103+
})?;
11001104

11011105
Ok(Some((manifest, manifest_hash)))
11021106
}

src/dist/manifestation.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -313,8 +313,12 @@ impl Manifestation {
313313

314314
// Read configuration and delete it
315315
let rel_config_path = prefix.rel_manifest_file(CONFIG_FILE);
316-
let config_str = utils::read_file("dist config", &prefix.path().join(&rel_config_path))?;
317-
let config = Config::parse(&config_str)?;
316+
let abs_config_path = prefix.path().join(&rel_config_path);
317+
let config_str = utils::read_file("dist config", &abs_config_path)?;
318+
let config = Config::parse(&config_str).with_context(|| RustupError::ParsingFile {
319+
name: "config",
320+
path: abs_config_path,
321+
})?;
318322
tx.remove_file("dist config", rel_config_path)?;
319323

320324
for component in config.components {
@@ -359,7 +363,12 @@ impl Manifestation {
359363
let config_path = prefix.path().join(rel_config_path);
360364
if utils::path_exists(&config_path) {
361365
let config_str = utils::read_file("dist config", &config_path)?;
362-
Ok(Some(Config::parse(&config_str)?))
366+
Ok(Some(Config::parse(&config_str).with_context(|| {
367+
RustupError::ParsingFile {
368+
name: "Config",
369+
path: config_path,
370+
}
371+
})?))
363372
} else {
364373
Ok(None)
365374
}
@@ -371,7 +380,12 @@ impl Manifestation {
371380
let old_manifest_path = prefix.manifest_file(DIST_MANIFEST);
372381
if utils::path_exists(&old_manifest_path) {
373382
let manifest_str = utils::read_file("installed manifest", &old_manifest_path)?;
374-
Ok(Some(Manifest::parse(&manifest_str)?))
383+
Ok(Some(Manifest::parse(&manifest_str).with_context(|| {
384+
RustupError::ParsingFile {
385+
name: "manifest",
386+
path: old_manifest_path,
387+
}
388+
})?))
375389
} else {
376390
Ok(None)
377391
}

src/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ pub(crate) enum RustupError {
7171
ReadingDirectory { name: &'static str, path: PathBuf },
7272
#[error("could not read {name} file: '{}'", .path.display())]
7373
ReadingFile { name: &'static str, path: PathBuf },
74+
#[error("could not parse {name} file: '{}'", .path.display())]
75+
ParsingFile { name: &'static str, path: PathBuf },
7476
#[error("could not remove '{}' directory: '{}'", .name, .path.display())]
7577
RemovingDirectory { name: &'static str, path: PathBuf },
7678
#[error("could not remove '{name}' file: '{}'", .path.display())]

src/settings.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ impl SettingsFile {
4343
drop(b);
4444
*self.cache.borrow_mut() = Some(if utils::is_file(&self.path) {
4545
let content = utils::read_file("settings", &self.path)?;
46-
Settings::parse(&content)?
46+
Settings::parse(&content).with_context(|| RustupError::ParsingFile {
47+
name: "settings",
48+
path: self.path.clone(),
49+
})?
4750
} else {
4851
needs_save = true;
4952
Default::default()

0 commit comments

Comments
 (0)