mirror of
https://github.com/frappe/gunicorn.git
synced 2026-01-14 11:09:11 +08:00
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:
parent
a1ced17d8b
commit
885b530e1a
@ -178,19 +178,18 @@ def writelines(sock, lines, chunked=False):
|
|||||||
for line in list(lines):
|
for line in list(lines):
|
||||||
write(sock, line, chunked)
|
write(sock, line, chunked)
|
||||||
|
|
||||||
def write_error(sock, msg, status_int=500,
|
def write_error(sock, status_int, reason, mesg):
|
||||||
reason="Internal Server Error"):
|
|
||||||
html = textwrap.dedent("""\
|
html = textwrap.dedent("""\
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>%(reason)s</title>
|
<title>%(reason)s</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>%(reason)s</h1>
|
<h1>%(reason)s</h1>
|
||||||
%(msg)s
|
%(mesg)s
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
""") % {"reason": reason, "msg": msg}
|
""") % {"reason": reason, "mesg": mesg}
|
||||||
|
|
||||||
http = textwrap.dedent("""\
|
http = textwrap.dedent("""\
|
||||||
HTTP/1.1 %s %s\r
|
HTTP/1.1 %s %s\r
|
||||||
|
|||||||
@ -116,9 +116,18 @@ class Worker(object):
|
|||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
def handle_error(self, client, exc):
|
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,
|
if isinstance(exc, (InvalidRequestLine, InvalidRequestMethod,
|
||||||
InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,)):
|
InvalidHTTPVersion, InvalidHeader, InvalidHeaderName,)):
|
||||||
|
|
||||||
|
status_int = 400
|
||||||
|
reason = "Bad Request"
|
||||||
|
|
||||||
if isinstance(exc, InvalidRequestLine):
|
if isinstance(exc, InvalidRequestLine):
|
||||||
mesg = "<p>Invalid Request Line '%s'</p>" % str(exc)
|
mesg = "<p>Invalid Request Line '%s'</p>" % str(exc)
|
||||||
elif isinstance(exc, InvalidRequestMethod):
|
elif isinstance(exc, InvalidRequestMethod):
|
||||||
@ -127,22 +136,15 @@ class Worker(object):
|
|||||||
mesg = "<p>Invalid HTTP Version '%s'</p>" % str(exc)
|
mesg = "<p>Invalid HTTP Version '%s'</p>" % str(exc)
|
||||||
elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
|
elif isinstance(exc, (InvalidHeaderName, InvalidHeader,)):
|
||||||
mesg = "<p>Invalid Header'%s'</p>" % str(exc)
|
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:
|
if self.debug:
|
||||||
tb = traceback.format_exc()
|
tb = traceback.format_exc()
|
||||||
mesg += "<h2>Traceback:</23><pre>%s</pre>" % tb
|
mesg += "<h2>Traceback:</h2>\n<pre>%s</pre>" % tb
|
||||||
|
|
||||||
try:
|
try:
|
||||||
util.write_error(client, mesg, status_int=status_int,
|
util.write_error(client, status_int, reason, mesg)
|
||||||
reason=reason)
|
|
||||||
except:
|
except:
|
||||||
if self.debug:
|
self.log.exception("Failed to send error message.")
|
||||||
self.log.warning("Unexpected error %s" % traceback.format_exc())
|
|
||||||
|
|
||||||
def handle_winch(self, sig, fname):
|
def handle_winch(self, sig, fname):
|
||||||
# Ignore SIGWINCH in worker. Fixes a crash on OpenBSD.
|
# Ignore SIGWINCH in worker. Fixes a crash on OpenBSD.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user