Improve error handler

Now it will only pastebin the error when it has finished gathering it
all.
This commit is contained in:
Emanuel Barry 2015-08-27 10:02:56 -04:00
parent 3dc3e4d172
commit b3f30bbd91
2 changed files with 14 additions and 12 deletions

View File

@ -120,7 +120,7 @@ class ErrorHandler(io.TextIOWrapper):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.cli = None self.cli = None
self.target_logger = None self.target_logger = None
self.data = [] self.data = None
def write(self, data): def write(self, data):
assert not (self.cli is None or self.target_logger is None) assert not (self.cli is None or self.target_logger is None)
@ -131,15 +131,17 @@ class ErrorHandler(io.TextIOWrapper):
length = len(data) length = len(data)
b = data.encode("utf-8", "replace") b = data.encode("utf-8", "replace")
self.buffer.write(b) self.buffer.write(b)
self.data.append(data) self.data = data
if data and not data.startswith(("Traceback", " ", "Exception in thread")):
self.flush() self.flush()
return length return length
def flush(self): def flush(self):
self.buffer.flush() self.buffer.flush()
exc = self.data[-1].partition(":")[0] if self.data is None:
return
exc = self.data.rstrip().splitlines()[-1].partition(":")[0]
import builtins import builtins
@ -156,8 +158,8 @@ class ErrorHandler(io.TextIOWrapper):
elif hasattr(builtins, exc): elif hasattr(builtins, exc):
exc = getattr(builtins, exc) exc = getattr(builtins, exc)
if not issubclass(exc, Exception): if not isinstance(exc, type) or not issubclass(exc, Exception):
del self.data[:] self.data = None
return # not an actual exception return # not an actual exception
msg = "An error has occurred and has been logged." msg = "An error has occurred and has been logged."
@ -170,10 +172,10 @@ class ErrorHandler(io.TextIOWrapper):
sock.send(b"".join(s.encode("utf-8", "replace") for s in self.data) + b"\n") sock.send(b"".join(s.encode("utf-8", "replace") for s in self.data) + b"\n")
url = sock.recv(1024).decode("utf-8") url = sock.recv(1024).decode("utf-8")
except socket.error: except socket.error:
self.target_logger("".join(self.data), display=False) self.target_logger(self.data, display=False)
else: else:
self.cli.msg(botconfig.DEV_CHANNEL, " ".join((msg, url))) self.cli.msg(botconfig.DEV_CHANNEL, " ".join((msg, url)))
del self.data[:] self.data = None
if var.PHASE in ("join", "day", "night"): if var.PHASE in ("join", "day", "night"):
from src.decorators import COMMANDS from src.decorators import COMMANDS
for cmd in COMMANDS["fstop"]: for cmd in COMMANDS["fstop"]:

View File

@ -42,7 +42,7 @@ def on_privmsg(cli, rawnick, chan, msg, notice = False):
if botconfig.DEBUG_MODE: if botconfig.DEBUG_MODE:
raise raise
else: else:
traceback.print_exc() sys.stderr.write(traceback.format_exc())
for x in decorators.COMMANDS: for x in decorators.COMMANDS:
@ -62,7 +62,7 @@ def on_privmsg(cli, rawnick, chan, msg, notice = False):
if botconfig.DEBUG_MODE: if botconfig.DEBUG_MODE:
raise raise
else: else:
traceback.print_exc() sys.stderr.write(traceback.format_exc())
def unhandled(cli, prefix, cmd, *args): def unhandled(cli, prefix, cmd, *args):
@ -77,7 +77,7 @@ def unhandled(cli, prefix, cmd, *args):
if botconfig.DEBUG_MODE: if botconfig.DEBUG_MODE:
raise raise
else: else:
traceback.print_exc() sys.stderr.write(traceback.format_exc())
def connect_callback(cli): def connect_callback(cli):
@hook("endofmotd", hookid=294) @hook("endofmotd", hookid=294)