From 9f9c6acf6c5bd309c3e4e7ed9c08ea577f4f46ca Mon Sep 17 00:00:00 2001 From: sxyazi Date: Sun, 29 Sep 2024 15:55:51 +0800 Subject: [PATCH] feat: add more context to error messages --- yazi-adapter/src/emulator.rs | 23 +++++++++++------------ yazi-cli/src/main.rs | 2 +- yazi-cli/src/package/deploy.rs | 2 +- yazi-cli/src/package/mod.rs | 8 ++++++-- yazi-config/src/preset.rs | 6 +++--- yazi-fm/src/logs.rs | 10 +++++++--- yazi-fm/src/main.rs | 2 +- 7 files changed, 30 insertions(+), 23 deletions(-) diff --git a/yazi-adapter/src/emulator.rs b/yazi-adapter/src/emulator.rs index 48c1738c7..498e090e5 100644 --- a/yazi-adapter/src/emulator.rs +++ b/yazi-adapter/src/emulator.rs @@ -1,6 +1,6 @@ -use std::{env, io::{LineWriter, stderr}, time::Duration}; +use std::{io::{LineWriter, stderr}, time::Duration}; -use anyhow::{Result, anyhow, bail}; +use anyhow::{Result, bail}; use crossterm::{cursor::{RestorePosition, SavePosition}, execute, style::Print, terminal::{disable_raw_mode, enable_raw_mode}}; use scopeguard::defer; use tokio::{io::{AsyncReadExt, BufReader}, time::timeout}; @@ -103,21 +103,20 @@ impl Emulator { } pub fn via_env() -> (String, String) { - fn tmux_env(name: &str) -> Result { - let output = std::process::Command::new("tmux").args(["show-environment", name]).output()?; - - String::from_utf8(output.stdout)? - .trim() - .strip_prefix(&format!("{name}=")) - .map_or_else(|| Err(anyhow!("")), |s| Ok(s.to_string())) + fn tmux_env(name: &str) -> Option { + String::from_utf8_lossy( + &std::process::Command::new("tmux").args(["show-environment", name]).output().ok()?.stdout, + ) + .trim() + .strip_prefix(&format!("{name}=")) + .map(ToOwned::to_owned) } - let mut term = env::var("TERM").unwrap_or_default(); - let mut program = env::var("TERM_PROGRAM").unwrap_or_default(); + let mut term = std::env::var("TERM").unwrap_or_default(); + let program = std::env::var("TERM_PROGRAM").unwrap_or_default(); if *TMUX { term = tmux_env("TERM").unwrap_or(term); - program = tmux_env("TERM_PROGRAM").unwrap_or(program); } (term, program) diff --git a/yazi-cli/src/main.rs b/yazi-cli/src/main.rs index f51754126..fc4b23508 100644 --- a/yazi-cli/src/main.rs +++ b/yazi-cli/src/main.rs @@ -46,7 +46,7 @@ async fn main() -> anyhow::Result<()> { } Command::Pack(cmd) => { - package::init(); + package::init()?; package::Package::migrate().await?; if cmd.install { package::Package::install_from_config("plugin", false).await?; diff --git a/yazi-cli/src/package/deploy.rs b/yazi-cli/src/package/deploy.rs index bb845f081..6bdc10564 100644 --- a/yazi-cli/src/package/deploy.rs +++ b/yazi-cli/src/package/deploy.rs @@ -41,7 +41,7 @@ For safety, please manually delete it from your plugin/flavor directory and re-r fs::copy(&from, &to) .await - .with_context(|| format!("Failed to copy `{}` to `{}`", from.display(), to.display()))?; + .with_context(|| format!("failed to copy `{}` to `{}`", from.display(), to.display()))?; } println!("Done!"); diff --git a/yazi-cli/src/package/mod.rs b/yazi-cli/src/package/mod.rs index 2e7ec2a11..0e614c6df 100644 --- a/yazi-cli/src/package/mod.rs +++ b/yazi-cli/src/package/mod.rs @@ -8,10 +8,14 @@ mod package; mod parser; mod upgrade; +use anyhow::Context; use git::*; pub(super) use package::*; -pub(super) fn init() { +pub(super) fn init() -> anyhow::Result<()> { let root = yazi_shared::Xdg::state_dir().join("packages"); - std::fs::create_dir_all(root).expect("Failed to create packages directory"); + std::fs::create_dir_all(&root) + .with_context(|| format!("failed to create packages directory: {root:?}"))?; + + Ok(()) } diff --git a/yazi-config/src/preset.rs b/yazi-config/src/preset.rs index 848c29608..b95ab9cb7 100644 --- a/yazi-config/src/preset.rs +++ b/yazi-config/src/preset.rs @@ -1,6 +1,6 @@ use std::{borrow::Cow, path::{Path, PathBuf}}; -use anyhow::{Context, Result, anyhow}; +use anyhow::{Context, Result}; use toml::{Table, Value}; use crate::{preset, theme::Flavor}; @@ -26,7 +26,7 @@ impl Preset { let p = p.join(format!("flavors/{use_}.yazi/flavor.toml")); let flavor = - std::fs::read_to_string(&p).with_context(|| anyhow!("Failed to load flavor {p:?}"))?; + std::fs::read_to_string(&p).with_context(|| format!("failed to load flavor {p:?}"))?; Self::merge_str(&user, &Self::merge_str(&flavor, &preset!("theme"))?) } @@ -54,7 +54,7 @@ impl Preset { return Ok(base); } - Self::merge_str(&s, &base).with_context(|| anyhow!("Loading {user:?}")) + Self::merge_str(&s, &base).with_context(|| format!("failed to parse config: {user:?}")) } fn merge(a: &mut Table, b: Table, max: u8) { diff --git a/yazi-fm/src/logs.rs b/yazi-fm/src/logs.rs index 9401a6774..497af896a 100644 --- a/yazi-fm/src/logs.rs +++ b/yazi-fm/src/logs.rs @@ -1,3 +1,4 @@ +use anyhow::Context; use tracing_appender::non_blocking::WorkerGuard; use tracing_subscriber::{Registry, fmt, prelude::__tracing_subscriber_SubscriberExt}; use yazi_shared::{RoCell, Xdg}; @@ -7,9 +8,10 @@ static _GUARD: RoCell = RoCell::new(); pub(super) struct Logs; impl Logs { - pub(super) fn start() { + pub(super) fn start() -> anyhow::Result<()> { let state_dir = Xdg::state_dir(); - std::fs::create_dir_all(&state_dir).expect("Failed to create state directory"); + std::fs::create_dir_all(&state_dir) + .with_context(|| format!("failed to create state directory: {state_dir:?}"))?; let appender = tracing_appender::rolling::never(state_dir, "yazi.log"); let (handle, guard) = tracing_appender::non_blocking(appender); @@ -18,8 +20,10 @@ impl Logs { let subscriber = Registry::default() .with(fmt::layer().pretty().with_writer(handle).with_ansi(cfg!(debug_assertions))); - tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); + tracing::subscriber::set_global_default(subscriber) + .context("setting default subscriber failed")?; _GUARD.init(guard); + Ok(()) } } diff --git a/yazi-fm/src/main.rs b/yazi-fm/src/main.rs index 8bf392356..617cebad5 100644 --- a/yazi-fm/src/main.rs +++ b/yazi-fm/src/main.rs @@ -38,7 +38,7 @@ use term::*; #[tokio::main] async fn main() -> anyhow::Result<()> { Panic::install(); - Logs::start(); + Logs::start()?; _ = fdlimit::raise_fd_limit();