当读写yaml文件出现异常时,抛出错误,方便定位
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
# 第三方库导入
|
||||
import yaml # pip install pyyaml
|
||||
from loguru import logger
|
||||
|
||||
|
||||
class YamlHandle:
|
||||
@@ -21,8 +22,15 @@ class YamlHandle:
|
||||
|
||||
@property
|
||||
def read_yaml(self):
|
||||
with open(file=self.filename, mode="r", encoding="utf-8") as fp:
|
||||
return yaml.safe_load(fp.read())
|
||||
try:
|
||||
with open(file=self.filename, mode="r", encoding="utf-8") as fp:
|
||||
return yaml.safe_load(fp.read())
|
||||
except FileNotFoundError as e:
|
||||
logger.error(f"YAML file ({self.filename}) not found: {e}")
|
||||
raise e
|
||||
except yaml.YAMLError as e:
|
||||
logger.error(f"Error while reading YAML file ({self.filename}): {e}")
|
||||
raise e
|
||||
|
||||
def write(self, data, mode="a"):
|
||||
"""
|
||||
@@ -31,5 +39,9 @@ class YamlHandle:
|
||||
:param mode: 写入模式
|
||||
:return:
|
||||
"""
|
||||
with open(self.filename, mode=mode, encoding="utf-8") as f:
|
||||
yaml.dump(data, f)
|
||||
try:
|
||||
with open(self.filename, mode=mode, encoding="utf-8") as f:
|
||||
yaml.dump(data, f)
|
||||
except yaml.YAMLError as e:
|
||||
logger.error(f"Error while writing to YAML file ({self.filename}): {e}")
|
||||
raise e
|
||||
|
||||
Reference in New Issue
Block a user