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

« back to all changes in this revision

Viewing changes to ivle/dispatch/__init__.py

Drop the 4xx-series exception handling ability from
ivle.dispatch.handle_unknown_exception(). Nothing raises anything but a 5xx
IVLEError now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
170
170
    This is a full handler. It assumes nothing has been written, and writes a
171
171
    complete HTML page.
172
172
    req: May be EITHER an IVLE req or an Apache req.
173
 
    IVLE reqs may have the HTML head/foot written (on a 400 error), but
174
 
    the handler code may pass an apache req if an exception occurs before
 
173
    The handler code may pass an apache req if an exception occurs before
175
174
    the IVLE request is created.
176
175
    """
177
176
    req.content_type = "text/html"
221
220
        logfail = True
222
221
    logging.debug('Logging Unhandled Exception')
223
222
 
224
 
    # We handle 3 types of error.
225
 
    # IVLEErrors with 4xx response codes (client error).
226
 
    # IVLEErrors with 5xx response codes (handled server error).
227
 
    # Other exceptions (unhandled server error).
228
 
    # IVLEErrors should not have other response codes than 4xx or 5xx
229
 
    # (eg. throw_redirect should have been used for 3xx codes).
230
 
    # Therefore, that is treated as an unhandled error.
231
 
 
232
 
    if (exc_type == util.IVLEError and httpcode >= 400
233
 
        and httpcode <= 499):
234
 
        # IVLEErrors with 4xx response codes are client errors.
235
 
        # Therefore, these have a "nice" response (we even coat it in the IVLE
236
 
        # HTML wrappers).
237
 
        
238
 
        req.write_html_head_foot = True
239
 
        req.write_javascript_settings = False
240
 
        req.write('<div id="ivle_padding">\n')
 
223
    # A "bad" error message. We shouldn't get here unless IVLE
 
224
    # misbehaves (which is currently very easy, if things aren't set up
 
225
    # correctly).
 
226
    # Write the traceback.
 
227
    # If this is a non-4xx IVLEError, get the message and httpcode and
 
228
    # make the error message a bit nicer (but still include the
 
229
    # traceback).
 
230
    # We also need to special-case IVLEJailError, as we can get another
 
231
    # almost-exception out of it.
 
232
 
 
233
    codename, msg = None, None
 
234
 
 
235
    if exc_type is util.IVLEJailError:
 
236
        msg = exc_value.type_str + ": " + exc_value.message
 
237
        tb = 'Exception information extracted from IVLEJailError:\n'
 
238
        tb += urllib.unquote(exc_value.info)
 
239
    else:
241
240
        try:
242
241
            codename, msg = req.get_http_codename(httpcode)
243
242
        except AttributeError:
244
 
            codename, msg = None, None
 
243
            pass
245
244
        # Override the default message with the supplied one,
246
245
        # if available.
247
 
        if exc_value.message is not None:
 
246
        if hasattr(exc_value, 'message') and exc_value.message is not None:
248
247
            msg = exc_value.message
249
 
        if codename is not None:
250
 
            req.write("<h1>Error: %s</h1>\n" % cgi.escape(codename))
251
 
        else:
252
 
            req.write("<h1>Error</h1>\n")
253
 
        if msg is not None:
254
 
            req.write("<p>%s</p>\n" % cgi.escape(msg))
255
 
        else:
256
 
            req.write("<p>An unknown error occured.</p>\n")
257
 
        
258
 
        # Logging
259
 
        logging.info(str(msg))
260
 
        
261
 
        req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
262
 
        if logfail:
263
 
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
264
 
                %cgi.escape(logfile))
265
 
        req.write('</div>\n')
266
 
        html.write_html_foot(req)
267
 
    else:
268
 
        # A "bad" error message. We shouldn't get here unless IVLE
269
 
        # misbehaves (which is currently very easy, if things aren't set up
270
 
        # correctly).
271
 
        # Write the traceback.
272
 
        # If this is a non-4xx IVLEError, get the message and httpcode and
273
 
        # make the error message a bit nicer (but still include the
274
 
        # traceback).
275
 
        # We also need to special-case IVLEJailError, as we can get another
276
 
        # almost-exception out of it.
277
 
 
278
 
        codename, msg = None, None
279
 
 
280
 
        if exc_type is util.IVLEJailError:
281
 
            msg = exc_value.type_str + ": " + exc_value.message
282
 
            tb = 'Exception information extracted from IVLEJailError:\n'
283
 
            tb += urllib.unquote(exc_value.info)
284
 
        else:
285
 
            try:
286
 
                codename, msg = req.get_http_codename(httpcode)
287
 
            except AttributeError:
288
 
                pass
289
 
            # Override the default message with the supplied one,
290
 
            # if available.
291
 
            if hasattr(exc_value, 'message') and exc_value.message is not None:
292
 
                msg = exc_value.message
293
 
                # Prepend the exception type
294
 
                if exc_type != util.IVLEError:
295
 
                    msg = exc_type.__name__ + ": " + repr(msg)
296
 
 
297
 
            tb = ''.join(traceback.format_exception(exc_type, exc_value,
298
 
                                                    exc_traceback))
299
 
 
300
 
        # Logging
301
 
        logging.error('%s\n%s'%(str(msg), tb))
302
 
        # Error messages are only displayed is the user is NOT a student,
303
 
        # or if there has been a problem logging the error message
304
 
        show_errors = (not publicmode) and ((login and \
305
 
                            str(role) != "student") or logfail)
306
 
        req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"                 
 
248
            # Prepend the exception type
 
249
            if exc_type != util.IVLEError:
 
250
                msg = exc_type.__name__ + ": " + repr(msg)
 
251
 
 
252
        tb = ''.join(traceback.format_exception(exc_type, exc_value,
 
253
                                                exc_traceback))
 
254
 
 
255
    # Logging
 
256
    logging.error('%s\n%s'%(str(msg), tb))
 
257
    # Error messages are only displayed is the user is NOT a student,
 
258
    # or if there has been a problem logging the error message
 
259
    show_errors = (not publicmode) and ((login and \
 
260
                        str(role) != "student") or logfail)
 
261
    req.write("""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"                 
307
262
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">                                      
308
263
<html xmlns="http://www.w3.org/1999/xhtml">
309
264
<head><title>IVLE Internal Server Error</title></head>
310
265
<body>
311
266
<h1>IVLE Internal Server Error""")
312
 
        if (show_errors):
313
 
            if (codename is not None
314
 
                        and httpcode != mod_python.apache.HTTP_INTERNAL_SERVER_ERROR):
315
 
                req.write(": %s" % cgi.escape(codename))
316
 
        
317
 
        req.write("""</h1>
 
267
    if (show_errors):
 
268
        if (codename is not None
 
269
                    and httpcode != mod_python.apache.HTTP_INTERNAL_SERVER_ERROR):
 
270
            req.write(": %s" % cgi.escape(codename))
 
271
 
 
272
    req.write("""</h1>
318
273
<p>An error has occured which is the fault of the IVLE developers or
319
274
administration. The developers have been notified.</p>
320
275
""")
321
 
        if (show_errors):
322
 
            if msg is not None:
323
 
                req.write("<p>%s</p>\n" % cgi.escape(msg))
324
 
            if httpcode is not None:
325
 
                req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
326
 
            req.write("""
327
 
    <p>Please report this to <a href="mailto:%s">%s</a> (the system
328
 
    administrator). Include the following information:</p>
329
 
    """ % (cgi.escape(admin_email), cgi.escape(admin_email)))
 
276
    if (show_errors):
 
277
        if msg is not None:
 
278
            req.write("<p>%s</p>\n" % cgi.escape(msg))
 
279
        if httpcode is not None:
 
280
            req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
 
281
        req.write("""
 
282
<p>Please report this to <a href="mailto:%s">%s</a> (the system
 
283
administrator). Include the following information:</p>
 
284
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
330
285
 
331
 
            req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
332
 
            if logfail:
333
 
                req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
334
 
                    %cgi.escape(logfile))
335
 
        req.write("</body></html>")
 
286
        req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
 
287
        if logfail:
 
288
            req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
 
289
                %cgi.escape(logfile))
 
290
    req.write("</body></html>")