changeset 200:76e459d48c43

serve: Correctly pass on the HTTP status code when an error occurs.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 13 Jan 2015 21:53:22 -0800
parents aaf08277b96d
children 0c9de41689bb
files piecrust/serving.py
diffstat 1 files changed, 7 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/piecrust/serving.py	Tue Jan 13 20:41:44 2015 -0800
+++ b/piecrust/serving.py	Tue Jan 13 21:53:22 2015 -0800
@@ -91,13 +91,13 @@
 
     def _run_request(self, environ, start_response):
         try:
-            return self._run_piecrust(environ, start_response)
+            return self._try_run_request(environ, start_response)
         except Exception as ex:
             if self.debug:
                 raise
             return self._handle_error(ex, environ, start_response)
 
-    def _run_piecrust(self, environ, start_response):
+    def _try_run_request(self, environ, start_response):
         request = Request(environ)
 
         # We don't support anything else than GET requests since we're
@@ -377,16 +377,19 @@
         return response
 
     def _handle_error(self, exception, environ, start_response):
+        code = 500
         path = 'error'
-        if isinstance(exception, NotFound):
-            path += '404'
         description = str(exception)
         if isinstance(exception, HTTPException):
+            code = exception.code
             description = exception.description
+            if isinstance(exception, NotFound):
+                path += '404'
         env = Environment(loader=ErrorMessageLoader())
         template = env.get_template(path)
         context = {'details': description}
         response = Response(template.render(context), mimetype='text/html')
+        response.status_code = code
         return response(environ, start_response)