feat: improve error handling in config file reading (#2901)

This commit is contained in:
三咲雅 misaki masa 2025-06-21 22:23:59 +08:00 committed by GitHub
parent f5d0f47b51
commit 3a9591a3d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 7 deletions

View file

@ -1,4 +1,4 @@
use anyhow::Result; use anyhow::{Context, Result};
use serde::Deserialize; use serde::Deserialize;
use yazi_codegen::DeserializeOver1; use yazi_codegen::DeserializeOver1;
use yazi_fs::{Xdg, ok_or_not_found}; use yazi_fs::{Xdg, ok_or_not_found};
@ -38,7 +38,9 @@ impl Keymap {
impl Keymap { impl Keymap {
pub(crate) fn read() -> Result<String> { pub(crate) fn read() -> Result<String> {
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<Self> { pub(crate) fn reshape(self) -> Result<Self> {

View file

@ -33,7 +33,7 @@ impl Flavor {
"" => String::new(), "" => String::new(),
name => { name => {
let p = Xdg::config_dir().join(format!("flavors/{name}.yazi/flavor.toml")); 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:?}"))?
} }
}) })
} }

View file

@ -1,6 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use anyhow::{Result, bail}; use anyhow::{Context, Result, bail};
use serde::Deserialize; use serde::Deserialize;
use yazi_codegen::{DeserializeOver1, DeserializeOver2}; use yazi_codegen::{DeserializeOver1, DeserializeOver2};
use yazi_fs::{Xdg, expand_path, ok_or_not_found}; use yazi_fs::{Xdg, expand_path, ok_or_not_found};
@ -207,7 +207,9 @@ pub struct Help {
impl Theme { impl Theme {
pub(crate) fn read() -> Result<String> { pub(crate) fn read() -> Result<String> {
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<Self> { pub(crate) fn reshape(mut self, light: bool) -> Result<Self> {

View file

@ -1,4 +1,4 @@
use anyhow::Result; use anyhow::{Context, Result};
use serde::Deserialize; use serde::Deserialize;
use yazi_codegen::DeserializeOver1; use yazi_codegen::DeserializeOver1;
use yazi_fs::{Xdg, ok_or_not_found}; use yazi_fs::{Xdg, ok_or_not_found};
@ -21,7 +21,9 @@ pub struct Yazi {
impl Yazi { impl Yazi {
pub(super) fn read() -> Result<String> { pub(super) fn read() -> Result<String> {
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<Self> { pub(super) fn reshape(self) -> Result<Self> {