# HG changeset patch # User Ludovic Chabant # Date 1421214802 28800 # Node ID 76e459d48c438edd8ef642229e5b556c40845eaf # Parent aaf08277b96d0b88b8cc9cb8ba3ac01aa7e83135 serve: Correctly pass on the HTTP status code when an error occurs. diff -r aaf08277b96d -r 76e459d48c43 piecrust/serving.py --- 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)