anki/qt/aqt/errors.py

130 lines
4.3 KiB
Python
Raw Normal View History

2019-02-05 04:59:03 +01:00
# Copyright: Ankitects Pty Ltd and contributors
# License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html
2017-12-11 07:20:00 +01:00
import html
import re
2019-12-20 10:19:03 +01:00
import sys
import traceback
from typing import Optional, TextIO, cast
from markdown import markdown
2019-12-20 10:19:03 +01:00
from aqt import mw
from aqt.main import AnkiQt
from aqt.qt import *
from aqt.utils import showText, showWarning, supportText, tr
if not os.environ.get("DEBUG"):
2019-12-23 01:34:10 +01:00
def excepthook(etype, val, tb) -> None: # type: ignore
2019-12-23 01:34:10 +01:00
sys.stderr.write(
"Caught exception:\n%s\n"
% ("".join(traceback.format_exception(etype, val, tb)))
)
sys.excepthook = excepthook
2019-12-23 01:34:10 +01:00
class ErrorHandler(QObject):
"Catch stderr and write into buffer."
ivl = 100
errorTimer = pyqtSignal()
def __init__(self, mw: AnkiQt) -> None:
QObject.__init__(self, mw)
self.mw = mw
self.timer: Optional[QTimer] = None
qconnect(self.errorTimer, self._setTimer)
self.pool = ""
self._oldstderr = sys.stderr
sys.stderr = cast(TextIO, self)
def unload(self) -> None:
sys.stderr = self._oldstderr
sys.excepthook = None
def write(self, data: str) -> None:
# dump to stdout
sys.stdout.write(data)
# save in buffer
self.pool += data
# and update timer
self.setTimer()
def setTimer(self) -> None:
# we can't create a timer from a different thread, so we post a
# message to the object on the main thread
self.errorTimer.emit() # type: ignore
def _setTimer(self) -> None:
if not self.timer:
self.timer = QTimer(self.mw)
qconnect(self.timer.timeout, self.onTimeout)
self.timer.setInterval(self.ivl)
self.timer.setSingleShot(True)
self.timer.start()
2021-02-01 14:28:21 +01:00
def tempFolderMsg(self) -> str:
2021-03-26 04:48:26 +01:00
return tr.qt_misc_unable_to_access_anki_media_folder()
def onTimeout(self) -> None:
2017-12-11 07:20:00 +01:00
error = html.escape(self.pool)
self.pool = ""
self.mw.progress.clear()
if "AbortSchemaModification" in error:
return
if "DeprecationWarning" in error:
return
2017-07-09 05:26:50 +02:00
if "10013" in error:
2021-03-26 04:48:26 +01:00
showWarning(tr.qt_misc_your_firewall_or_antivirus_program_is())
return
if "invalidTempFolder" in error:
showWarning(self.tempFolderMsg())
return
2016-07-04 10:06:08 +02:00
if "Beautiful Soup is not an HTTP client" in error:
return
2019-04-23 02:19:05 +02:00
if "database or disk is full" in error or "Errno 28" in error:
2021-03-26 04:48:26 +01:00
showWarning(tr.qt_misc_your_computers_storage_may_be_full())
return
if "disk I/O error" in error:
2021-03-26 04:48:26 +01:00
showWarning(markdown(tr.errors_accessing_db()))
return
must_close = False
if "PanicException" in error:
must_close = True
txt = markdown(
"**A fatal error occurred, and Anki must close. Please report this message on the forums.**"
)
error = f"{supportText() + self._addonText(error)}\n{error}"
elif self.mw.addonManager.dirty:
# Older translations include a link to the old discussions site; rewrite it to a newer one
message = tr.errors_addons_active_popup().replace(
"https://help.ankiweb.net/discussions/add-ons/",
"https://forums.ankiweb.net/c/add-ons/11",
)
txt = markdown(message)
error = f"{supportText() + self._addonText(error)}\n{error}"
else:
2021-03-26 04:48:26 +01:00
txt = markdown(tr.errors_standard_popup())
error = f"{supportText()}\n{error}"
2019-12-23 01:34:10 +01:00
# show dialog
txt = f"{txt}<div style='white-space: pre-wrap'>{error}</div>"
showText(txt, type="html", copyBtn=True)
if must_close:
sys.exit(1)
2021-02-01 14:28:21 +01:00
def _addonText(self, error: str) -> str:
matches = re.findall(r"addons21/(.*?)/", error)
if not matches:
return ""
# reverse to list most likely suspect first, dict to deduplicate:
2019-12-23 01:34:10 +01:00
addons = [
mw.addonManager.addonName(i) for i in dict.fromkeys(reversed(matches))
]
# highlight importance of first add-on:
addons[0] = f"<b>{addons[0]}</b>"
addons_str = ", ".join(addons)
return f"{tr.addons_possibly_involved(addons=addons_str)}\n"