🐛 fix(utils.py): improve error handling and file type detection in load_file_into_dict function
This commit is contained in:
parent
2a61d60aab
commit
3441bb4e5b
1 changed files with 9 additions and 11 deletions
|
|
@ -16,17 +16,15 @@ def load_file_into_dict(file_path: str) -> dict:
|
||||||
if not os.path.exists(file_path):
|
if not os.path.exists(file_path):
|
||||||
raise FileNotFoundError(f"File not found: {file_path}")
|
raise FileNotFoundError(f"File not found: {file_path}")
|
||||||
|
|
||||||
file_extension = os.path.splitext(file_path)[1].lower()
|
# Files names are UUID, so we can't find the extension
|
||||||
|
with open(file_path, "r") as file:
|
||||||
if file_extension == ".json":
|
try:
|
||||||
with open(file_path, "r") as json_file:
|
data = json.load(file)
|
||||||
data = json.load(json_file)
|
except json.JSONDecodeError:
|
||||||
elif file_extension in [".yaml", ".yml"]:
|
file.seek(0)
|
||||||
with open(file_path, "r") as yaml_file:
|
data = yaml.safe_load(file)
|
||||||
data = yaml.safe_load(yaml_file)
|
except ValueError as exc:
|
||||||
else:
|
raise ValueError("Invalid file type. Expected .json or .yaml.") from exc
|
||||||
raise ValueError("Unsupported file type. Please provide a JSON or YAML file.")
|
|
||||||
|
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue