Define names for sqlite error codes

This commit is contained in:
Florian Bruhin 2018-09-01 21:31:01 +02:00
parent f5c92ded41
commit 37396d68f3
3 changed files with 26 additions and 11 deletions

View File

@ -27,6 +27,23 @@ from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlError
from qutebrowser.utils import log, debug from qutebrowser.utils import log, debug
class SqliteErrorCode:
"""Error codes as used by sqlite.
See https://sqlite.org/rescode.html - note we only define the codes we use
in qutebrowser here.
"""
BUSY = '5' # database is locked
READONLY = '8' # attempt to write a readonly database
IOERR = '10' # disk I/O error
CORRUPT = '11' # database disk image is malformed
FULL = '13' # database or disk is full
CANTOPEN = '14' # unable to open database file
CONSTRAINT = '19' # UNIQUE constraint failed
class SqlError(Exception): class SqlError(Exception):
"""Base class for all SQL related errors.""" """Base class for all SQL related errors."""
@ -75,16 +92,13 @@ def raise_sqlite_error(msg, error):
log.sql.debug("database text: {}".format(error.databaseText())) log.sql.debug("database text: {}".format(error.databaseText()))
log.sql.debug("driver text: {}".format(error.driverText())) log.sql.debug("driver text: {}".format(error.driverText()))
log.sql.debug("error code: {}".format(error.nativeErrorCode())) log.sql.debug("error code: {}".format(error.nativeErrorCode()))
# https://sqlite.org/rescode.html
# https://github.com/qutebrowser/qutebrowser/issues/2930
# https://github.com/qutebrowser/qutebrowser/issues/3004
environmental_errors = [ environmental_errors = [
'5', # SQLITE_BUSY ("database is locked") SqliteErrorCode.BUSY,
'8', # SQLITE_READONLY ("attempt to write a readonly database") SqliteErrorCode.READONLY,
'10', # SQLITE_IOERR ("disk I/O error") SqliteErrorCode.IOERR,
'11', # SQLITE_CORRUPT ("database disk image is malformed") SqliteErrorCode.CORRUPT,
'13', # SQLITE_FULL ("database or disk is full") SqliteErrorCode.FULL,
'14', # SQLITE_CANTOPEN ("unable to open database file") SqliteErrorCode.CANTOPEN,
] ]
# At least in init(), we can get errors like this: # At least in init(), we can get errors like this:
# > type: ConnectionError # > type: ConnectionError

View File

@ -64,6 +64,7 @@ def whitelist_generator(): # noqa
yield 'qutebrowser.utils.debug.qflags_key' yield 'qutebrowser.utils.debug.qflags_key'
yield 'qutebrowser.utils.qtutils.QtOSError.qt_errno' yield 'qutebrowser.utils.qtutils.QtOSError.qt_errno'
yield 'scripts.utils.bg_colors' yield 'scripts.utils.bg_colors'
yield 'qutebrowser.misc.sql.SqliteErrorCode.CONSTRAINT'
# Qt attributes # Qt attributes
yield 'PyQt5.QtWebKit.QWebPage.ErrorPageExtensionReturn().baseUrl' yield 'PyQt5.QtWebKit.QWebPage.ErrorPageExtensionReturn().baseUrl'

View File

@ -40,8 +40,8 @@ def test_sqlerror(klass):
class TestSqlError: class TestSqlError:
@pytest.mark.parametrize('error_code, exception', [ @pytest.mark.parametrize('error_code, exception', [
('5', sql.SqlEnvironmentError), # SQLITE_BUSY (sql.SqliteErrorCode.BUSY, sql.SqlEnvironmentError),
('19', sql.SqlBugError), # SQLITE_CONSTRAINT (sql.SqliteErrorCode.CONSTRAINT, sql.SqlBugError),
]) ])
def test_environmental(self, error_code, exception): def test_environmental(self, error_code, exception):
sql_err = QSqlError("driver text", "db text", QSqlError.UnknownError, sql_err = QSqlError("driver text", "db text", QSqlError.UnknownError,