diff --git a/src-tauri/src/download.rs b/src-tauri/src/download.rs index e09f7a0..9ee3808 100644 --- a/src-tauri/src/download.rs +++ b/src-tauri/src/download.rs @@ -365,7 +365,7 @@ fn run_download(mgr: &DownloadMgr, asin: &str) { } } - let aaxc = find_any_aaxc(&dir).or_else(|| find_any_aaxc(&dir)); + let aaxc = find_any_aaxc(&dir); let final_bytes = current_bytes(&dir); let ok = aaxc .and_then(|p| fs::metadata(&p).ok()) @@ -375,15 +375,36 @@ fn run_download(mgr: &DownloadMgr, asin: &str) { if ok { mgr.finish(asin, DlStatus::Done, final_bytes, String::new()); } else { - let msg = if stderr_text.trim().is_empty() { - "Download endete ohne vollständige AAXC-Datei".to_string() - } else { - stderr_text.lines().last().unwrap_or("unbekannter Fehler").to_string() - }; + let msg = pick_download_error(&stderr_text); mgr.finish(asin, DlStatus::Failed, final_bytes, msg); } } +/// Sucht die aufschlussreichste Zeile aus dem stderr-Fehlerprotokoll: +/// bevorzugt „error: failed for …“ (Enzeljob-Kontext), sonst letzte +/// „error:“-Zeile als Fallback. Zusammenfassung-Zeilen („1 job(s) failed“) +/// sollen nicht als Meldung landen. +fn pick_download_error(stderr: &str) -> String { + let lines: Vec<&str> = stderr.lines().filter(|l| !l.trim().is_empty()).collect(); + if let Some(job_line) = lines + .iter() + .find(|l| l.starts_with("error:") && l.contains("failed for")) + { + return job_line.trim_start_matches("error: ").to_string(); + } + if let Some(err_line) = lines + .iter() + .rev() + .find(|l| l.starts_with("error:") && !l.contains("job(s) failed")) + { + return err_line.trim_start_matches("error: ").to_string(); + } + if let Some(last) = lines.last() { + return last.to_string(); + } + "Download endete ohne vollständige AAXC-Datei".to_string() +} + /// Größe der größten AAXC/AXP-Datei im Job-Verzeichnis. fn current_bytes(dir: &Path) -> u64 { let mut best = 0u64; @@ -425,6 +446,19 @@ fn find_any_aaxc(dir: &Path) -> Option { mod tests { use super::*; + #[test] + fn pick_download_error_waehlt_job_fehler() { + let s = "File x.jpg downloaded\nChapter file saved\nerror: download_aaxc failed for Einhornprinz (B015HG6T6A): AudibleCliException: is not downloadable.\nThe download ended with the following result:\nerror: 1 job(s) failed. Use --ignore-errors to download the rest anyway"; + assert_eq!( + pick_download_error(s), + "download_aaxc failed for Einhornprinz (B015HG6T6A): AudibleCliException: is not downloadable." + ); + // Fallback: keine Job-Zeile, aber error:-Zeile + assert_eq!(pick_download_error("error: 401 unauthorized\nerror: 1 job(s) failed"), "401 unauthorized"); + // gar nichts da + assert_eq!(pick_download_error(""), "Download endete ohne vollständige AAXC-Datei"); + } + #[test] fn dl_item_serde_status() { let it = DlItem {