Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed error response

  • Loading branch information...
commit 35361258862a04b8b8a98eec59329c3ae1b9c23e 1 parent 79b682e
@klen authored
View
6 adrest/resources/rpc.py
@@ -4,6 +4,7 @@
from ..utils.emitter import JSONPEmitter, JSONEmitter
from ..utils.parser import JSONParser, FormParser
from ..utils.tools import as_tuple
+from ..utils.response import SerializedHttpResponse
from ..views import ResourceView
@@ -53,7 +54,10 @@ def handle_request(self, request, **resources):
return self.rpc_call(request, **payload)
except Exception, e:
- return dict(error=dict(message=str(e)))
+ return SerializedHttpResponse(
+ dict(error=dict(message=str(e))),
+ error=True
+ )
def rpc_call(self, request, method=None, params=None, **kwargs):
args = []
View
4 adrest/utils/emitter.py
@@ -103,7 +103,7 @@ def serialize(self, content):
**getattr(self.resource, 'related', dict())
)
return self.xmldoc_tpl % (
- 'true' if self.response.status_code == HTTP_200_OK else 'false',
+ 'true' if not self.response.error else 'false',
self.resource.version,
int(mktime(datetime.now().timetuple())),
worker.serialize(content)
@@ -114,7 +114,7 @@ class TemplateEmitter(BaseEmitter):
" Serialize by django templates. "
def serialize(self, content):
- if self.response.status_code != HTTP_200_OK:
+ if self.response.error:
template_name = op.join('api', 'error.%s' % self.format)
else:
template_name = self.resource.template or self.get_template_path(content)
View
16 adrest/utils/response.py
@@ -1,10 +1,11 @@
from django.http import HttpResponse
+from .status import HTTP_200_OK
class SerializedMeta(type):
def __call__(mcs, content='', mimetype=None, status=None,
- content_type=None, finaly=False):
+ content_type=None, finaly=False, error=False):
""" Don't create clones.
"""
@@ -20,7 +21,8 @@ def __call__(mcs, content='', mimetype=None, status=None,
mimetype=mimetype,
status=status,
content_type=content_type,
- finaly=finaly
+ finaly=finaly,
+ error=error,
)
@@ -29,22 +31,30 @@ class SerializedHttpResponse(HttpResponse):
Django http response will be returned as is.
:param finaly: Prevent serialization.
+ :param error: Force error in response.
"""
__metaclass__ = SerializedMeta
def __init__(self, content='', mimetype=None, status=None,
- content_type=None, finaly=False):
+ content_type=None, finaly=False, error=False):
"""
Save original response.
"""
self.response = content
self.finaly = finaly
+ self._error = error
super(SerializedHttpResponse, self).__init__(
content,
mimetype=mimetype,
status=status,
content_type=content_type)
+ @property
+ def error(self):
+ return self._error or self.status_code != HTTP_200_OK
+
def __repr__(self):
return "<SerializedHttpResponse %s>" % self.status_code
+
+# pymode:lint_ignore=E1103
Please sign in to comment.
Something went wrong with that request. Please try again.