feat: add more context to error messages (#1701)

This commit is contained in:
三咲雅 · Misaki Masa 2024-09-29 15:56:28 +08:00 committed by GitHub
parent 3e4973dbbf
commit e6912da7d1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 30 additions and 23 deletions

View File

@ -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<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)

View File

@ -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?;

View File

@ -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!");

View File

@ -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(())
}

View File

@ -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) {

View File

@ -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<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);
@ -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(())
}
}

View File

@ -38,7 +38,7 @@ use term::*;
#[tokio::main]
async fn main() -> anyhow::Result<()> {
Panic::install();
Logs::start();
Logs::start()?;
_ = fdlimit::raise_fd_limit();