Better exception formatting and handling.

This commit is contained in:
Florian Bruhin 2014-09-16 08:20:19 +02:00
parent 2e419d413b
commit 5eef6d422d
13 changed files with 69 additions and 83 deletions

View File

@ -29,6 +29,7 @@ import warnings
import bdb
import base64
import functools
import traceback
from PyQt5.QtWidgets import QApplication, QDialog, QMessageBox
from PyQt5.QtCore import (pyqtSlot, QTimer, QEventLoop, Qt, QStandardPaths,
@ -479,9 +480,8 @@ class Application(QApplication):
QUrl.RemovePassword | QUrl.FullyEncoded)
if url:
pages.append(url)
except Exception as e: # pylint: disable=broad-except
log.destroy.debug("Error while recovering tab: {}: {}".format(
e.__class__.__name__, e))
except Exception: # pylint: disable=broad-except
log.destroy.exception("Error while recovering tab")
return pages
def _save_geometry(self):
@ -508,8 +508,8 @@ class Application(QApplication):
self._crashlogfile.close()
try:
os.remove(self._crashlogfile.name)
except (PermissionError, FileNotFoundError) as e:
log.destroy.warning("Could not remove crash log ({})!".format(e))
except (PermissionError, FileNotFoundError):
log.destroy.exception("Could not remove crash log!")
def _exception_hook(self, exctype, excvalue, tb):
"""Handle uncaught python exceptions.
@ -524,9 +524,8 @@ class Application(QApplication):
try:
self.shutdown()
return
except Exception as e:
log.init.debug("Error while shutting down: {}: {}".format(
e.__class__.__name__, e))
except Exception:
log.init.exception("Error while shutting down")
self.quit()
return
@ -537,37 +536,32 @@ class Application(QApplication):
try:
pages = self._recover_pages()
except Exception as e:
log.destroy.debug("Error while recovering pages: {}: {}".format(
e.__class__.__name__, e))
except Exception:
log.destroy.exception("Error while recovering pages")
pages = []
try:
history = self.mainwindow.status.cmd.history[-5:]
except Exception as e:
log.destroy.debug("Error while getting history: {}: {}".format(
e.__class__.__name__, e))
except Exception:
log.destroy.exception("Error while getting history: {}")
history = []
try:
widgets = self.get_all_widgets()
except Exception as e:
log.destroy.debug("Error while getting widgets: {}: {}".format(
e.__class__.__name__, e))
except Exception:
log.destroy.exception("Error while getting widgets")
widgets = ""
try:
objects = self.get_all_objects()
except Exception as e:
log.destroy.debug("Error while getting objects: {}: {}".format(
e.__class__.__name__, e))
except Exception:
log.destroy.exception("Error while getting objects")
objects = ""
try:
self.lastWindowClosed.disconnect(self.shutdown)
except TypeError as e:
log.destroy.debug("Error while preventing shutdown: {}: {}".format(
e.__class__.__name__, e))
except TypeError:
log.destroy.exception("Error while preventing shutdown")
QApplication.closeAllWindows()
self._crashdlg = crash.ExceptionCrashDialog(pages, history, exc,
widgets, objects)
@ -632,8 +626,8 @@ class Application(QApplication):
try:
r = eval(s) # pylint: disable=eval-used
out = repr(r)
except Exception as e: # pylint: disable=broad-except
out = ': '.join([e.__class__.__name__, str(e)])
except Exception: # pylint: disable=broad-except
out = traceback.format_exc()
qutescheme.pyeval_output = out
self.mainwindow.tabs.openurl(QUrl('qute:pyeval'), newtab=True)

View File

@ -178,9 +178,9 @@ class ConfigManager(QObject):
lines.append("# {}{}:".format(optname, typestr))
try:
desc = self.sections[sectname].descriptions[optname]
except KeyError as e:
log.misc.debug("No description for {}.{}! ({}: {})".format(
sectname, optname, e.__class__.__name__, e))
except KeyError:
log.misc.exception("No description for {}.{}!".format(
sectname, optname))
continue
for descline in desc.splitlines():
lines += wrapper.wrap(descline)

View File

@ -90,8 +90,8 @@ class ColorDict(dict):
"""
try:
val = super().__getitem__(key)
except KeyError as e:
log.style.warning("No color defined for {}!".format(e))
except KeyError:
log.style.exception("No color defined for {}!")
return ''
if isinstance(val, QColor):
# This could happen when accidentaly declarding something as

View File

@ -65,6 +65,7 @@ def maybe_leave(mode, reason=None):
try:
instance().leave(mode, reason)
except ValueError as e:
# This is rather likely to happen, so we only log to debug log.
log.modes.debug(e)

View File

@ -893,7 +893,7 @@ class OurTests(AttachmentTestCase):
def setUpModule():
"""Disable logging."""
logging.disable(logging.WARNING)
logging.disable(logging.ERROR)
def tearDownModule():

View File

@ -21,6 +21,7 @@
import os.path
from qutebrowser.utils import rfc6266
from qutebrowser.utils import log
@ -47,10 +48,8 @@ def parse_content_disposition(reply):
content_disposition = rfc6266.parse_headers(
bytes(reply.rawHeader('Content-Disposition')))
filename = content_disposition.filename()
except UnicodeDecodeError as e:
log.misc.warning("Error while getting filename: {}: {}".format(
e.__class__.__name__, e))
filename = None
except UnicodeDecodeError:
log.misc.exception("Error while decoding filename")
else:
is_inline = content_disposition.is_inline()
# Then try to get filename from url

View File

@ -309,9 +309,8 @@ def parse_headers(content_disposition):
content_disposition = normalize_ws(content_disposition)
try:
parsed = peg.parse(content_disposition, ContentDispositionValue)
except (SyntaxError, DuplicateParamError, InvalidISO8859Error) as e:
log.rfc6266.warning("Error while parsing Content-Disposition: "
"{} - {}".format(e.__class__.__name__, e))
except (SyntaxError, DuplicateParamError, InvalidISO8859Error):
log.rfc6266.exception("Error while parsing Content-Disposition")
return ContentDisposition()
else:
return ContentDisposition(disposition=parsed.dtype,

View File

@ -342,12 +342,11 @@ class Question(QObject):
try:
self.aborted.emit()
self.completed.emit()
except TypeError as e:
except TypeError:
# FIXME
# We seem to get "pyqtSignal must be bound to a QObject, not
# 'Question' here, which makes no sense at all..."
log.misc.debug("Error while aborting question: {}: {}".format(
e.__class__.__name__, e))
log.misc.exception("Error while aborting question")
class Timer(QTimer):

View File

@ -64,9 +64,8 @@ def _git_str():
try:
gitpath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
os.path.pardir, os.path.pardir)
except NameError as e:
log.misc.debug("Error while getting git path: {}: {}".format(
e.__class__.__name__, e))
except NameError:
log.misc.exception("Error while getting git path")
else:
commit = _git_str_subprocess(gitpath)
if commit is not None:
@ -112,9 +111,8 @@ def _release_info():
try:
with open(fn, 'r', encoding='utf-8') as f:
data.append((fn, ''.join(f.readlines())))
except IOError as e:
log.misc.warning("Error while reading {}: {}: {}".format(
fn, e.__class__.__name__, e))
except IOError:
log.misc.exception("Error while reading {}.".format(fn))
return data
@ -133,9 +131,8 @@ def _module_versions():
try:
lines.append('SIP: {}'.format(
sipconfig.Configuration().sip_version_str))
except (AttributeError, TypeError) as e:
log.misc.warning("Error while getting SIP version: {}: {}".format(
e.__class__.__name__, e))
except (AttributeError, TypeError):
log.misc.exception("Error while getting SIP version")
lines.append('SIP: ?')
modules = collections.OrderedDict([
('colorlog', []),

View File

@ -17,11 +17,12 @@
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
# pylint: disable=broad-except
"""The dialog which gets shown when qutebrowser crashes."""
import sys
import traceback
import urllib.error
import getpass
from PyQt5.QtCore import Qt, QSize
@ -123,19 +124,17 @@ class _CrashDialog(QDialog):
try:
self._crash_info.append(("Contact info",
"User: {}".format(getpass.getuser())))
except Exception as e: # pylint: disable=broad-except
self._crash_info.append(("Contact info", "User: {}: {}".format(
e.__class__.__name__, e)))
except Exception:
self._crash_info.append(("Contact info", traceback.format_exc()))
try:
self._crash_info.append(("Version info", version.version()))
except Exception as e: # pylint: disable=broad-except
except Exception:
self._crash_info.append(("Version info", traceback.format_exc()))
try:
self._crash_info.append(("Config",
config.instance().dump_userconfig()))
except AttributeError as e:
self._crash_info.append(("Config", "{}: {}".format(
e.__class__.__name__, e)))
except Exception:
self._crash_info.append(("Config", traceback.format_exc()))
def _format_crash_info(self):
"""Format the gathered crash info to be displayed.
@ -157,7 +156,8 @@ class _CrashDialog(QDialog):
"""Paste the crash info into the pastebin."""
try:
url = utils.pastebin(self._txt.toPlainText())
except (urllib.error.URLError, ValueError) as e:
except Exception as e:
log.misc.exception("Error while paste-binning")
self._url.setText('Error while reporting: {}: {}'.format(
e.__class__.__name__, e))
return
@ -230,9 +230,8 @@ class ExceptionCrashDialog(_CrashDialog):
]
try:
self._crash_info.append(("Debug log", log.ram_handler.dump_log()))
except AttributeError as e:
self._crash_info.append(("Debug log", "{}: {}".format(
e.__class__.__name__, e)))
except Exception:
self._crash_info.append(("Debug log", traceback.format_exc()))
class FatalCrashDialog(_CrashDialog):
@ -330,6 +329,5 @@ class ReportDialog(_CrashDialog):
]
try:
self._crash_info.append(("Debug log", log.ram_handler.dump_log()))
except AttributeError as e:
self._crash_info.append(("Debug log", "{}: {}".format(
e.__class__.__name__, e)))
except Exception:
self._crash_info.append(("Debug log", traceback.format_exc()))

View File

@ -55,16 +55,17 @@ class MainWindow(QWidget):
data = stateconf['geometry']['mainwindow']
log.init.debug("Restoring mainwindow from {}".format(data))
geom = base64.b64decode(data, validate=True)
except (KeyError, binascii.Error) as e:
log.init.warning("Error while reading geometry: {}: {}".format(
e.__class__.__name__, e))
except KeyError:
# First start
pass
except binascii.Error:
log.init.exception("Error while reading geometry")
self._set_default_geometry()
else:
try:
ok = self.restoreGeometry(geom)
except KeyError:
log.init.warning("Error while restoring geometry: {}: "
"{}".format(e.__class__.__name__, e))
log.init.exception("Error while restoring geometry.")
self._set_default_geometry()
if not ok:
log.init.warning("Error while restoring geometry.")

View File

@ -224,9 +224,8 @@ class TabbedBrowser(tabwidget.TabWidget):
"""
try:
self.currentChanged.disconnect()
except TypeError as e:
log.destroy.debug("Error while shutting down tabs: {}: {}".format(
e.__class__.__name__, e))
except TypeError:
log.destroy.exception("Error while shutting down tabs")
for tab in self.widgets():
self._remove_tab(tab)

View File

@ -41,6 +41,7 @@ import argparse
import collections
import functools
import contextlib
import traceback
import colorama as col
import pep257
@ -86,8 +87,8 @@ def _run_distutils(name, args):
ep()
except SystemExit as e:
return e.code
except Exception as e:
print('{}: {}'.format(e.__class__.__name__, e))
except Exception:
traceback.print_exc()
return None
@ -95,8 +96,8 @@ def _run_subprocess(name, args):
"""Run a checker via subprocess."""
try:
return subprocess.call([name] + args)
except FileNotFoundError as e:
print('{}: {}'.format(e.__class__.__name__, e))
except FileNotFoundError:
traceback.print_exc()
return None
@ -130,9 +131,8 @@ def check_pep257(target):
status = pep257.main(*pep257.parse_options())
print()
return status
except Exception as e:
print('{}: {}'.format(e.__class__.__name__, e))
print()
except Exception:
traceback.print_exc()
return None
@ -181,9 +181,8 @@ def check_vcs_conflict(target):
ok = False
print()
return ok
except Exception as e:
print('{}: {}'.format(e.__class__.__name__, e))
print()
except Exception:
traceback.print_exc()
return None