From 603fb15bb454b0546df17217a8de728544038124 Mon Sep 17 00:00:00 2001 From: Denis Nutiu Date: Sun, 4 Jun 2023 19:40:15 +0300 Subject: [PATCH] add better error handling for when source path does not exists --- app/config.py | 9 +++++++++ app/converter/converter.py | 26 ++++++++++++++------------ main.py | 3 ++- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/app/config.py b/app/config.py index 0623622..9d7fa7d 100644 --- a/app/config.py +++ b/app/config.py @@ -13,6 +13,15 @@ def yaml_config_settings_source(settings: BaseSettings): return yaml.safe_load(fh) +def ensure_config_exists(): + """ + Ensure config file exist at the path specified in the environment variable CONFIG_PATH. + """ + path = os.getenv("CONFIG_PATH", "config.yaml") + if not os.path.exists(path): + raise FileNotFoundError(f"Config file not found at {path}") + + class RegexHeuristics(BaseModel): """ Regex heuristics options for applying modifying a line using regex lines. diff --git a/app/converter/converter.py b/app/converter/converter.py index a3efd2f..1e22871 100644 --- a/app/converter/converter.py +++ b/app/converter/converter.py @@ -44,18 +44,20 @@ class Converter: """ source_path = self._jekyll_posts_path output_path = Path(self._hugo_posts_path) - _, _, files = next(os.walk(source_path)) posts_converted_count = 0 - for file in files: - source_abs_path = source_path / Path(file) + try: + _, _, files = next(os.walk(source_path)) + for file in files: + source_abs_path = source_path / Path(file) - file_reader = FileReader(str(source_abs_path)) - file_writer = FileWriter(output_path.joinpath(source_abs_path.name)) + file_reader = FileReader(str(source_abs_path)) + file_writer = FileWriter(output_path.joinpath(source_abs_path.name)) - self.markdown_converter.convert_jekyll_to_hugo( - file_reader, - file_writer, - ) - posts_converted_count += 1 - - self._logger.info(f"Converted {posts_converted_count} posts! 🚀") + self.markdown_converter.convert_jekyll_to_hugo( + file_reader, + file_writer, + ) + posts_converted_count += 1 + self._logger.info(f"Converted {posts_converted_count} posts! 🚀") + except StopIteration: + self._logger.fatal(f"Source path {source_path} does not exist!") diff --git a/main.py b/main.py index d8c9127..7fac573 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,12 @@ import logging -from app.config import Configurator +from app.config import Configurator, ensure_config_exists from app.converter import Converter def main(): # Configurator + ensure_config_exists() configurator = Configurator() # Logging configuration