Skip to content

Commit

Permalink
Ensure that >400 level HTTP errors are properly handled
Browse files Browse the repository at this point in the history
Fixes: #1072

We discovored that the API endpoint to get an enketo edit url
encountered >400 level errors it did not properly set the HTTP
status code and instead returned the edit url as false.

This set of commits fixes that by raising an EnketoError exception
which has a friendly default error message that can be overriden.
  • Loading branch information
moshthepitt committed Aug 3, 2017
1 parent 8614c5b commit cc7fe7f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 6 deletions.
13 changes: 13 additions & 0 deletions onadata/apps/api/tests/viewsets/test_data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ def enketo_mock(url, request):
return response


@urlmatch(netloc=r'(.*\.)?enketo\.ona\.io$')
def enketo_mock_http_413(url, request):
response = requests.Response()
response.status_code = 413
response._content = ''
return response


def _data_list(formid):
return [{
u'id': formid,
Expand Down Expand Up @@ -1128,6 +1136,11 @@ def test_get_enketo_edit_url(self):
response.data['url'],
"https://hmh2a.enketo.ona.io")

with HTTMock(enketo_mock_http_413):
response = view(request, pk=formid, dataid=dataid)
self.assertEqual(response.status_code, 400)
self.assertEqual(response.get('Cache-Control'), None)

def test_get_form_public_data(self):
self._make_submissions()
view = DataViewSet.as_view({'get': 'list'})
Expand Down
2 changes: 1 addition & 1 deletion onadata/apps/api/viewsets/data_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ def enketo(self, request, *args, **kwargs):
data["url"] = get_enketo_edit_url(
request, self.object, return_url)
except EnketoError as e:
data['detail'] = "{}".format(e)
raise ParseError(str(e))
else:
raise PermissionDenied(_(u"You do not have edit permissions."))

Expand Down
21 changes: 16 additions & 5 deletions onadata/libs/utils/viewer_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from django.core.files.uploadedfile import InMemoryUploadedFile
from django.core.mail import mail_admins
from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy

from onadata.libs.utils import common_tags

Expand All @@ -22,7 +23,18 @@ class MyError(Exception):


class EnketoError(Exception):
pass

default_message = ugettext_lazy("There was a problem with your submission"
" or form. Please contact support.")

def __init__(self, message=None):
if message is None:
self.message = self.default_message
else:
self.message = message

def __str__(self):
return "{}".format(self.message)


def image_urls_for_form(xform):
Expand Down Expand Up @@ -120,8 +132,8 @@ def report_exception(subject, info, exc_info=None):
info += u"".join(traceback.format_exception(*exc_info))

if settings.DEBUG:
print subject
print info
print(subject)
print(info)
else:
mail_admins(subject=subject, message=info)

Expand Down Expand Up @@ -189,7 +201,6 @@ def enketo_url(form_url,
req = requests.post(
url, data=values, auth=(settings.ENKETO_API_TOKEN, ''),
verify=getattr(settings, 'VERIFY_SSL', True))

if req.status_code in [200, 201]:
try:
response = req.json()
Expand All @@ -208,7 +219,7 @@ def enketo_url(form_url,
try:
response = req.json()
except ValueError:
pass
raise EnketoError()
else:
if 'message' in response:
raise EnketoError(response['message'])
Expand Down

0 comments on commit cc7fe7f

Please sign in to comment.