Skip to content

Commit

Permalink
Fix errors when pages are missing from the cache
Browse files Browse the repository at this point in the history
When the language directories are empty, the entire cache dir has
to be deleted in order for --update to work properly.
If the user manually deletes pages for some reason, the error messages
now tell them to run --clean-cache first.
  • Loading branch information
acuteenvy committed Jan 1, 2025
1 parent 79e42c9 commit 3bda245
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ impl<'a> Cache<'a> {
}

if result.is_empty() {
Err(Error::new(
"'pages.en' contains no platform directories. Please run 'tldr --update'.",
Err(Error::messed_up_cache(
"'pages.en' contains no platform directories.",
))
} else {
// read_dir() order can differ across runs, so it's
Expand Down Expand Up @@ -421,7 +421,9 @@ impl<'a> Cache<'a> {

fn print_basenames(mut pages: Vec<OsString>) -> Result<()> {
if pages.is_empty() {
return Err(Error::new("no pages found. Please run 'tldr --update'."));
return Err(Error::messed_up_cache(
"no pages found, but the 'pages.en' directory exists.",
));
}

// Show pages in alphabetical order.
Expand Down
7 changes: 7 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,13 @@ impl Error {
.kind(ErrorKind::Download)
}

pub fn messed_up_cache(e: &str) -> Self {
Error::new(format!(
"{e}\n\nThis should never happen, did you delete something from the cache?\n\
Please run 'tldr --clean-cache' followed by 'tldr --update' to redownload all pages."
))
}

/// Print the error message to stderr and return an appropriate `ExitCode`.
pub fn exit_code(self) -> ExitCode {
let _ = writeln!(io::stderr(), "{} {self}", "error:".red().bold());
Expand Down

0 comments on commit 3bda245

Please sign in to comment.