Keep more error context info
This commit is contained in:
parent
2f8676533e
commit
7bab866eae
1 changed files with 28 additions and 7 deletions
35
src/files.rs
35
src/files.rs
|
|
@ -54,8 +54,16 @@ pub struct Files {
|
||||||
|
|
||||||
#[derive(Debug, thiserror::Error)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
pub enum Error {
|
pub enum Error {
|
||||||
#[error("{0}")]
|
#[error("Could not resolve {path}: {error}")]
|
||||||
Io(#[from] io::Error),
|
ResolvePath { path: PathBuf, error: io::Error },
|
||||||
|
#[error("Could not load {file}: {error}")]
|
||||||
|
ReadFile { file: PathBuf, error: io::Error },
|
||||||
|
#[error("Could not write {file}: {error}")]
|
||||||
|
WriteFile { file: PathBuf, error: io::Error },
|
||||||
|
#[error("Could not resolve timezone {timezone}: {error}")]
|
||||||
|
ResolveTz { timezone: String, error: io::Error },
|
||||||
|
#[error("Could not determine local timezone: {error}")]
|
||||||
|
LocalTz { error: io::Error },
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
Parse(#[from] parse::Error),
|
Parse(#[from] parse::Error),
|
||||||
#[error("{file1} has time zone {tz1} but {file2} has time zone {tz2}")]
|
#[error("{file1} has time zone {tz1} but {file2} has time zone {tz2}")]
|
||||||
|
|
@ -83,13 +91,20 @@ impl Files {
|
||||||
files: &mut Vec<LoadedFile>,
|
files: &mut Vec<LoadedFile>,
|
||||||
name: &Path,
|
name: &Path,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let path = name.canonicalize()?;
|
let path = name.canonicalize().map_err(|e| Error::ResolvePath {
|
||||||
|
path: name.to_path_buf(),
|
||||||
|
error: e,
|
||||||
|
})?;
|
||||||
if paths.contains_key(&path) {
|
if paths.contains_key(&path) {
|
||||||
// We've already loaded this exact file.
|
// We've already loaded this exact file.
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
let content = fs::read_to_string(name)?;
|
let content = fs::read_to_string(name).map_err(|e| Error::ReadFile {
|
||||||
|
file: path.clone(),
|
||||||
|
error: e,
|
||||||
|
})?;
|
||||||
|
|
||||||
// Using `name` instead of `path` for the unwrap below.
|
// Using `name` instead of `path` for the unwrap below.
|
||||||
let file = parse::parse(name, &content)?;
|
let file = parse::parse(name, &content)?;
|
||||||
let includes = file.includes.clone();
|
let includes = file.includes.clone();
|
||||||
|
|
@ -128,9 +143,12 @@ impl Files {
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(if let Some((_, tz)) = found {
|
Ok(if let Some((_, tz)) = found {
|
||||||
Tz::named(&tz)?
|
Tz::named(&tz).map_err(|e| Error::ResolveTz {
|
||||||
|
timezone: tz,
|
||||||
|
error: e,
|
||||||
|
})?
|
||||||
} else {
|
} else {
|
||||||
Tz::local()?
|
Tz::local().map_err(|e| Error::LocalTz { error: e })?
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -144,7 +162,10 @@ impl Files {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn save_file(path: &Path, file: &File) -> Result<()> {
|
fn save_file(path: &Path, file: &File) -> Result<()> {
|
||||||
fs::write(path, &format!("{}", file))?;
|
fs::write(path, &format!("{}", file)).map_err(|e| Error::WriteFile {
|
||||||
|
file: path.to_path_buf(),
|
||||||
|
error: e,
|
||||||
|
})?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue