From: Quentin Smith STDERR: Uh-oh! We experienced an error. Sorry about that. We've gotten
mail about it.' + str(addition) + '
'
-
-Template.database = database
-Template.config = config
-Template.err = None
-
-class JsonDict:
- """Class to store a dictionary that will be converted to JSON"""
- def __init__(self, **kws):
- self.data = kws
- if 'err' in kws:
- err = kws['err']
- del kws['err']
- self.addError(err)
-
- def __str__(self):
- return simplejson.dumps(self.data)
-
- def addError(self, text):
- """Add stderr text to be displayed on the website."""
- self.data['err'] = \
- makeErrorPre(self.data.get('err'), text)
-
class Defaults:
"""Class to store default values for fields."""
memory = 256
@@ -388,17 +392,6 @@ class Defaults:
for key in kws:
setattr(self, key, kws[key])
-
-
-DEFAULT_HEADERS = {'Content-Type': 'text/html'}
-
-def invalidInput(op, username, fields, err, emsg):
- """Print an error page when an InvalidInput exception occurs"""
- d = dict(op=op, user=username, err_field=err.err_field,
- err_value=str(err.err_value), stderr=emsg,
- errorMessage=str(err))
- return templates.invalid(searchList=[d])
-
def hasVnc(status):
"""Does the machine with a given status list support VNC?"""
if status is None:
@@ -567,11 +560,6 @@ def modifyDict(username, state, machine_id, fields):
controls.renameMachine(machine, oldname, validate.name)
return dict(machine=machine)
-
-def badOperation(u, s, p, e):
- """Function called when accessing an unknown URI."""
- return ({'Status': '404 Not Found'}, 'Invalid operation.')
-
def infoDict(username, state, machine):
"""Get the variables used by info.tmpl."""
status = controls.statusInfo(machine)
@@ -660,14 +648,6 @@ def infoDict(username, state, machine):
fields = fields)
return d
-mapping = dict()
-
-def printHeaders(headers):
- """Print a dictionary as HTTP headers."""
- for key, value in headers.iteritems():
- print '%s: %s' % (key, value)
- print
-
def send_error_mail(subject, body):
import subprocess
@@ -684,98 +664,4 @@ Subject: %s
p.stdin.close()
p.wait()
-def show_error(op, username, fields, err, emsg, traceback):
- """Print an error page when an exception occurs"""
- d = dict(op=op, user=username, fields=fields,
- errorMessage=str(err), stderr=emsg, traceback=traceback)
- details = templates.error_raw(searchList=[d])
- exclude = config.web.errormail_exclude
- if username not in exclude and '*' not in exclude:
- send_error_mail('xvm error on %s for %s: %s' % (op, username, err),
- details)
- d['details'] = details
- return templates.error(searchList=[d])
-
-def handler(username, state, path, fields):
- operation, path = pathSplit(path)
- if not operation:
- operation = 'list'
- print 'Starting', operation
- fun = mapping.get(operation, badOperation)
- return fun(username, state, path, fields)
-
-class App:
- def __init__(self, environ, start_response):
- self.environ = environ
- self.start = start_response
-
- self.username = getUser(environ)
- self.state = State(self.username)
- self.state.environ = environ
-
- random.seed() #sigh
-
- def __iter__(self):
- start_time = time.time()
- database.clear_cache()
- sys.stderr = StringIO()
- fields = cgi.FieldStorage(fp=self.environ['wsgi.input'], environ=self.environ)
- operation = self.environ.get('PATH_INFO', '')
- if not operation:
- self.start("301 Moved Permanently", [('Location', './')])
- return
- if self.username is None:
- operation = 'unauth'
-
- try:
- checkpoint.checkpoint('Before')
- output = handler(self.username, self.state, operation, fields)
- checkpoint.checkpoint('After')
-
- headers = dict(DEFAULT_HEADERS)
- if isinstance(output, tuple):
- new_headers, output = output
- headers.update(new_headers)
- e = revertStandardError()
- if e:
- if hasattr(output, 'addError'):
- output.addError(e)
- else:
- # This only happens on redirects, so it'd be a pain to get
- # the message to the user. Maybe in the response is useful.
- output = output + '\n\nstderr:\n' + e
- output_string = str(output)
- checkpoint.checkpoint('output as a string')
- except Exception, err:
- if not fields.has_key('js'):
- if isinstance(err, InvalidInput):
- self.start('200 OK', [('Content-Type', 'text/html')])
- e = revertStandardError()
- yield str(invalidInput(operation, self.username, fields,
- err, e))
- return
- import traceback
- self.start('500 Internal Server Error',
- [('Content-Type', 'text/html')])
- e = revertStandardError()
- s = show_error(operation, self.username, fields,
- err, e, traceback.format_exc())
- yield str(s)
- return
- status = headers.setdefault('Status', '200 OK')
- del headers['Status']
- self.start(status, headers.items())
- yield output_string
- if fields.has_key('timedebug'):
- yield '%s
' % cgi.escape(str(checkpoint))
-
-def constructor():
- connect()
- return App
-
-def main():
- from flup.server.fcgi_fork import WSGIServer
- WSGIServer(constructor()).run()
-
-if __name__ == '__main__':
- main()
+random.seed()
diff --git a/code/templates/error.tmpl b/code/templates/error.mako
similarity index 73%
rename from code/templates/error.tmpl
rename to code/templates/error.mako
index ff9b4e0..51c6a65 100644
--- a/code/templates/error.tmpl
+++ b/code/templates/error.mako
@@ -1,11 +1,10 @@
-#from skeleton import skeleton
-#extends skeleton
+<%page expression_filter="h"/>
+<%inherit file="skeleton.mako" />
-#def title
+<%def name="title()">
ERROR!
-#end def
+%def>
-#def body
In case you're curious, the gory details are below.
-$details +${details}-#end def diff --git a/code/templates/error_raw.mako b/code/templates/error_raw.mako new file mode 100644 index 0000000..a8cf902 --- /dev/null +++ b/code/templates/error_raw.mako @@ -0,0 +1,12 @@ +Error on operation ${op} for user ${user}: ${errorMessage} + +Fields: +%for f in fields: +${f}=${fields[f]} +%endfor + +Error output: +${stderr}\ +---- end error output + +${traceback} diff --git a/code/templates/error_raw.tmpl b/code/templates/error_raw.tmpl deleted file mode 100644 index 5ae1490..0000000 --- a/code/templates/error_raw.tmpl +++ /dev/null @@ -1,12 +0,0 @@ -Error on operation $op for user $user: $errorMessage - -Fields: -#for $f in $fields: -$f=$fields[$f].value -#end for - -Error output: -$stderr#slurp ----- end error output - -$traceback diff --git a/code/templates/invalid.mako b/code/templates/invalid.mako new file mode 100644 index 0000000..c597d18 --- /dev/null +++ b/code/templates/invalid.mako @@ -0,0 +1,16 @@ +<%page expression_filter="h"/> +<%inherit file="skeleton.mako" /> + +<%def name="title()"> +Invalid Input +%def> + +
Your input was bad:
+Field | value | reason |
${err_field} | ${err_value} | ${errorMessage} |
Your input was bad:
-operation | Field | value | reason |
$op | $err_field | $err_value | $errorMessage |