From 3a9591a3d9d8f4c9fb990274e967bdef89177d14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E9=9B=85=20misaki=20masa?= Date: Sat, 21 Jun 2025 22:23:59 +0800 Subject: [PATCH] feat: improve error handling in config file reading (#2901) --- yazi-config/src/keymap/keymap.rs | 6 ++++-- yazi-config/src/theme/flavor.rs | 2 +- yazi-config/src/theme/theme.rs | 6 ++++-- yazi-config/src/yazi.rs | 6 ++++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/yazi-config/src/keymap/keymap.rs b/yazi-config/src/keymap/keymap.rs index ff49ccc4..f1618274 100644 --- a/yazi-config/src/keymap/keymap.rs +++ b/yazi-config/src/keymap/keymap.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use serde::Deserialize; use yazi_codegen::DeserializeOver1; use yazi_fs::{Xdg, ok_or_not_found}; @@ -38,7 +38,9 @@ impl Keymap { impl Keymap { pub(crate) fn read() -> Result { - Ok(ok_or_not_found(std::fs::read_to_string(Xdg::config_dir().join("keymap.toml")))?) + let p = Xdg::config_dir().join("keymap.toml"); + ok_or_not_found(std::fs::read_to_string(&p)) + .with_context(|| format!("Failed to read keymap {p:?}")) } pub(crate) fn reshape(self) -> Result { diff --git a/yazi-config/src/theme/flavor.rs b/yazi-config/src/theme/flavor.rs index e486648a..bc81c60d 100644 --- a/yazi-config/src/theme/flavor.rs +++ b/yazi-config/src/theme/flavor.rs @@ -33,7 +33,7 @@ impl Flavor { "" => String::new(), name => { let p = Xdg::config_dir().join(format!("flavors/{name}.yazi/flavor.toml")); - std::fs::read_to_string(&p).with_context(|| format!("Failed to load flavor {p:?}"))? + std::fs::read_to_string(&p).with_context(|| format!("Failed to read flavor {p:?}"))? } }) } diff --git a/yazi-config/src/theme/theme.rs b/yazi-config/src/theme/theme.rs index 6cc244a1..57d1985a 100644 --- a/yazi-config/src/theme/theme.rs +++ b/yazi-config/src/theme/theme.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use anyhow::{Result, bail}; +use anyhow::{Context, Result, bail}; use serde::Deserialize; use yazi_codegen::{DeserializeOver1, DeserializeOver2}; use yazi_fs::{Xdg, expand_path, ok_or_not_found}; @@ -207,7 +207,9 @@ pub struct Help { impl Theme { pub(crate) fn read() -> Result { - Ok(ok_or_not_found(std::fs::read_to_string(Xdg::config_dir().join("theme.toml")))?) + let p = Xdg::config_dir().join("theme.toml"); + ok_or_not_found(std::fs::read_to_string(&p)) + .with_context(|| format!("Failed to read theme {p:?}")) } pub(crate) fn reshape(mut self, light: bool) -> Result { diff --git a/yazi-config/src/yazi.rs b/yazi-config/src/yazi.rs index 8f6de1ad..ba346ccf 100644 --- a/yazi-config/src/yazi.rs +++ b/yazi-config/src/yazi.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use anyhow::{Context, Result}; use serde::Deserialize; use yazi_codegen::DeserializeOver1; use yazi_fs::{Xdg, ok_or_not_found}; @@ -21,7 +21,9 @@ pub struct Yazi { impl Yazi { pub(super) fn read() -> Result { - Ok(ok_or_not_found(std::fs::read_to_string(Xdg::config_dir().join("yazi.toml")))?) + let p = Xdg::config_dir().join("yazi.toml"); + ok_or_not_found(std::fs::read_to_string(&p)) + .with_context(|| format!("Failed to read config {p:?}")) } pub(super) fn reshape(self) -> Result {