fix(errors): add file path context to all IO errors in ConfigError (#2412)

This commit is contained in:
Imbris 2023-04-28 10:53:46 -04:00 committed by GitHub
parent 0e379fe15a
commit 95bfff1934
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 11 deletions

View file

@ -83,9 +83,6 @@ pub enum ConfigError {
KdlDeserializationError(#[from] kdl::KdlError), KdlDeserializationError(#[from] kdl::KdlError),
#[error("KdlDeserialization error: {0}")] #[error("KdlDeserialization error: {0}")]
KdlError(KdlError), // TODO: consolidate these KdlError(KdlError), // TODO: consolidate these
// Io error
#[error("IoError: {0}")]
Io(#[from] io::Error),
#[error("Config error: {0}")] #[error("Config error: {0}")]
Std(#[from] Box<dyn std::error::Error>), Std(#[from] Box<dyn std::error::Error>),
// Io error with path context // Io error with path context

View file

@ -730,7 +730,9 @@ impl Layout {
let swap_layout_and_path = Layout::swap_layout_and_path(&layout_path); let swap_layout_and_path = Layout::swap_layout_and_path(&layout_path);
let mut kdl_layout = String::new(); let mut kdl_layout = String::new();
layout_file.read_to_string(&mut kdl_layout)?; layout_file
.read_to_string(&mut kdl_layout)
.map_err(|e| ConfigError::IoPath(e, layout_path.into()))?;
Ok(( Ok((
layout_path.as_os_str().to_string_lossy().into(), layout_path.as_os_str().to_string_lossy().into(),
kdl_layout, kdl_layout,

View file

@ -10,8 +10,6 @@ use crate::input::theme::{FrameConfig, Theme, Themes, UiConfig};
use crate::setup::{find_default_config_dir, get_layout_dir}; use crate::setup::{find_default_config_dir, get_layout_dir};
use kdl_layout_parser::KdlLayoutParser; use kdl_layout_parser::KdlLayoutParser;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use strum::IntoEnumIterator; use strum::IntoEnumIterator;
use miette::NamedSource; use miette::NamedSource;
@ -1746,9 +1744,8 @@ impl Themes {
pub fn from_path(path_to_theme_file: PathBuf) -> Result<Self, ConfigError> { pub fn from_path(path_to_theme_file: PathBuf) -> Result<Self, ConfigError> {
// String is the theme name // String is the theme name
let mut file = File::open(path_to_theme_file.clone())?; let kdl_config = std::fs::read_to_string(&path_to_theme_file)
let mut kdl_config = String::new(); .map_err(|e| ConfigError::IoPath(e, path_to_theme_file.into()))?;
file.read_to_string(&mut kdl_config)?;
let kdl_config: KdlDocument = kdl_config.parse()?; let kdl_config: KdlDocument = kdl_config.parse()?;
let kdl_themes = kdl_config.get("themes").ok_or(ConfigError::new_kdl_error( let kdl_themes = kdl_config.get("themes").ok_or(ConfigError::new_kdl_error(
"No theme node found in file".into(), "No theme node found in file".into(),
@ -1761,8 +1758,10 @@ impl Themes {
pub fn from_dir(path_to_theme_dir: PathBuf) -> Result<Self, ConfigError> { pub fn from_dir(path_to_theme_dir: PathBuf) -> Result<Self, ConfigError> {
let mut themes = Themes::default(); let mut themes = Themes::default();
for entry in std::fs::read_dir(path_to_theme_dir)? { for entry in std::fs::read_dir(&path_to_theme_dir)
let entry = entry?; .map_err(|e| ConfigError::IoPath(e, path_to_theme_dir.clone()))?
{
let entry = entry.map_err(|e| ConfigError::IoPath(e, path_to_theme_dir.clone()))?;
let path = entry.path(); let path = entry.path();
if let Some(extension) = path.extension() { if let Some(extension) = path.extension() {
if extension == "kdl" { if extension == "kdl" {