handle application errors

This commit is contained in:
Benoit Chesneau 2010-01-23 15:04:52 +01:00
parent 04bb2c3a51
commit e699a50952
4 changed files with 20 additions and 4 deletions

View File

@ -4,7 +4,7 @@
# The %(here)s variable will be replaced with the parent directory of this file # The %(here)s variable will be replaced with the parent directory of this file
# #
[DEFAULT] [DEFAULT]
debug = false debug = true
# Uncomment and replace with the address which should receive any error reports # Uncomment and replace with the address which should receive any error reports
#email_to = you@yourdomain.com #email_to = you@yourdomain.com
smtp_server = localhost smtp_server = localhost

View File

@ -18,7 +18,7 @@ class HttpResponse(object):
def send(self): def send(self):
# send headers # send headers
resp_head = [] resp_head = []
resp_head.append("HTTP/1.1 %s\r\n" % (self.status)) resp_head.append("HTTP/1.0 %s\r\n" % (self.status))
resp_head.append("Server: %s\r\n" % self.SERVER_VERSION) resp_head.append("Server: %s\r\n" % self.SERVER_VERSION)
resp_head.append("Date: %s\r\n" % http_date()) resp_head.append("Date: %s\r\n" % http_date())

View File

@ -61,6 +61,9 @@ def write(sock, data):
raise raise
i += 1 i += 1
def writelines(sock, lines):
for line in list(lines):
write(sock, line)
def normalize_name(name): def normalize_name(name):
return "-".join([w.lower().capitalize() for w in name.split("-")]) return "-".join([w.lower().capitalize() for w in name.split("-")])

View File

@ -12,6 +12,7 @@ import signal
import socket import socket
import sys import sys
import tempfile import tempfile
import traceback
from gunicorn import http from gunicorn import http
from gunicorn import util from gunicorn import util
@ -115,7 +116,19 @@ class Worker(object):
util.close_on_exec(client) util.close_on_exec(client)
try: try:
req = http.HttpRequest(client, addr, self.address) req = http.HttpRequest(client, addr, self.address)
try:
response = self.app(req.read(), req.start_response) response = self.app(req.read(), req.start_response)
except Exception, e:
exc = ''.join(traceback.format_exception(*sys.exc_info()))
msg = "<h1>Internal Server Error</h1><h2>wsgi error:</h2><pre>%s</pre>" % exc
util.writelines(client,
["HTTP/1.0 500 Internal Server Error\r\n",
"Connection: close\r\n",
"Content-type: text/html\r\n",
"Content-length: %s\r\n" % str(len(msg)),
"\r\n",
msg])
return
http.HttpResponse(client, response, req).send() http.HttpResponse(client, response, req).send()
except Exception, e: except Exception, e:
self.log.exception("Error processing request. [%s]" % str(e)) self.log.exception("Error processing request. [%s]" % str(e))