diff --git a/app/app_controller.py b/app/app_controller.py index 23ccd2c..f81bab6 100644 --- a/app/app_controller.py +++ b/app/app_controller.py @@ -20,6 +20,7 @@ def copy_files_from_source_to_destination(self): StatPresenter().present_analysis_of_candidate_files(self.source_directory, self.destination_directory) if self.__user_confirmation_of_copy(): FileCopier().copy_source_files_to_destination() + self.__display_errors() FileGateway().wipe_database() # TODO dev only, remove later def __create_db_records_for_files_to_be_copied(self, destination_directory): @@ -32,3 +33,7 @@ def __user_confirmation_of_copy(self): print(f'Proceed with copy? ( y / n )') if input() == 'y': return True + + def __display_errors(self): + error_messages = Logger().write_errors_to_logfile() + print(error_messages) if error_messages else None diff --git a/app/db_controller.py b/app/db_controller.py index d4af210..5f9e01e 100644 --- a/app/db_controller.py +++ b/app/db_controller.py @@ -1,5 +1,8 @@ +import sqlite3 from sqlite3 import Error +from app.logger import Logger + class DBControllerMeta(type): _instance = {} @@ -25,8 +28,7 @@ def execute_query(self, query, values): cursor.execute(query, values) self.connection.commit() except Error as e: - print(f"Error '{e}' occurred with \nQuery {query}\nValues: {values}") - raise Error + Logger().log_error(e, values) def execute_read_query(self, query): cursor = self.connection.cursor() diff --git a/app/logger.py b/app/logger.py index 440b7bc..a8002ad 100644 --- a/app/logger.py +++ b/app/logger.py @@ -15,6 +15,8 @@ class Logger(metaclass=LoggerMeta): def __init__(self): self.log_file_path = None + self.error_messages = [] + self.combined_error_messages = '' def init_log_file(self, destination_directory): timestamp = datetime.now().strftime('%Y-%m-%d-%H%M.%S') @@ -35,6 +37,15 @@ def log_unsuccessful_copy(self, source_file_path, destination_filepath): log_entry = f'Copy failed: {source_file_path} not copied to {destination_filepath}' self.__write_to_logfile(log_entry) + def log_error(self, error, values): + message = f'Error: {error}. Values: {values}' + self.error_messages.append(message) + + def write_errors_to_logfile(self): + heading = '\nErrors:\n' + self.combined_error_messages = '\n'.join(self.error_messages) + self.__write_to_logfile(heading + self.combined_error_messages) + def __write_to_logfile(self, log_entry): with open(self.log_file_path, 'a') as file: file.write(f'{log_entry}\n') diff --git a/test/test_logger.py b/test/test_logger.py index 60472b6..d9019e7 100644 --- a/test/test_logger.py +++ b/test/test_logger.py @@ -42,3 +42,33 @@ def test_unsuccessful_write_to_logfile(): expected_entry = f'Copy failed: {source_file_path} not copied to {destination_filepath}\n' assert expected_entry == content + + +def test_logs_error(): + # reset error log as it may be populated by other tests + Logger().error_messages = [] + + values = ['/source/file1.jpg', '/destination/file1.jpg', 1024, None, False] + error = 'UNIQUE constraint failed: files.source_filepath' + expected_error_message = "Error: UNIQUE constraint failed: files.source_filepath. Values: " \ + "['/source/file1.jpg', '/destination/file1.jpg', 1024, None, False]" + Logger().log_error(error, values) + assert Logger().error_messages[0] == expected_error_message + + +def test_writes_errors_to_logfile(): + # reset error log as it may be populated by other tests + Logger().error_messages = [] + + values = ['/source/file1.jpg', '/destination/file1.jpg', 1024, None, False] + error = 'UNIQUE constraint failed: files.source_filepath' + Logger().log_error(error, values) + Logger().write_errors_to_logfile() + + with open(Logger().log_file_path, 'r') as file: + content = file.read() + + expected_content = "\nErrors:\nError: UNIQUE constraint failed: files.source_filepath. Values: " \ + "['/source/file1.jpg', '/destination/file1.jpg', 1024, None, False]\n" + + assert expected_content == content