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:
codecov-io: ## [Current coverage][cc-pull] is 100% (diff: 100%)
> Merging [#851][cc-pull] into [master][cc-base-branch] will not change coverage

```diff
@@           master   #851   diff @@
====================================
  Files          29     29          
  Lines        1848   1846     -2   
  Methods         0      0          
  Messages        0      0          
  Branches      307    305     -2   
====================================
- Hits         1848   1846     -2   
  Misses          0      0          
  Partials        0      0          
```

> Powered by [Codecov](https://codecov.io?src=pr). Last update [a9f1813...8e1cb96][cc-compare]
[cc-base-branch]: https://codecov.io/gh/falconry/falcon/branch/master?src=pr
[cc-compare]: https://codecov.io/gh/falconry/falcon/compare/a9f1813bbd85ae58dd3d81aaea53a6db58032c3b...8e1cb96cb4f92f3abf961e3ac78c0a71edcc833f?src=pr
[cc-pull]: https://codecov.io/gh/falconry/falcon/pull/851?src=pr

Patch:
diff --git a/falcon/http_error.py b/falcon/http_error.py
index 1e9143e..6b7cd1e 100644
--- 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 @@ class HTTPError(Exception):
 
         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 @@ class HTTPError(Exception):
 
         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
index 9cb39b4..14f0eef 100644
--- a/tests/test_httperror.py
+++ b/tests/test_httperror.py
@@ -259,15 +259,15 @@ class TestHTTPError(testing.TestBase):
     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 @@ class TestHTTPError(testing.TestBase):
         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 @@ class TestHTTPError(testing.TestBase):
         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 @@ class TestHTTPError(testing.TestBase):
                         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'])