Added templates for 404 and 500 errors. 404 displays a generic "page
not found" error. 500 displays the exception type and value, along with
a traceback that can be revealed.

Risk: low
Visibility: medium (changing the look and feel of errors)

Signed-off-by: James Ren <jamesren@google.com>


git-svn-id: http://test.kernel.org/svn/autotest/trunk@1895 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/frontend/afe/views.py b/frontend/afe/views.py
index 365f84f..8d7911b 100644
--- a/frontend/afe/views.py
+++ b/frontend/afe/views.py
@@ -1,8 +1,10 @@
-import urllib2
+import urllib2, sys, traceback, cgi
 
 from frontend.afe import models, rpc_handler, rpc_interface, site_rpc_interface
 from frontend.afe import rpc_utils
 from django.http import HttpResponse, HttpResponsePermanentRedirect
+from django.http import HttpResponseServerError
+from django.template import Context, loader
 
 # since site_rpc_interface is later in the list, its methods will override those
 # of rpc_interface
@@ -41,3 +43,14 @@
         if header not in ('connection',):
             http_response[header] = value
     return http_response
+
+
+def handler500(request):
+    t = loader.get_template('500.html')
+    trace = traceback.format_exc()
+    context = Context({
+        'type': sys.exc_type,
+        'value': sys.exc_value,
+        'traceback': cgi.escape(trace)
+    })
+    return HttpResponseServerError(t.render(context))
diff --git a/frontend/settings.py b/frontend/settings.py
index 5eb6650..d5a5ae8 100644
--- a/frontend/settings.py
+++ b/frontend/settings.py
@@ -93,6 +93,10 @@
     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
     # Always use forward slashes, even on Windows.
     # Don't forget to use absolute paths, not relative paths.
+
+    # Note: we are violating the above recommendation about not using relative
+    # paths, but this appears to work fine.
+    "templates"
 )
 
 INSTALLED_APPS = (
diff --git a/frontend/templates/404.html b/frontend/templates/404.html
new file mode 100644
index 0000000..c3cf705
--- /dev/null
+++ b/frontend/templates/404.html
@@ -0,0 +1,16 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
+"http://www.w3.org/TR/html4/strict.dtd">
+
+<html>
+  <head>
+    <title>Page not found</title>
+  </head>
+  <body>
+    <h2>Page not found</h2>
+
+    <p>
+      404: The page was not found. Please check the URL in your address bar.
+    </p>
+
+  </body>
+</html>
diff --git a/frontend/templates/500.html b/frontend/templates/500.html
new file mode 100644
index 0000000..0205579
--- /dev/null
+++ b/frontend/templates/500.html
@@ -0,0 +1,23 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
+"http://www.w3.org/TR/html4/strict.dtd">
+
+<html>
+  <head>
+    <title>{{ type }}</title>
+  </head>
+  <body>
+    <h2>{{ type }}</h2>
+
+    <p>{{ value }}</p>
+
+    <p><a href="#" onclick=
+        "document.getElementById('traceback').style.display='';return false;">
+      Show Traceback
+    </a></p>
+
+    <pre style="display:none" id="traceback">
+{{ traceback }}
+    </pre>
+
+  </body>
+</html>
diff --git a/frontend/urls.py b/frontend/urls.py
index b66c8d4..571a3a5 100644
--- a/frontend/urls.py
+++ b/frontend/urls.py
@@ -3,6 +3,8 @@
 
 RE_PREFIX = '^' + settings.URL_PREFIX
 
+handler500 = 'frontend.afe.views.handler500'
+
 urlpatterns = patterns('',
     (RE_PREFIX + r'admin/', include('django.contrib.admin.urls')),
     (RE_PREFIX, include('frontend.afe.urls')),