Skip to content
Closed
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
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()