From b428eff10e5faaeaf0a91de9911331e2d5e11755 Mon Sep 17 00:00:00 2001
From: GyDi <segydi@foxmail.com>
Date: Tue, 14 Jun 2022 01:40:02 +0800
Subject: [PATCH] feat: improve yaml file error log

---
 src-tauri/src/utils/config.rs | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/src-tauri/src/utils/config.rs b/src-tauri/src/utils/config.rs
index 7b7a357..38c03e0 100644
--- a/src-tauri/src/utils/config.rs
+++ b/src-tauri/src/utils/config.rs
@@ -4,8 +4,20 @@ use std::{fs, path::PathBuf};
 
 /// read data from yaml as struct T
 pub fn read_yaml<T: DeserializeOwned + Default>(path: PathBuf) -> T {
-  let yaml_str = fs::read_to_string(path).unwrap_or("".into());
-  serde_yaml::from_str::<T>(&yaml_str).unwrap_or(T::default())
+  if !path.exists() {
+    log::error!("file not found \"{}\"", path.display());
+    return T::default();
+  }
+
+  let yaml_str = fs::read_to_string(&path).unwrap_or("".into());
+
+  match serde_yaml::from_str::<T>(&yaml_str) {
+    Ok(val) => val,
+    Err(_) => {
+      log::error!("failed to read yaml file \"{}\"", path.display());
+      T::default()
+    }
+  }
 }
 
 /// save the data to the file
-- 
GitLab