# Buggy code
def raise_sqlite_error(msg, error):
    """Raise either a SqlBugError or SqlEnvironmentError."""
    error_code = error.nativeErrorCode()
    database_text = error.databaseText()
    driver_text = error.driverText()

    log.sql.debug("SQL error:")
    log.sql.debug("type: {}".format(debug.qenum_key(QSqlError, error.type())))
    log.sql.debug("database text: {}".format(database_text))
    log.sql.debug("driver text: {}".format(driver_text))
    log.sql.debug("error code: {}".format(error_code))

    environmental_errors = [
        SqliteErrorCode.BUSY,
        SqliteErrorCode.READONLY,
        SqliteErrorCode.IOERR,
        SqliteErrorCode.CORRUPT,
        SqliteErrorCode.FULL,
        SqliteErrorCode.CANTOPEN,
    ]

    # WORKAROUND for https://bugreports.qt.io/browse/QTBUG-70506
    # We don't know what the actual error was, but let's assume it's not us to
    # blame... Usually this is something like an unreadable database file.
    qtbug_70506 = (
        error_code == SqliteErrorCode.UNKNOWN
        and driver_text == "Error opening database"
        and database_text == "out of memory"
    )

    if error_code in environmental_errors or qtbug_70506:
        raise SqlEnvironmentError(msg, error)
    if (
        error_code == SqliteErrorCode.ERROR
        and driver_text == "Unable to execute statement"
        and database_text.startswith("Expression tree is too large")
    ):
        raise SqlLongQueryError(msg, error)
    raise SqlBugError(msg, error)


# Traceback (qutebrowser.misc.sql.SqlEnvironmentError)
00:59:51 DEBUG    completion completionmodel:set_pattern:179 Setting completion pattern ''
00:59:51 DEBUG    sql        sql:__init__:174 Preparing SQL query: "SELECT url, title, strftime('%Y-%m-%d', last_atime, 'unixepoch', 'localtime') FROM CompletionHistory WHERE ((url || title) LIKE :0 escape '\')  ORDER BY last_atime DESC"
00:59:51 DEBUG    sql        sql:run:207 Running SQL query: "SELECT url, title, strftime('%Y-%m-%d', last_atime, 'unixepoch', 'localtime') FROM CompletionHistory WHERE ((url || title) LIKE :0 escape '\')  ORDER BY last_atime DESC"
00:59:51 DEBUG    sql        sql:run:210 query bindings: {':0': '%%'}
00:59:51 DEBUG    sql        sql:raise_sqlite_error:94 SQL error:
00:59:51 DEBUG    sql        sql:raise_sqlite_error:96 type: ConnectionError
00:59:51 DEBUG    sql        sql:raise_sqlite_error:97 database text: database disk image is malformed
00:59:51 DEBUG    sql        sql:raise_sqlite_error:98 driver text: Unable to fetch row
00:59:51 DEBUG    sql        sql:raise_sqlite_error:99 error code: 11
00:59:51 DEBUG    sql        debug:__exit__:265 Running completion query took 0.000379 seconds.
00:59:51 DEBUG    completion debug:__exit__:265 Set pattern  took 0.001134 seconds.
00:59:51 ERROR    misc       crashsignal:exception_hook:214 Uncaught exception
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/qutebrowser/completion/completer.py", line 260, in _update_completion
completion.set_pattern(pattern)
File "/usr/lib/python3.7/site-packages/qutebrowser/completion/completionwidget.py", line 320, in set_pattern
self.model().set_pattern(pattern)
File "/usr/lib/python3.7/site-packages/qutebrowser/completion/models/completionmodel.py", line 185, in set_pattern
cat.set_pattern(pattern)
File "/usr/lib/python3.7/site-packages/qutebrowser/completion/models/histcategory.py", line 102, in set_pattern
str(i): w for i, w in enumerate(words)})
File "/usr/lib/python3.7/site-packages/qutebrowser/misc/sql.py", line 213, in run
self._check_ok('exec', ok)
File "/usr/lib/python3.7/site-packages/qutebrowser/misc/sql.py", line 196, in _check_ok
raise_sqlite_error(msg, error)
File "/usr/lib/python3.7/site-packages/qutebrowser/misc/sql.py", line 118, in raise_sqlite_error
raise SqlEnvironmentError(msg, error)
qutebrowser.misc.sql.SqlEnvironmentError: Failed to exec query "SELECT url, title, strftime('%Y-%m-%d', last_atime, 'unixepoch', 'localtime') FROM CompletionHistory WHERE ((url || title) LIKE :0 escape '\')  ORDER BY last_atime DESC": "database disk image is malformed Unable to fetch row"

# Fixed code
def raise_sqlite_error(msg, error):
    """Raise either a SqlBugError or SqlKnownError."""
    error_code = error.nativeErrorCode()
    database_text = error.databaseText()
    driver_text = error.driverText()

    log.sql.debug("SQL error:")
    log.sql.debug("type: {}".format(debug.qenum_key(QSqlError, error.type())))
    log.sql.debug("database text: {}".format(database_text))
    log.sql.debug("driver text: {}".format(driver_text))
    log.sql.debug("error code: {}".format(error_code))

    environmental_errors = [
        SqliteErrorCode.BUSY,
        SqliteErrorCode.READONLY,
        SqliteErrorCode.IOERR,
        SqliteErrorCode.CORRUPT,
        SqliteErrorCode.FULL,
        SqliteErrorCode.CANTOPEN,
    ]

    # WORKAROUND for https://bugreports.qt.io/browse/QTBUG-70506
    # We don't know what the actual error was, but let's assume it's not us to
    # blame... Usually this is something like an unreadable database file.
    qtbug_70506 = (
        error_code == SqliteErrorCode.UNKNOWN
        and driver_text == "Error opening database"
        and database_text == "out of memory"
    )

    if (error_code in environmental_errors or qtbug_70506) or (
        error_code == SqliteErrorCode.ERROR
        and driver_text == "Unable to execute statement"
        and database_text.startswith("Expression tree is too large")
    ):
        raise SqlKnownError(msg, error)

    raise SqlBugError(msg, error)