def log_error(cursor, e, context=5, function_call=""): error_output = html_render(sys.exc_info(), context=5, headers=False) # It might be they're not logged in if "user" in common_f.cache and common_f.cache['user'] != None: user_id = common_f.cache['user'].id else: user_id = -1 exception_type = str(sys.exc_info()[0]).replace("<class '", "").replace("'>", "") # We log the error here the_error = Error( args = common_f.print_post_data(joiner="\n\n"), user_id = user_id, mode = common_f.get_val("mode", pages.default_page), function_call = function_call, exception_type = exception_type, traceback = error_output, ) error_logged = True try: the_error.insert(cursor) except Exception as e: if context != 0: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), the_error.insert(test_mode=True))) else: error_logged = False # Display an output here if context == 0: # No context means it's a user we don't want seeing the stack trace cgi_form = cgi.FieldStorage() return """<br><div class="error"> There has been an error. {error_logged} </div> <div style="padding:10px"> Below is a copy of all the data you submitted. <br> <hr> <br> {cgi_form} </div> """.format( cgi_form = "<br><br>".join([str(http_data.value) for http_data in cgi_form.list]), error_logged = "The error has been logged automatically." if error_logged else "The error could not be logged" ) # No limit to context, this means we can print the stack trace return ("Content-type: text/html; charset=utf-8" + "\n" + error_output)
def test_cgi_form_functions(self): self.test_targets.append(common_f.get_val) self.test_targets.append(common_f.print_post_data) gui_test_utils.new_cgi_form(( ("a", 1), ("b", 2), ("c", 3), )) self.assertEqual(common_f.get_val("a"), 1) self.assertEqual(common_f.get_val("b"), 2) self.assertEqual(common_f.get_val("c"), 3) self.assertEqual(common_f.print_post_data(joiner="\n"), """a = 1 b = 2 c = 3""")