Repository: falconry/falcon
Pull request #1498 (2019-04-23 02:07:30)

fix(errors): overridable title for HTTPNotAcceptable
and HTTPUnsupportedMediaType

Coykto: Please don't close it, i'll fix it once i'll have time.
codecov[bot]: # [Codecov](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=h1) Report
> Merging [#1498](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=desc) into [master](https://codecov.io/gh/falconry/falcon/commit/f7927da2a3b390e00872cef9d1526418f1b0c23d?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/falconry/falcon/pull/1498/graphs/tree.svg?width=650&token=zOtkfNQ3h6&height=150&src=pr)](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=tree)

```diff
@@          Coverage Diff           @@
##           master   #1498   +/-   ##
======================================
  Coverage     100%    100%           
======================================
  Files          38      38           
  Lines        2567    2561    -6     
  Branches      391     391           
======================================
- Hits         2567    2561    -6
```


| [Impacted Files](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [falcon/media/handlers.py](https://codecov.io/gh/falconry/falcon/pull/1498/diff?src=pr&el=tree#diff-ZmFsY29uL21lZGlhL2hhbmRsZXJzLnB5) | `100% <ø> (ø)` | :arrow_up: |
| [falcon/errors.py](https://codecov.io/gh/falconry/falcon/pull/1498/diff?src=pr&el=tree#diff-ZmFsY29uL2Vycm9ycy5weQ==) | `100% <100%> (ø)` | :arrow_up: |
| [falcon/response.py](https://codecov.io/gh/falconry/falcon/pull/1498/diff?src=pr&el=tree#diff-ZmFsY29uL3Jlc3BvbnNlLnB5) | `100% <0%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=footer). Last update [f7927da...fc4dbc4](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

codecov[bot]: # [Codecov](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=h1) Report
> Merging [#1498](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=desc) into [master](https://codecov.io/gh/falconry/falcon/commit/f7927da2a3b390e00872cef9d1526418f1b0c23d?src=pr&el=desc) will **not change** coverage.
> The diff coverage is `100%`.

[![Impacted file tree graph](https://codecov.io/gh/falconry/falcon/pull/1498/graphs/tree.svg?width=650&token=zOtkfNQ3h6&height=150&src=pr)](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=tree)

```diff
@@          Coverage Diff           @@
##           master   #1498   +/-   ##
======================================
  Coverage     100%    100%           
======================================
  Files          38      38           
  Lines        2567    2561    -6     
  Branches      391     391           
======================================
- Hits         2567    2561    -6
```


| [Impacted Files](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [falcon/media/handlers.py](https://codecov.io/gh/falconry/falcon/pull/1498/diff?src=pr&el=tree#diff-ZmFsY29uL21lZGlhL2hhbmRsZXJzLnB5) | `100% <ø> (ø)` | :arrow_up: |
| [falcon/errors.py](https://codecov.io/gh/falconry/falcon/pull/1498/diff?src=pr&el=tree#diff-ZmFsY29uL2Vycm9ycy5weQ==) | `100% <100%> (ø)` | :arrow_up: |
| [falcon/response.py](https://codecov.io/gh/falconry/falcon/pull/1498/diff?src=pr&el=tree#diff-ZmFsY29uL3Jlc3BvbnNlLnB5) | `100% <0%> (ø)` | :arrow_up: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=footer). Last update [f7927da...fc4dbc4](https://codecov.io/gh/falconry/falcon/pull/1498?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

Coykto: I guess requested changes are addressed. Not sure, though.

Patch:
diff --git a/falcon/errors.py b/falcon/errors.py
index a433176..d16c21a 100644
--- a/falcon/errors.py
+++ b/falcon/errors.py
@@ -379,9 +379,9 @@ class HTTPNotAcceptable(HTTPError):
             base articles related to this error (default ``None``).
     """
 
-    def __init__(self, description=None, headers=None, **kwargs):
+    def __init__(self, title=None, description=None, headers=None, **kwargs):
         super(HTTPNotAcceptable, self).__init__(status.HTTP_406,
-                                                'Media type not acceptable',
+                                                title,
                                                 description, headers,
                                                 **kwargs)
 
@@ -763,9 +763,9 @@ class HTTPUnsupportedMediaType(HTTPError):
             base articles related to this error (default ``None``).
     """
 
-    def __init__(self, description=None, headers=None, **kwargs):
+    def __init__(self, title=None, description=None, headers=None, **kwargs):
         super(HTTPUnsupportedMediaType, self).__init__(
-            status.HTTP_415, 'Unsupported media type',
+            status.HTTP_415, title,
             description, headers, **kwargs)
 
 
diff --git a/falcon/media/handlers.py b/falcon/media/handlers.py
index 3b742e4..90e78ae 100644
--- a/falcon/media/handlers.py
+++ b/falcon/media/handlers.py
@@ -47,7 +47,7 @@ class Handlers(UserDict):
 
         if not resolved:
             raise errors.HTTPUnsupportedMediaType(
-                '{0} is an unsupported media type.'.format(media_type)
+                description='{0} is an unsupported media type.'.format(media_type)
             )
 
         return self.data[resolved]


Test patch:
diff --git a/tests/test_error.py b/tests/test_error.py
index 3e2b4c8..ea33ae1 100644
--- a/tests/test_error.py
+++ b/tests/test_error.py
@@ -139,6 +139,13 @@ def test_http_not_acceptable_no_title_and_desc_and_challenges():
         assert e.description is None
 
 
+def test_http_not_acceptable_with_title():
+    try:
+        raise falcon.HTTPNotAcceptable(title='test title')
+    except falcon.HTTPNotAcceptable as e:
+        assert e.title == 'test title'
+
+
 def test_http_not_acceptable_with_title_and_desc_and_challenges():
     try:
         raise falcon.HTTPNotAcceptable(description='Testdescription')
@@ -153,6 +160,13 @@ def test_http_unsupported_media_type_no_title_and_desc_and_challenges():
         assert e.description is None
 
 
+def test_http_unsupported_media_type_with_title():
+    try:
+        raise falcon.HTTPUnsupportedMediaType(title='test title')
+    except falcon.HTTPUnsupportedMediaType as e:
+        assert e.title == 'test title'
+
+
 def test_http_unsupported_media_type_with_title_and_desc_and_challenges():
     try:
         raise falcon.HTTPUnsupportedMediaType(description='boom')
diff --git a/tests/test_example.py b/tests/test_example.py
index c2a9eed..cbcf638 100644
--- a/tests/test_example.py
+++ b/tests/test_example.py
@@ -82,13 +82,15 @@ class RequireJSON:
     def process_request(self, req, resp):
         if not req.client_accepts_json:
             raise falcon.HTTPNotAcceptable(
-                'This API only supports responses encoded as JSON.',
+                title='406 Not Acceptable',
+                description='This API only supports responses encoded as JSON.',
                 href='http://docs.examples.com/api/json')
 
         if req.method in ('POST', 'PUT'):
             if 'application/json' not in req.content_type:
                 raise falcon.HTTPUnsupportedMediaType(
-                    'This API only supports requests encoded as JSON.',
+                    title='415 Unsupported Media Type',
+                    description='This API only supports requests encoded as JSON.',
                     href='http://docs.examples.com/api/json')
 
 
diff --git a/tests/test_request_media.py b/tests/test_request_media.py
index 9a7d9b9..be277bc 100644
--- a/tests/test_request_media.py
+++ b/tests/test_request_media.py
@@ -74,8 +74,10 @@ def test_unknown_media_type(media_type):
     with pytest.raises(errors.HTTPUnsupportedMediaType) as err:
         client.resource.captured_req.media
 
-    msg = '{} is an unsupported media type.'.format(media_type)
-    assert err.value.description == msg
+    title_msg = '415 Unsupported Media Type'
+    description_msg = '{} is an unsupported media type.'.format(media_type)
+    assert err.value.title == title_msg
+    assert err.value.description == description_msg
 
 
 @pytest.mark.parametrize('media_type', [