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:
Gabriel Luiz Freitas Almeida 2024-07-31 19:11:46 -03:00 committed by GitHub
commit 1c3ee13a85
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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