refactor: improve file handling in loading starter projects (#3123)
Refactor the `load_starter_projects` function in `setup.py` to improve error handling when loading starter projects. Instead of using a try-except block around the `orjson.loads` function, the function now uses a `with` statement to open the file and handle any JSON decoding errors. This ensures that any errors are properly caught and a `ValueError` is raised with the appropriate error message. Additionally, the function now logs a message when a starter project is successfully loaded.
This commit is contained in:
parent
3835f8d8b6
commit
1c3ee13a85
1 changed files with 7 additions and 6 deletions
|
|
@ -343,12 +343,13 @@ def load_starter_projects() -> list[tuple[Path, dict]]:
|
|||
starter_projects = []
|
||||
folder = Path(__file__).parent / "starter_projects"
|
||||
for file in folder.glob("*.json"):
|
||||
try:
|
||||
project = orjson.loads(file.read_text(encoding="utf-8"))
|
||||
except orjson.JSONDecodeError as e:
|
||||
raise ValueError(f"Error loading starter project {file}: {e}")
|
||||
starter_projects.append((file, project))
|
||||
logger.info(f"Loaded starter project {file}")
|
||||
with open(file, "r", encoding="utf-8") as f:
|
||||
try:
|
||||
project = orjson.loads(f.read())
|
||||
starter_projects.append((file, project))
|
||||
logger.info(f"Loaded starter project {file}")
|
||||
except orjson.JSONDecodeError as e:
|
||||
raise ValueError(f"Error loading starter project {file}: {e}")
|
||||
return starter_projects
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue