Better error handling.

pull/61/head
Christian T. Jacobs 2017-07-03 22:35:28 +01:00
rodzic a4452f8c17
commit 6d0b02b83e
4 zmienionych plików z 30 dodań i 30 usunięć

Wyświetl plik

@ -76,7 +76,7 @@ class Log(Gtk.ListStore):
""" Check whether each field name in AVAILABLE_FIELD_NAMES_ORDERED is in the database table. If not, add it
(with all entries being set to an empty string initially).
:raises sqlite.Error, IndexError: if the existing database column names could not be obtained, or missing column names could not be added.
:raises sqlite.Error, IndexError: If the existing database column names could not be obtained, or missing column names could not be added.
"""
logging.debug("Adding any missing database columns...")
@ -186,7 +186,7 @@ class Log(Gtk.ListStore):
:arg int index: The index of the record in the SQL database.
:arg iter: The iterator pointing to the record to be deleted in the Gtk.ListStore. If the default value of None is used, only the database entry is deleted and the corresponding Gtk.ListStore is left alone.
:raises sqlite.Error, IndexError: if the record could not be deleted.
:raises sqlite.Error, IndexError: If the record could not be deleted.
"""
logging.debug("Deleting record from log...")
@ -211,7 +211,7 @@ class Log(Gtk.ListStore):
:arg str data: The data that should replace the current data in the field.
:arg iter: The iterator pointing to the record to be edited in the Gtk.ListStore. If the default value of None is used, only the database entry is edited and the corresponding Gtk.ListStore is left alone.
:arg column_index: The index of the column in the Gtk.ListStore to be edited. If the default value of None is used, only the database entry is edited and the corresponding Gtk.ListStore is left alone.
:raises sqlite.Error, IndexError: if the record could not be edited.
:raises sqlite.Error, IndexError: If the record could not be edited.
"""
logging.debug("Editing field '%s' in record %d..." % (field_name, index))
with self.connection:

Wyświetl plik

@ -121,12 +121,14 @@ class Logbook:
self.path = path
logging.debug("Retrieving all the logs in the logbook...")
self.logs = self.get_logs()
if(self.logs is None):
try:
self.logs = self.get_logs()
except (sqlite.Error, IndexError) as e:
logging.exception(e)
error(parent=self.application.window, message="Could not open logbook. Something went wrong when trying to retrieve the logs. Perhaps the logbook file is encrypted, corrupted, or in the wrong format?")
return False
else:
logging.debug("All logs retrieved successfully.")
logging.debug("All logs retrieved successfully.")
logging.debug("Rendering logs...")
# For rendering the logs. One treeview and one treeselection per Log.
@ -672,8 +674,8 @@ class Logbook:
try:
records = log.records
except sqlite.Error as e:
error(parent=self.application.window, message="Could not retrieve the records from the SQL database. No records have been exported.")
logging.exception(e)
error(parent=self.application.window, message="Could not retrieve the records from the SQL database. No records have been exported.")
return
# Write the records.
@ -684,8 +686,8 @@ class Logbook:
except IOError as e:
error(parent=self.application.window, message="Could not export the records. I/O error %d: %s" % (e.errno, e.strerror))
except Exception as e: # All other exceptions.
error(parent=self.application.window, message="Could not export the records.")
logging.exception(e)
error(parent=self.application.window, message="Could not export the records.")
return
@ -742,8 +744,8 @@ class Logbook:
try:
records = log.records
except sqlite.Error as e:
error(parent=self.application.window, message="Could not retrieve the records from the SQL database. No records have been exported.")
logging.exception(e)
error(parent=self.application.window, message="Could not retrieve the records from the SQL database. No records have been exported.")
return
# Write the records.
@ -754,8 +756,8 @@ class Logbook:
except IOError as e:
error(parent=self.application.window, message="Could not export the records. I/O error %d: %s" % (e.errno, e.strerror))
except Exception as e: # All other exceptions.
error(parent=self.application.window, message="Could not export the records.")
logging.exception(e)
error(parent=self.application.window, message="Could not export the records.")
return
@ -774,8 +776,8 @@ class Logbook:
try:
records = log.records
except sqlite.Error as e:
error(parent=self.application.window, message="Could not retrieve the records from the SQL database. No records have been printed.")
logging.exception(e)
error(parent=self.application.window, message="Could not retrieve the records from the SQL database. No records have been printed.")
return
# Print the records.
@ -842,8 +844,8 @@ class Logbook:
try:
log.add_record(fields_and_data)
except (sqlite.Error, IndexError) as e:
error(parent=self.application.window, message="Could not add the record to the log.")
logging.exception(e)
error(parent=self.application.window, message="Could not add the record to the log.")
# Scroll to the new record's row in the treeview (but don't select it).
try:
@ -894,8 +896,8 @@ class Logbook:
try:
log.delete_record(row_index, iter=child_iter)
except (sqlite.Error, IndexError) as e:
error(parent=self.application.window, message="Could not delete the record from the log.")
logging.exception(e)
error(parent=self.application.window, message="Could not delete the record from the log.")
# Update summary, etc.
self.summary.update()
@ -963,8 +965,8 @@ class Logbook:
# We add 1 onto the column_index here because we don't want to consider the index column.
log.edit_record(row_index, field_names[i], fields_and_data[field_names[i]], iter=child_iter, column_index=i+1)
except(sqlite.Error, IndexError) as e:
error(parent=rd.dialog, message="Could not edit record %d." % row_index)
logging.exception(e)
error(parent=rd.dialog, message="Could not edit record %d." % row_index)
# Update summary, etc.
self.summary.update()
@ -1017,8 +1019,8 @@ class Logbook:
record_count = log.record_count
info(parent=self.application.window, message="Log '%s' contains %d records." % (log.name, record_count))
except sqlite.Error as e:
error(parent=self.application.window, message="Could not get the record count for '%s' because of a database error." % log.name)
logging.exception(e)
error(parent=self.application.window, message="Could not get the record count for '%s' because of a database error." % log.name)
return
@ -1090,17 +1092,14 @@ class Logbook:
:returns: A list containing all the logs in the logbook, or None if the retrieval was unsuccessful.
:rtype: list
:raises sqlite.Error: If the log names could not be determined from the sqlite_master table in the database.
"""
logs = []
try:
with self.connection:
c = self.connection.cursor()
c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT GLOB 'sqlite_*'")
for name in c:
l = Log(self.connection, name[0])
l.populate()
logs.append(l)
except (sqlite.Error, IndexError) as e:
logging.exception(e)
return None
with self.connection:
c = self.connection.cursor()
c.execute("SELECT name FROM sqlite_master WHERE type='table' AND name NOT GLOB 'sqlite_*'")
for name in c:
l = Log(self.connection, name[0])
l.populate()
logs.append(l)
return logs

Wyświetl plik

@ -402,7 +402,7 @@ class RecordDialog:
raise ValueError("Unknown callsign database: %s" % database)
except ValueError as e:
logging.exception(e)
error(e)
error(parent=self.dialog, message=e)
return
# Get username and password from configuration file

Wyświetl plik

@ -74,10 +74,11 @@ class TestLogbook(unittest.TestCase):
@mock.patch('pyqso.auxiliary_dialogs.handle_gtk_dialog')
def test_open_invalid_logbook(self, mock_handle_gtk_dialog):
""" Open an invalid database file (comprising only one line of plain text) and check that an error occurs. """
invalid_logbook = Logbook(application=mock.MagicMock())
path_to_invalid_database = os.path.join(os.path.realpath(os.path.dirname(__file__)), os.pardir, "res/invalid.db")
opened = self.logbook.open(path=path_to_invalid_database)
opened = invalid_logbook.open(path=path_to_invalid_database)
assert(not opened)
assert(self.logbook.logs is None)
assert(not invalid_logbook.logs)
@mock.patch('pyqso.logbook.Logbook.render_log')
@mock.patch('pyqso.auxiliary_dialogs.handle_gtk_dialog')