Repository: falconry/falcon

Issue:
Error title should default to HTTP status
When the title is set to `None`, `HTTPError` should default to simply reusing the description passed in with the code in the `status` argument, i.e.: 

``` py
self.title = status_code[4:]
```



Discussion:
I will be working on this :) 


Resolving patch:
diff --git a/falcon/http_error.py b/falcon/http_error.py
--- a/falcon/http_error.py
+++ b/falcon/http_error.py
@@ -40,8 +40,7 @@ class HTTPError(Exception):
             returns ``True``, but child classes may override it
             in order to return ``False`` when an empty HTTP body is desired.
             See also the ``falcon.http_error.NoRepresentation`` mixin.
-        title (str): Error title to send to the client. Will be ``None`` if
-            the error should result in an HTTP response with an empty body.
+        title (str): Error title to send to the client.
         description (str): Description of the error to send to the client.
         headers (dict): Extra headers to add to the response.
         link (str): An href that the client can provide to the user for
@@ -53,7 +52,8 @@ class HTTPError(Exception):
         status (str): HTTP status code and text, such as "400 Bad Request"
 
     Keyword Args:
-        title (str): Human-friendly error title (default ``None``).
+        title (str): Human-friendly error title. If not provided, defaults
+            to the HTTP status line as determined by the ``status`` argument.
         description (str): Human-friendly description of the error, along with
             a helpful suggestion or two (default ``None``).
         headers (dict or list): A ``dict`` of header names and values
@@ -97,7 +97,13 @@ class HTTPError(Exception):
     def __init__(self, status, title=None, description=None, headers=None,
                  href=None, href_text=None, code=None):
         self.status = status
-        self.title = title
+
+        # TODO(kgriffs): HTTP/2 does away with the "reason phrase". Eventually
+        #   we'll probably switch over to making everything code-based to more
+        #   easily support HTTP/2. When that happens, should we continue to
+        #   include the reason phrase in the title?
+        self.title = title or status
+
         self.description = description
         self.headers = headers
         self.code = code
@@ -133,8 +139,7 @@ def to_dict(self, obj_type=dict):
 
         obj = obj_type()
 
-        if self.title is not None:
-            obj['title'] = self.title
+        obj['title'] = self.title
 
         if self.description is not None:
             obj['description'] = self.description
@@ -171,8 +176,7 @@ def to_xml(self):
 
         error_element = et.Element('error')
 
-        if self.title is not None:
-            et.SubElement(error_element, 'title').text = self.title
+        et.SubElement(error_element, 'title').text = self.title
 
         if self.description is not None:
             et.SubElement(error_element, 'description').text = self.description


Test patch:
diff --git a/tests/test_httperror.py b/tests/test_httperror.py
--- a/tests/test_httperror.py
+++ b/tests/test_httperror.py
@@ -259,15 +259,15 @@ def test_base_class(self):
     def test_no_description_json(self):
         body = self.simulate_request('/fail', method='PATCH')
         self.assertEqual(self.srmock.status, falcon.HTTP_400)
-        self.assertEqual(body, [b'{}'])
+        self.assertEqual(body, [b'{\n    "title": "400 Bad Request"\n}'])
 
     def test_no_description_xml(self):
         body = self.simulate_request('/fail', method='PATCH',
                                      headers={'Accept': 'application/xml'})
         self.assertEqual(self.srmock.status, falcon.HTTP_400)
 
-        expected_xml = (b'<?xml version="1.0" encoding="UTF-8"?>'
-                        b'<error />')
+        expected_xml = (b'<?xml version="1.0" encoding="UTF-8"?><error>'
+                        b'<title>400 Bad Request</title></error>')
 
         self.assertEqual(body, [expected_xml])
 
@@ -550,6 +550,7 @@ def test_404_with_body(self):
         self.assertEqual(self.srmock.status, falcon.HTTP_404)
         self.assertNotEqual(response, [])
         expected_body = {
+            u'title': u'404 Not Found',
             u'description': u'Not Found'
         }
         self.assertEqual(json.loads(response), expected_body)
@@ -590,6 +591,7 @@ def test_405_with_body(self):
         self.assertEqual(self.srmock.status, falcon.HTTP_405)
         self.assertNotEqual(response, [])
         expected_body = {
+            u'title': u'405 Method Not Allowed',
             u'description': u'Not Allowed'
         }
         self.assertEqual(json.loads(response), expected_body)
@@ -777,3 +779,14 @@ def test_misc(self):
                         needs_title=False)
         self._misc_test(falcon.HTTPInternalServerError, falcon.HTTP_500)
         self._misc_test(falcon.HTTPBadGateway, falcon.HTTP_502)
+
+    def test_title_default_message_if_none(self):
+        headers = {
+            'X-Error-Status': falcon.HTTP_503
+        }
+
+        body = self.simulate_request('/fail', headers=headers, decode='utf-8')
+        body_json = json.loads(body)
+
+        self.assertEqual(self.srmock.status, headers['X-Error-Status'])
+        self.assertEqual(body_json['title'], headers['X-Error-Status'])