Skip to content

Commit 73da455

Browse files
committed
Added back-end error
1 parent 5da96f4 commit 73da455

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

tests/database.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
import os
66
import unittest
77

8-
import sqlite3
9-
108
from winevtrc import database
9+
from winevtrc import errors
1110
from winevtrc import resource_file
1211
from winevtrc import resources
1312

@@ -82,7 +81,7 @@ def testGetValues(self):
8281
generator = database_file.GetValues(
8382
[u'bogus'], [u'name', u'value'], u'')
8483

85-
with self.assertRaises(sqlite3.OperationalError):
84+
with self.assertRaises(errors.BackendError):
8685
next(generator)
8786

8887
database_file.Close()
@@ -328,7 +327,7 @@ def testGetMessages(self):
328327

329328
generator = database_reader.GetMessages(u'0x00000413', u'1.0.0.0')
330329

331-
with self.assertRaises(sqlite3.OperationalError):
330+
with self.assertRaises(errors.BackendError):
332331
list(generator)
333332

334333
database_reader.Close()
@@ -344,7 +343,7 @@ def testGetStringTables(self):
344343
generator = database_reader.GetStringTables()
345344

346345
# TODO: string tables currently not written.
347-
with self.assertRaises(sqlite3.OperationalError):
346+
with self.assertRaises(errors.BackendError):
348347
list(generator)
349348

350349
database_reader.Close()
@@ -360,7 +359,7 @@ def testGetStrings(self):
360359
generator = database_reader.GetStrings(u'0x00000409', u'1.0.0.0')
361360

362361
# TODO: string tables currently not written.
363-
with self.assertRaises(sqlite3.OperationalError):
362+
with self.assertRaises(errors.BackendError):
364363
list(generator)
365364

366365
database_reader.Close()

winevtrc/database.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import sqlite3
1212

1313
from winevtrc import definitions
14+
from winevtrc import errors
1415
from winevtrc import py2to3
1516
from winevtrc import resources
1617

@@ -56,6 +57,7 @@ def CreateTable(self, table_name, column_definitions):
5657
column_definitions (list[str]): column definitions.
5758
5859
Raises:
60+
BackendError: if the database back-end raises an exception.
5961
IOError: if the database is not opened or
6062
if the database is in read-only mode.
6163
"""
@@ -68,7 +70,10 @@ def CreateTable(self, table_name, column_definitions):
6870
sql_query = u'CREATE TABLE {0:s} ( {1:s} )'.format(
6971
table_name, u', '.join(column_definitions))
7072

71-
self._cursor.execute(sql_query)
73+
try:
74+
self._cursor.execute(sql_query)
75+
except sqlite3.OperationalError as exception:
76+
raise errors.BackendError(exception)
7277

7378
def GetValues(self, table_names, column_names, condition):
7479
"""Retrieves values from a table.
@@ -82,6 +87,7 @@ def GetValues(self, table_names, column_names, condition):
8287
generator: values generator.
8388
8489
Raises:
90+
BackendError: if the database back-end raises an exception.
8591
IOError: if the database is not opened.
8692
"""
8793
def _GetValues(cursor, table_names, column_names, condition):
@@ -102,7 +108,10 @@ def _GetValues(cursor, table_names, column_names, condition):
102108
sql_query = u'SELECT {1:s} FROM {0:s}{2:s}'.format(
103109
u', '.join(table_names), u', '.join(column_names), condition)
104110

105-
cursor.execute(sql_query)
111+
try:
112+
cursor.execute(sql_query)
113+
except sqlite3.OperationalError as exception:
114+
raise errors.BackendError(exception)
106115

107116
for row in cursor:
108117
values = {}
@@ -126,18 +135,25 @@ def HasTable(self, table_name):
126135
bool: True if the table exists, false otheriwse.
127136
128137
Raises:
138+
BackendError: if the database back-end raises an exception.
129139
IOError: if the database is not opened.
130140
"""
131141
if not self._connection:
132142
raise IOError(u'Cannot determine if table exists database not opened.')
133143

134144
sql_query = self._HAS_TABLE_QUERY.format(table_name)
135145

136-
self._cursor.execute(sql_query)
137-
if self._cursor.fetchone():
138-
has_table = True
139-
else:
140-
has_table = False
146+
try:
147+
self._cursor.execute(sql_query)
148+
149+
if self._cursor.fetchone():
150+
has_table = True
151+
else:
152+
has_table = False
153+
154+
except sqlite3.OperationalError as exception:
155+
raise errors.BackendError(exception)
156+
141157
return has_table
142158

143159
def InsertValues(self, table_name, column_names, values):
@@ -149,6 +165,7 @@ def InsertValues(self, table_name, column_names, values):
149165
values (list[str]): values formatted as a string.
150166
151167
Raises:
168+
BackendError: if the database back-end raises an exception.
152169
IOError: if the database is not opened or
153170
if the database is in read-only mode or
154171
if an unsupported value type is encountered.
@@ -181,7 +198,10 @@ def InsertValues(self, table_name, column_names, values):
181198
sql_query = u'INSERT INTO {0:s} ( {1:s} ) VALUES ( {2:s} )'.format(
182199
table_name, u', '.join(column_names), u', '.join(sql_values))
183200

184-
self._cursor.execute(sql_query)
201+
try:
202+
self._cursor.execute(sql_query)
203+
except sqlite3.OperationalError as exception:
204+
raise errors.BackendError(exception)
185205

186206
def Open(self, filename, read_only=False):
187207
"""Opens the database file.
@@ -196,6 +216,7 @@ def Open(self, filename, read_only=False):
196216
bool: True if successful or False if not.
197217
198218
Raises:
219+
BackendError: if the database back-end raises an exception.
199220
IOError: if the database is already opened.
200221
"""
201222
if self._connection:
@@ -208,7 +229,11 @@ def Open(self, filename, read_only=False):
208229
if not self._connection:
209230
return False
210231

211-
self._cursor = self._connection.cursor()
232+
try:
233+
self._cursor = self._connection.cursor()
234+
except sqlite3.OperationalError as exception:
235+
raise errors.BackendError(exception)
236+
212237
if not self._cursor:
213238
return False
214239

winevtrc/errors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# -*- coding: utf-8 -*-
2+
"""The error objects."""
3+
4+
5+
class Error(Exception):
6+
"""The error interface."""
7+
8+
9+
class BackendError(Error):
10+
"""Error that is raised for database back-end exceptions."""

0 commit comments

Comments
 (0)