refactor: Improved error handling for the tail-logs subcommand to propagate errors up the stack instead of exiting there.

This commit is contained in:
2025-12-04 10:10:19 -07:00
parent a0073b65ad
commit 659023d561
3 changed files with 10 additions and 16 deletions
+1 -1
View File
@@ -152,7 +152,7 @@ async fn main() -> Result<()> {
let mut cli = Cli::command();
generate(shell, &mut cli, "managarr", &mut io::stdout())
}
Command::TailLogs { no_color } => tail_logs(no_color).await,
Command::TailLogs { no_color } => tail_logs(no_color).await?,
},
None => {
let app_nw = Arc::clone(&app);
@@ -312,11 +312,7 @@ impl Network<'_, '_> {
Route::Sonarr(ActiveSonarrBlock::SeriesHistorySortPrompt, _)
);
let series_history = app
.data
.sonarr_data
.series_history
.get_or_insert_default();
let series_history = app.data.sonarr_data.series_history.get_or_insert_default();
if !is_sorting {
history_vec.sort_by(|a, b| a.id.cmp(&b.id));
+8 -10
View File
@@ -6,10 +6,10 @@ use std::sync::Arc;
use std::time::Duration;
use anyhow::Result;
use anyhow::anyhow;
use anyhow::{anyhow, Context};
use colored::Colorize;
use indicatif::{ProgressBar, ProgressStyle};
use log::{LevelFilter, error};
use log::{error, LevelFilter};
use log4rs::append::file::FileAppender;
use log4rs::config::{Appender, Root};
use log4rs::encode::pattern::PatternEncoder;
@@ -18,7 +18,7 @@ use reqwest::{Certificate, Client};
use tokio::sync::Mutex;
use tokio_util::sync::CancellationToken;
use crate::app::{App, AppConfig, log_and_print_error};
use crate::app::{log_and_print_error, App, AppConfig};
use crate::cli::{self, Command};
use crate::network::Network;
use crate::ui::theme::ThemeDefinitionsWrapper;
@@ -79,16 +79,15 @@ pub fn convert_runtime(runtime: i64) -> (i64, i64) {
(hours, minutes)
}
pub async fn tail_logs(no_color: bool) {
pub async fn tail_logs(no_color: bool) -> Result<()> {
let re = Regex::new(r"^(?P<timestamp>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d{3})\s+<(?P<opid>[^\s>]+)>\s+\[(?P<level>[A-Z]+)]\s+(?P<logger>[^:]+):(?P<line>\d+)\s+-\s+(?P<message>.*)$").unwrap();
let file_path = get_log_path();
let file = File::open(&file_path).expect("Cannot open file");
let mut reader = BufReader::new(file);
if let Err(e) = reader.seek(SeekFrom::End(0)) {
eprintln!("Unable to tail log file: {e:?}");
process::exit(1);
};
reader
.seek(SeekFrom::End(0))
.with_context(|| "Unable to tail log file")?;
let mut lines = reader.lines();
@@ -104,8 +103,7 @@ pub async fn tail_logs(no_color: bool) {
}
}
})
.await
.unwrap();
.await?
}
fn colorize_log_line(line: &str, re: &Regex) -> String {