52
49
# Make the request object into an IVLE request which can be passed to apps
55
req = Request(req, html.write_html_head)
57
# Pass the apachereq to error reporter, since ivle req isn't created
59
handle_unknown_exception(apachereq, *sys.exc_info())
60
# Tell Apache not to generate its own errors as well
63
# Run the main handler, and catch all exceptions
65
return handler_(req, apachereq)
66
except mod_python.apache.SERVER_RETURN:
67
# An apache error. We discourage these, but they might still happen.
71
handle_unknown_exception(req, *sys.exc_info())
72
# Tell Apache not to generate its own errors as well
75
def handler_(req, apachereq):
77
Nested handler function. May raise exceptions. The top-level handler is
78
just used to catch exceptions.
79
Takes both an IVLE request and an Apache req.
51
req = Request(req, html.write_html_head)
81
53
# Check req.app to see if it is valid. 404 if not.
82
54
if req.app is not None and req.app not in conf.apps.app_url:
83
55
# Maybe it is a special app!
84
56
if req.app == 'logout':
87
req.throw_error(Request.HTTP_NOT_FOUND,
88
"There is no application called %s." % repr(req.app))
59
# TODO: Nicer 404 message?
60
req.throw_error(Request.HTTP_NOT_FOUND)
90
62
# Special handling for public mode - just call public app and get out
91
63
# NOTE: This will not behave correctly if the public app uses
102
74
app = conf.apps.app_url[req.app]
104
76
# Check if app requires auth. If so, perform authentication and login.
105
# This will either return a User object, None, or perform a redirect
106
# which we will not catch here.
107
77
if app.requireauth:
108
req.user = login.login(req)
109
logged_in = req.user is not None
78
req.username = login.login(req)
79
logged_in = req.username is not None
111
req.user = login.get_user_details(req)
81
req.username = login.get_username(req)
155
124
session = req.get_session()
156
125
session.invalidate()
158
req.add_cookie(forumutil.invalidated_forum_cookie())
159
127
req.throw_redirect(util.make_path(''))
161
def handle_unknown_exception(req, exc_type, exc_value, exc_traceback):
163
Given an exception that has just been thrown from IVLE, print its details
165
This is a full handler. It assumes nothing has been written, and writes a
167
req: May be EITHER an IVLE req or an Apache req.
168
IVLE reqs may have the HTML head/foot written (on a 400 error), but
169
the handler code may pass an apache req if an exception occurs before
170
the IVLE request is created.
172
req.content_type = "text/html"
173
admin_email = apache._server.server_admin
175
httpcode = exc_value.httpcode
176
req.status = httpcode
177
except AttributeError:
179
req.status = apache.HTTP_INTERNAL_SERVER_ERROR
180
# We handle 3 types of error.
181
# IVLEErrors with 4xx response codes (client error).
182
# IVLEErrors with 5xx response codes (handled server error).
183
# Other exceptions (unhandled server error).
184
# IVLEErrors should not have other response codes than 4xx or 5xx
185
# (eg. throw_redirect should have been used for 3xx codes).
186
# Therefore, that is treated as an unhandled error.
188
if (exc_type == util.IVLEError and httpcode >= 400
189
and httpcode <= 499):
190
# IVLEErrors with 4xx response codes are client errors.
191
# Therefore, these have a "nice" response (we even coat it in the IVLE
193
req.write_html_head_foot = True
194
req.write('<div id="ivle_padding">\n')
196
codename, msg = req.get_http_codename(httpcode)
197
except AttributeError:
198
codename, msg = None, None
199
# Override the default message with the supplied one,
201
if exc_value.message is not None:
202
msg = exc_value.message
203
if codename is not None:
204
req.write("<h1>Error: %s</h1>\n" % codename)
206
req.write("<h1>Error</h1>\n")
208
req.write("<p>%s</p>\n" % msg)
210
req.write("<p>An unknown error occured.</p>\n")
211
req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
212
req.write('</div>\n')
214
# A "bad" error message. We shouldn't get here unless IVLE
215
# misbehaves (which is currently very easy, if things aren't set up
217
# Write the traceback.
218
# If this is a non-4xx IVLEError, get the message and httpcode and
219
# make the error message a bit nicer (but still include the
222
codename, msg = req.get_http_codename(httpcode)
223
except AttributeError:
224
codename, msg = None, None
225
# Override the default message with the supplied one,
227
if hasattr(exc_value, 'message') and exc_value.message is not None:
228
msg = exc_value.message
231
<head><title>IVLE Internal Server Error</title></head>
233
<h1>IVLE Internal Server Error""")
234
if (codename is not None
235
and httpcode != apache.HTTP_INTERNAL_SERVER_ERROR):
236
req.write(": %s" % codename)
238
<p>An error has occured which is the fault of the IVLE developers or
242
req.write("<p>%s</p>\n" % msg)
243
if httpcode is not None:
244
req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
246
<p>Please report this to <a href="mailto:%s">%s</a> (the system
247
administrator). Include the following information:</p>
248
""" % (admin_email, admin_email))
250
tb_print = cStringIO.StringIO()
251
traceback.print_exception(exc_type, exc_value, exc_traceback,
254
req.write(tb_print.getvalue())
255
req.write("</pre>\n</body>\n")