Skip to content

Commit

Permalink
feat: add more context to error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi committed Sep 29, 2024
1 parent 3e4973d commit 9f9c6ac
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 23 deletions.
23 changes: 11 additions & 12 deletions yazi-adapter/src/emulator.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -103,21 +103,20 @@ impl Emulator {
}

pub fn via_env() -> (String, String) {
fn tmux_env(name: &str) -> Result<String> {
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> {
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)
Expand Down
2 changes: 1 addition & 1 deletion yazi-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;
Expand Down
2 changes: 1 addition & 1 deletion yazi-cli/src/package/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
Expand Down
8 changes: 6 additions & 2 deletions yazi-cli/src/package/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
6 changes: 3 additions & 3 deletions yazi-config/src/preset.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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"))?)
}
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 7 additions & 3 deletions yazi-fm/src/logs.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -7,9 +8,10 @@ static _GUARD: RoCell<WorkerGuard> = 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);
Expand All @@ -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(())
}
}
2 changes: 1 addition & 1 deletion yazi-fm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use term::*;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
Panic::install();
Logs::start();
Logs::start()?;

_ = fdlimit::raise_fd_limit();

Expand Down

0 comments on commit 9f9c6ac

Please sign in to comment.