~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/dispatch/__init__.py

  • Committer: dcoles
  • Date: 2008-07-16 06:01:58 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:893
Dispatch: Now attempts to log unhandled exceptions to a log directory specified 
in the config.py. Provides HTTP error code, logged in user, server and URI path 
of the exception. Will also provide a traceback similar to what the user will 
see on 'Non IVLE' errors.

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 
37
37
import conf
38
38
import conf.apps
 
39
import conf.conf
39
40
import apps
40
41
 
41
42
from request import Request
45
46
from common import (util, forumutil)
46
47
import traceback
47
48
import plugins.console
 
49
import logging
 
50
import socket
48
51
 
49
52
def handler(req):
50
53
    """Handles a request which may be to anywhere in the site except media.
177
180
    the IVLE request is created.
178
181
    """
179
182
    req.content_type = "text/html"
 
183
    logfile = os.path.join(conf.conf.log_path, 'ivle_error.log')
 
184
    logfail = False
180
185
    # For some reason, some versions of mod_python have "_server" instead of
181
186
    # "main_server". So we check for both.
182
187
    try:
192
197
    except AttributeError:
193
198
        httpcode = None
194
199
        req.status = apache.HTTP_INTERNAL_SERVER_ERROR
 
200
    try:
 
201
        login = req.user.login
 
202
    except AttributeError:
 
203
        login = None
 
204
 
 
205
    # Log File
 
206
    try:
 
207
        logging.basicConfig(level=logging.INFO,
 
208
            format='%(asctime)s %(levelname)s: ' +
 
209
                '(HTTP: ' + str(req.status) +
 
210
                ', Ref: ' + str(login) + '@' +
 
211
                str(socket.gethostname()) + str(req.uri) +
 
212
                ') %(message)s',
 
213
            filename=logfile,
 
214
            filemode='a')
 
215
    except IOError:
 
216
        logfail = True
 
217
    logging.debug('Logging Unhandled Exception')
 
218
 
195
219
    # We handle 3 types of error.
196
220
    # IVLEErrors with 4xx response codes (client error).
197
221
    # IVLEErrors with 5xx response codes (handled server error).
205
229
        # IVLEErrors with 4xx response codes are client errors.
206
230
        # Therefore, these have a "nice" response (we even coat it in the IVLE
207
231
        # HTML wrappers).
 
232
        
208
233
        req.write_html_head_foot = True
209
234
        req.write('<div id="ivle_padding">\n')
210
235
        try:
223
248
            req.write("<p>%s</p>\n" % cgi.escape(msg))
224
249
        else:
225
250
            req.write("<p>An unknown error occured.</p>\n")
 
251
        
 
252
        # Logging
 
253
        logging.info(str(msg))
 
254
        
226
255
        req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
 
256
        if logfail:
 
257
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
 
258
                %cgi.escape(logfile))
227
259
        req.write('</div>\n')
228
260
    else:
229
261
        # A "bad" error message. We shouldn't get here unless IVLE
258
290
            tb = ''.join(traceback.format_exception(exc_type, exc_value,
259
291
                                                    exc_traceback))
260
292
 
 
293
        # Logging
 
294
        logging.error('%s\n%s'%(str(msg), tb))
 
295
 
261
296
        req.write("""<html>
262
297
<head><title>IVLE Internal Server Error</title></head>
263
298
<body>
278
313
administrator). Include the following information:</p>
279
314
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
280
315
 
281
 
        req.write("<pre>\n")
282
 
        req.write(cgi.escape(tb))
283
 
        req.write("</pre>\n</body>\n")
 
316
        req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
 
317
        if logfail:
 
318
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
 
319
                %cgi.escape(logfile))
 
320
        req.write("</body>")