84
79
just used to catch exceptions.
85
80
Takes both an IVLE request and an Apache req.
87
# Hack? Try and get the user login early just in case we throw an error
88
# (most likely 404) to stop us seeing not logged in even when we are.
89
if not req.publicmode:
90
req.user = login.get_user_details(req)
92
82
# Check req.app to see if it is valid. 404 if not.
93
83
if req.app is not None and req.app not in conf.apps.app_url:
94
84
# Maybe it is a special app!
98
88
req.throw_error(Request.HTTP_NOT_FOUND,
99
89
"There is no application called %s." % repr(req.app))
101
# Special handling for public mode - only allow the public app, call it
91
# Special handling for public mode - just call public app and get out
103
92
# NOTE: This will not behave correctly if the public app uses
104
93
# write_html_head_foot, but "serve" does not.
105
94
if req.publicmode:
106
if req.app != conf.apps.public_app:
107
req.throw_error(Request.HTTP_FORBIDDEN,
108
"This application is not available on the public site.")
109
95
app = conf.apps.app_url[conf.apps.public_app]
110
96
apps.call_app(app.dir, req)
159
145
# When done, write out the HTML footer if the app has requested it
160
146
if req.write_html_head_foot:
161
# Show the console if required
162
if logged_in and app.useconsole:
163
plugins.console.present(req, windowpane=True)
164
147
html.write_html_foot(req)
166
149
# Note: Apache will not write custom HTML error messages here.
188
171
the IVLE request is created.
190
173
req.content_type = "text/html"
191
logfile = os.path.join(conf.conf.log_path, 'ivle_error.log')
193
174
# For some reason, some versions of mod_python have "_server" instead of
194
175
# "main_server". So we check for both.
205
186
except AttributeError:
207
188
req.status = apache.HTTP_INTERNAL_SERVER_ERROR
209
login = req.user.login
210
except AttributeError:
215
logging.basicConfig(level=logging.INFO,
216
format='%(asctime)s %(levelname)s: ' +
217
'(HTTP: ' + str(req.status) +
218
', Ref: ' + str(login) + '@' +
219
str(socket.gethostname()) + str(req.uri) +
225
logging.debug('Logging Unhandled Exception')
227
189
# We handle 3 types of error.
228
190
# IVLEErrors with 4xx response codes (client error).
229
191
# IVLEErrors with 5xx response codes (handled server error).
237
199
# IVLEErrors with 4xx response codes are client errors.
238
200
# Therefore, these have a "nice" response (we even coat it in the IVLE
239
201
# HTML wrappers).
241
202
req.write_html_head_foot = True
242
203
req.write('<div id="ivle_padding">\n')
256
217
req.write("<p>%s</p>\n" % cgi.escape(msg))
258
219
req.write("<p>An unknown error occured.</p>\n")
261
logging.info(str(msg))
263
220
req.write("<p>(HTTP error code %d)</p>\n" % httpcode)
265
req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
266
%cgi.escape(logfile))
267
221
req.write('</div>\n')
269
223
# A "bad" error message. We shouldn't get here unless IVLE
273
227
# If this is a non-4xx IVLEError, get the message and httpcode and
274
228
# make the error message a bit nicer (but still include the
276
# We also need to special-case IVLEJailError, as we can get another
277
# almost-exception out of it.
279
codename, msg = None, None
281
if exc_type is util.IVLEJailError:
282
msg = exc_value.type_str + ": " + exc_value.message
283
tb = 'Exception information extracted from IVLEJailError:\n'
284
tb += urllib.unquote(exc_value.info)
287
codename, msg = req.get_http_codename(httpcode)
288
except AttributeError:
290
# Override the default message with the supplied one,
292
if hasattr(exc_value, 'message') and exc_value.message is not None:
293
msg = exc_value.message
294
# Prepend the exception type
295
if exc_type != util.IVLEError:
296
msg = exc_type.__name__ + ": " + msg
298
tb = ''.join(traceback.format_exception(exc_type, exc_value,
302
logging.error('%s\n%s'%(str(msg), tb))
231
codename, msg = req.get_http_codename(httpcode)
232
except AttributeError:
233
codename, msg = None, None
234
# Override the default message with the supplied one,
236
if hasattr(exc_value, 'message') and exc_value.message is not None:
237
msg = exc_value.message
238
# Prepend the exception type
239
if exc_type != util.IVLEError:
240
msg = exc_type.__name__ + ": " + msg
304
242
req.write("""<html>
305
243
<head><title>IVLE Internal Server Error</title></head>
321
259
administrator). Include the following information:</p>
322
260
""" % (cgi.escape(admin_email), cgi.escape(admin_email)))
324
req.write("<pre>\n%s\n</pre>\n"%cgi.escape(tb))
326
req.write("<p>Warning: Could not open Error Log: '%s'</p>\n"
327
%cgi.escape(logfile))
262
tb_print = cStringIO.StringIO()
263
traceback.print_exception(exc_type, exc_value, exc_traceback,
266
req.write(cgi.escape(tb_print.getvalue()))
267
req.write("</pre>\n</body>\n")