Skip to content

Commit

Permalink
feat: log errors to logfile, display to user
Browse files Browse the repository at this point in the history
  • Loading branch information
roryai committed Jan 9, 2024
1 parent 5a353e2 commit 22a7211
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/app_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
6 changes: 4 additions & 2 deletions app/db_controller.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import sqlite3
from sqlite3 import Error

from app.logger import Logger


class DBControllerMeta(type):
_instance = {}
Expand All @@ -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()
Expand Down
11 changes: 11 additions & 0 deletions app/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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')
30 changes: 30 additions & 0 deletions test/test_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 22a7211

Please sign in to comment.