mirror of
https://github.com/sprockets/sprockets.mixins.json_error.git
synced 2024-12-28 03:00:23 +00:00
commit
aff3d3869e
4 changed files with 130 additions and 11 deletions
22
README.rst
22
README.rst
|
@ -24,12 +24,30 @@ Requirements
|
|||
|
||||
Example
|
||||
-------
|
||||
This examples demonstrates how to use ``sprockets.mixins.json_error`` by ...
|
||||
This examples demonstrates how to use ``sprockets.mixins.json_error`` to format
|
||||
errors as JSON.
|
||||
|
||||
.. code:: python
|
||||
|
||||
from sprockets import mixins.json_error
|
||||
from tornado import web
|
||||
|
||||
class MyRequestHandler(json_error.JsonErrorMixin,
|
||||
web.RequestHandler):
|
||||
|
||||
def get(self, *args, **kwargs):
|
||||
raise web.HTTPError(404, log_message='My reason')
|
||||
|
||||
|
||||
The response from the handler will automatically be formatted as:
|
||||
|
||||
.. code:: json
|
||||
|
||||
{
|
||||
"message": "My reason",
|
||||
"type": "Not Found"
|
||||
}
|
||||
|
||||
# Example here
|
||||
|
||||
Version History
|
||||
---------------
|
||||
|
|
|
@ -4,5 +4,40 @@ mixins.json_error
|
|||
Handler mixin for writing JSON errors
|
||||
|
||||
"""
|
||||
version_info = (0, 0, 0)
|
||||
version_info = (1, 0, 0)
|
||||
__version__ = '.'.join(str(v) for v in version_info)
|
||||
|
||||
|
||||
class JsonErrorMixin(object):
|
||||
"""Mixin to write errors as JSON."""
|
||||
|
||||
def write_error(self, status_code, **kwargs):
|
||||
"""Suppress the automatic rendering of HTML code upon an error.
|
||||
|
||||
:param int status_code:
|
||||
The HTTP status code the :class:`HTTPError` raised.
|
||||
|
||||
:param dict kwargs:
|
||||
Automatically filled with exception information including
|
||||
the error that was raised, the class of error raised, and an
|
||||
object.
|
||||
|
||||
"""
|
||||
_, raised_error, _ = kwargs.get('exc_info', (None, None, None))
|
||||
|
||||
error_type = getattr(raised_error, 'error_type', self._reason)
|
||||
|
||||
try:
|
||||
error_message = raised_error.get_message()
|
||||
except AttributeError:
|
||||
error_message = 'Unexpected Error'
|
||||
|
||||
self.error = {
|
||||
'message': error_message,
|
||||
'type': error_type,
|
||||
}
|
||||
if hasattr(raised_error, 'documentation_url'):
|
||||
self.error['documentation_url'] = raised_error.documentation_url
|
||||
|
||||
self.set_header('Content-Type', 'application/json; charset=UTF-8')
|
||||
self.finish(self.error)
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
coverage>=3.7,<4
|
||||
coveralls>=0.4,<1
|
||||
nose>=1.3,<2
|
||||
tornado>=4.0,<5
|
||||
|
|
79
tests.py
79
tests.py
|
@ -2,12 +2,77 @@
|
|||
Tests for the sprockets.mixins.json_error package
|
||||
|
||||
"""
|
||||
import mock
|
||||
try:
|
||||
import unittest2 as unittest
|
||||
except ImportError:
|
||||
import unittest
|
||||
import json
|
||||
|
||||
from sprockets.mixins import json_error
|
||||
from tornado import testing, web
|
||||
|
||||
|
||||
class MyTest(unittest.TestCase):
|
||||
pass
|
||||
class HTTPErrorRequestHandler(
|
||||
json_error.JsonErrorMixin, web.RequestHandler):
|
||||
|
||||
def get(self):
|
||||
raise web.HTTPError(400, 'Error Reason')
|
||||
|
||||
|
||||
class CustomExceptionRequestHandler(
|
||||
json_error.JsonErrorMixin, web.RequestHandler):
|
||||
|
||||
class FailureError(Exception):
|
||||
|
||||
status_code = 400
|
||||
error_type = 'FailureError'
|
||||
documentation_url = 'http://www.example.com'
|
||||
|
||||
def get_message(self):
|
||||
return 'Too much Foo'
|
||||
|
||||
def get(self):
|
||||
raise self.FailureError()
|
||||
|
||||
|
||||
class UnexpectedErrorRequestHandler(
|
||||
json_error.JsonErrorMixin, web.RequestHandler):
|
||||
|
||||
def get(self):
|
||||
raise Exception()
|
||||
|
||||
|
||||
class TestHTTPError(testing.AsyncHTTPTestCase):
|
||||
|
||||
def get_app(self):
|
||||
return web.Application([('/', HTTPErrorRequestHandler)])
|
||||
|
||||
def test_tornado_thrown_exception(self):
|
||||
response = self.fetch('/')
|
||||
expected = {'message': 'Unexpected Error', 'type': 'Bad Request'}
|
||||
self.assertEqual(json.loads(response.body), expected)
|
||||
|
||||
|
||||
class TestCustomExceptions(testing.AsyncHTTPTestCase):
|
||||
|
||||
def get_app(self):
|
||||
return web.Application([('/', CustomExceptionRequestHandler)])
|
||||
|
||||
def test_tornado_custom_exception(self):
|
||||
response = self.fetch('/')
|
||||
expected = {
|
||||
'message': 'Too much Foo',
|
||||
'type': 'FailureError',
|
||||
'documentation_url': 'http://www.example.com',
|
||||
}
|
||||
self.assertEqual(json.loads(response.body), expected)
|
||||
|
||||
|
||||
class TestUnexpectedError(testing.AsyncHTTPTestCase):
|
||||
|
||||
def get_app(self):
|
||||
return web.Application([('/', UnexpectedErrorRequestHandler)])
|
||||
|
||||
def test_unexpected_exception(self):
|
||||
response = self.fetch('/')
|
||||
expected = {
|
||||
'message': 'Unexpected Error',
|
||||
'type': 'Internal Server Error'
|
||||
}
|
||||
self.assertEqual(json.loads(response.body), expected)
|
||||
|
|
Loading…
Reference in a new issue