Skip to content

Commit

Permalink
APIs should 404 rather than 500 when they can't find the thing.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmiller committed Feb 25, 2016
1 parent 312366a commit 1b6db23
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion opal/core/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,10 @@ class PatientListViewSet(viewsets.ViewSet):
base_name = 'patientlist'

def retrieve(self, request, pk=None):
patientlist = PatientList.get(pk)()
try:
patientlist = PatientList.get(pk)()
except ValueError:
return Response({'error': 'List does not exist'}, status=status.HTTP_404_NOT_FOUND)
return _build_json_response(patientlist.to_dict(request.user))


Expand Down
4 changes: 4 additions & 0 deletions opal/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -681,3 +681,7 @@ def test_retrieve_episodes(self, patient_list):
expected = _build_json_response({}).content
response = api.PatientListViewSet().retrieve(self.mock_request, pk='mylist').content
self.assertEqual(expected, response)

def test_retrieve_episodes_not_found(self):
response = api.PatientListViewSet().retrieve(self.mock_request, pk='not a real list at all')
self.assertEqual(404, response.status_code)

0 comments on commit 1b6db23

Please sign in to comment.