Always log exceptions during request handling.

* Always log the exception locally
  * Still only pass the exception in the HTTP response if
    debug is turned on.
  * Slight cosmetic changes to the actual HTML of the error
    response.
This commit is contained in:
Paul J. Davis 2011-01-29 14:42:32 -05:00
parent a1ced17d8b
commit 885b530e1a
2 changed files with 21 additions and 20 deletions

View File

@ -178,19 +178,18 @@ def writelines(sock, lines, chunked=False):
for line in list(lines):
write(sock, line, chunked)
def write_error(sock, msg, status_int=500,
reason="Internal Server Error"):
def write_error(sock, status_int, reason, mesg):
html = textwrap.dedent("""\
<html>
<head>
<title>%(reason)s</title>
</head>
<body>
<h1>%(reason)s</h1>
%(msg)s
</body>
<head>
<title>%(reason)s</title>
</head>
<body>
<h1>%(reason)s</h1>
%(mesg)s
</body>
</html>
""") % {"reason": reason, "msg": msg}
""") % {"reason": reason, "mesg": mesg}
http = textwrap.dedent("""\
HTTP/1.1 %s %s\r

View File

@ -116,9 +116,18 @@ class Worker(object):
sys.exit(0)
def handle_error(self, client, exc):
self.log.exception("Error hanlding request")
status_int = 500
reason = "Internal Server Error"
mesg = ""
if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,)):
status_int = 400
reason = "Bad Request"
if isinstance(exc, InvalidRequestLine):
mesg = "<p>Invalid Request Line '%s'</p>" % str(exc)
elif isinstance(exc, InvalidRequestMethod):
@ -127,22 +136,15 @@ class Worker(object):
mesg = "<p>Invalid HTTP Version '%s'</p>" % str(exc)
elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
mesg = "<p>Invalid Header'%s'</p>" % str(exc)
reason = "Bad Request"
status_int = 400
else:
mesg = reason = "Internal Server reason"
status_int = 500
if self.debug:
tb = traceback.format_exc()
mesg += "<h2>Traceback:</23><pre>%s</pre>" % tb
mesg += "<h2>Traceback:</h2>\n<pre>%s</pre>" % tb
try:
util.write_error(client, mesg, status_int=status_int,
reason=reason)
util.write_error(client, status_int, reason, mesg)
except:
if self.debug:
self.log.warning("Unexpected error %s" % traceback.format_exc())
self.log.exception("Failed to send error message.")
def handle_winch(self, sig, fname):
# Ignore SIGWINCH in worker. Fixes a crash on OpenBSD.