Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions maintenance.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@
* `nosetests --with-cov --cov-report term-missing --cov tornado_json tests/`
* With `tox>=1.8.0` installed for both py27 and py34
* `sudo tox # runs test matrix with py27,py34 and tornado322,402`

3 changes: 2 additions & 1 deletion tests/test_tornado_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,12 +160,13 @@ class TestJSendMixin(TestTornadoJSONBase):
class MockJSendMixinRH(jsend.JSendMixin):
"""Mock handler for testing JSendMixin"""
_buffer = None
_finished = False

def write(self, data):
self._buffer = data

def finish(self):
pass
self._finished = True

@classmethod
@pytest.fixture(scope="class", autouse=True)
Expand Down
25 changes: 14 additions & 11 deletions tornado_json/jsend.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ def success(self, data):
:param data: Acts as the wrapper for any data returned by the API
call. If the call returns no data, data should be set to null.
"""
self.write({'status': 'success', 'data': data})
self.finish()
if not self._finished:
self.write({'status': 'success', 'data': data})
self.finish()

def fail(self, data):
"""There was a problem with the data submitted, or some pre-condition
Expand All @@ -29,8 +30,9 @@ def fail(self, data):
failed. If the reasons for failure correspond to POST values,
the response object's keys SHOULD correspond to those POST values.
"""
self.write({'status': 'fail', 'data': data})
self.finish()
if not self._finished:
self.write({'status': 'fail', 'data': data})
self.finish()

def error(self, message, data=None, code=None):
"""An error occurred in processing the request, i.e. an exception was
Expand All @@ -46,10 +48,11 @@ def error(self, message, data=None, code=None):
:type code: int
:param code: A numeric code corresponding to the error, if applicable
"""
result = {'status': 'error', 'message': message}
if data:
result['data'] = data
if code:
result['code'] = code
self.write(result)
self.finish()
if not self._finished:
result = {'status': 'error', 'message': message}
if data:
result['data'] = data
if code:
result['code'] = code
self.write(result)
self.finish()