mirror of
https://github.com/sprockets/sprockets.http.git
synced 2024-11-14 11:19:26 +00:00
Use mediatype.ContentMixin if its also extended
This commit is contained in:
parent
2fdc020dab
commit
f028723e45
8 changed files with 39 additions and 10 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -4,3 +4,4 @@ build
|
||||||
dist
|
dist
|
||||||
env
|
env
|
||||||
*.egg-info
|
*.egg-info
|
||||||
|
.coverage
|
||||||
|
|
|
@ -79,9 +79,12 @@ exceptions will include the stack traces, etc.
|
||||||
Standardized Error Response Documents
|
Standardized Error Response Documents
|
||||||
-------------------------------------
|
-------------------------------------
|
||||||
Version 0.5.0 also introduced the :class:`~sprockets.http.mixins.ErrorWriter`
|
Version 0.5.0 also introduced the :class:`~sprockets.http.mixins.ErrorWriter`
|
||||||
class which implements ``write_error`` to provide a standard JSON
|
class which implements ``write_error`` to provide a standard machine-readable
|
||||||
document response instead of the default HTML response that Tornado
|
document response instead of the default HTML response that Tornado implements.
|
||||||
implements.
|
If :class:`~sprockets.mixins.mediatype.ContentMixin` is being used as well,
|
||||||
|
``write_error`` will use
|
||||||
|
:meth:`~sprockets.mixins.mediatype.ContentMixin.send_response` to send the
|
||||||
|
document, otherwise it is sent as JSON.
|
||||||
|
|
||||||
.. autoclass:: sprockets.http.mixins.ErrorWriter
|
.. autoclass:: sprockets.http.mixins.ErrorWriter
|
||||||
:members:
|
:members:
|
||||||
|
|
|
@ -2,6 +2,9 @@
|
||||||
|
|
||||||
Release History
|
Release History
|
||||||
===============
|
===============
|
||||||
|
`1.0.1`_ (20 Nov 2015)
|
||||||
|
----------------------
|
||||||
|
- Add support for ``sprockets.mixins.mediatype`` in ``sprockets.http.mixins.ErrorWriter``
|
||||||
|
|
||||||
`1.0.0`_ (20 Nov 2015)
|
`1.0.0`_ (20 Nov 2015)
|
||||||
----------------------
|
----------------------
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
nose>=1.3.1,<2
|
nose>=1.3.1,<2
|
||||||
|
mock>=1.3,<2
|
||||||
|
|
1
setup.py
1
setup.py
|
@ -2,7 +2,6 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
import os.path
|
import os.path
|
||||||
import sys
|
|
||||||
|
|
||||||
import setuptools
|
import setuptools
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
version_info = (1, 0, 0)
|
version_info = (1, 0, 1)
|
||||||
__version__ = '.'.join(str(v) for v in version_info)
|
__version__ = '.'.join(str(v) for v in version_info)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -78,9 +78,14 @@ class ErrorWriter(object):
|
||||||
"""
|
"""
|
||||||
Write error bodies out consistently.
|
Write error bodies out consistently.
|
||||||
|
|
||||||
Mix this class in to your inheritance chain to include error
|
Mix this class in to your inheritance chain to include error bodies in a
|
||||||
bodies as a standard JSON document. The error document has
|
machine-readable document format.
|
||||||
three simple properties:
|
|
||||||
|
If :class:`~sprockets.mixins.mediatype.ContentMixin` is also in use, it
|
||||||
|
will send the error response with it, otherwise the response is sent as
|
||||||
|
a JSON document.
|
||||||
|
|
||||||
|
The error document has three simple properties:
|
||||||
|
|
||||||
**type**
|
**type**
|
||||||
This is the type of exception that occurred or ``null``.
|
This is the type of exception that occurred or ``null``.
|
||||||
|
@ -121,5 +126,9 @@ class ErrorWriter(object):
|
||||||
reason = kwargs.get('reason', _get_http_reason(status_code))
|
reason = kwargs.get('reason', _get_http_reason(status_code))
|
||||||
error_body.setdefault('message', reason)
|
error_body.setdefault('message', reason)
|
||||||
|
|
||||||
self.set_header('Content-Type', 'application/json')
|
# If sprockets.mixins.media_type is being used, use it
|
||||||
self.write(json.dumps(error_body).encode('utf-8'))
|
if hasattr(self, 'send_response'):
|
||||||
|
self.send_response(error_body)
|
||||||
|
else:
|
||||||
|
self.set_header('Content-Type', 'application/json; charset=utf-8')
|
||||||
|
self.write(json.dumps(error_body).encode('utf-8'))
|
||||||
|
|
13
tests.py
13
tests.py
|
@ -1,5 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
|
import mock
|
||||||
|
|
||||||
from tornado import httputil, testing, web
|
from tornado import httputil, testing, web
|
||||||
|
|
||||||
|
@ -161,3 +162,15 @@ class ErrorWriterTests(testing.AsyncHTTPTestCase):
|
||||||
|
|
||||||
body = self._decode_response(response)
|
body = self._decode_response(response)
|
||||||
self.assertGreater(len(body['traceback']), 0)
|
self.assertGreater(len(body['traceback']), 0)
|
||||||
|
|
||||||
|
def test_that_mediatype_mixin_is_honored(self):
|
||||||
|
send_response = mock.Mock()
|
||||||
|
setattr(examples.StatusHandler, 'send_response', send_response)
|
||||||
|
response = self.fetch('/status/500')
|
||||||
|
self.assertEqual(response.code, 500)
|
||||||
|
send_response.assert_called_once_with({
|
||||||
|
'type': None,
|
||||||
|
'message': 'Internal Server Error',
|
||||||
|
'traceback': None
|
||||||
|
})
|
||||||
|
delattr(examples.StatusHandler, 'send_response')
|
||||||
|
|
Loading…
Reference in a new issue