# Buggy code
def __init__(self, msg, error):
    super().__init__(msg)
    self.error = error

    log.sql.debug("SQL error:")
    log.sql.debug("type: {}".format(debug.qenum_key(QSqlError, error.type())))
    log.sql.debug("database text: {}".format(error.databaseText()))
    log.sql.debug("driver text: {}".format(error.driverText()))
    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 = [
        "8",  # SQLITE_READONLY
        "9",  # SQLITE_LOCKED,
        "13",  # SQLITE_FULL,
    ]
    self.environmental = error.nativeErrorCode() in environmental_errors


# Traceback (qutebrowser.misc.sql.SqlException)
20:42:34 DEBUG    sql        sql:run:99 Running SQL query: "SELECT EXISTS(SELECT * FROM History WHERE url = :val)"
20:42:34 DEBUG    sql        sql:run:102 query bindings: {':val': 'https://wiki.archlinux.org/index.php/ArchWiki:Privacy_policy'}
20:42:34 DEBUG    sql        debug:__exit__:264 Historycontains took 0.000158 seconds.
20:42:34 DEBUG    sql        sql:run:99 Running SQL query: "SELECT EXISTS(SELECT * FROM History WHERE url = :val)"
20:42:34 DEBUG    sql        sql:run:102 query bindings: {':val': 'https://wiki.archlinux.org/index.php/ArchWiki:About'}
20:42:34 DEBUG    sql        debug:__exit__:264 Historycontains took 0.000111 seconds.
20:42:34 DEBUG    sql        sql:run:99 Running SQL query: "SELECT EXISTS(SELECT * FROM History WHERE url = :val)"
20:42:34 DEBUG    sql        sql:run:102 query bindings: {':val': 'https://wiki.archlinux.org/index.php/ArchWiki:General_disclaimer'}
20:42:34 DEBUG    sql        debug:__exit__:264 Historycontains took 9.4e-05 seconds.
20:42:34 DEBUG    sql        sql:run:99 Running SQL query: "SELECT EXISTS(SELECT * FROM History WHERE url = :val)"
20:42:34 DEBUG    sql        sql:run:102 query bindings: {':val': 'https://wiki.archlinux.org/index.php?title=Tor&amp;mobileaction=toggle_view_mobile'}
20:42:34 DEBUG    sql        debug:__exit__:264 Historycontains took 0.000114 seconds.
20:42:34 DEBUG    sql        sql:__init__:80 Preparing SQL query: "INSERT INTO History (url, title, atime, redirect) values(:url, :title, :atime, :redirect)"
20:42:34 DEBUG    sql        sql:run:99 Running SQL query: "INSERT INTO History (url, title, atime, redirect) values(:url, :title, :atime, :redirect)"
20:42:34 DEBUG    sql        sql:run:102 query bindings: {':atime': 1501461754, ':redirect': False, ':title': 'Tor - ArchWiki', ':url': 'https://wiki.archlinux.org/index.php/Tor#Usage'}
20:42:34 ERROR    misc       crashsignal:exception_hook:210 Uncaught exception
Traceback (most recent call last):
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/history.py", line 144, in add_from_tab
self.add_url(url, title)
File "/usr/lib/python3.6/site-packages/qutebrowser/browser/history.py", line 166, in add_url
'redirect': redirect})
File "/usr/lib/python3.6/site-packages/qutebrowser/misc/sql.py", line 214, in insert
q.run(**values)
File "/usr/lib/python3.6/site-packages/qutebrowser/misc/sql.py", line 105, in run
self.lastQuery(), self.lastError().text()))
qutebrowser.misc.sql.SqlException: Failed to exec query "INSERT INTO History (url, title, atime, redirect) values(:url, :title, :atime, :redirect)": "database is locked Unable to fetch row"

# Fixed code
def __init__(self, msg, error):
    super().__init__(msg)
    self.error = error

    log.sql.debug("SQL error:")
    log.sql.debug("type: {}".format(debug.qenum_key(QSqlError, error.type())))
    log.sql.debug("database text: {}".format(error.databaseText()))
    log.sql.debug("driver text: {}".format(error.driverText()))
    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 = [
        "5",  # SQLITE_BUSY ("database is locked")
        "8",  # SQLITE_READONLY
        "13",  # SQLITE_FULL
    ]
    self.environmental = error.nativeErrorCode() in environmental_errors