From 2620ccac967363f426cdf66ee488f141d97c4564 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Thu, 10 Dec 2015 14:25:08 +0000 Subject: [PATCH] Added error catching --- django_client_reverse/tests.py | 8 ++++++-- django_client_reverse/views.py | 9 ++++++--- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/django_client_reverse/tests.py b/django_client_reverse/tests.py index 054f6f7..72e68a1 100644 --- a/django_client_reverse/tests.py +++ b/django_client_reverse/tests.py @@ -6,6 +6,10 @@ class ReverserTestCase(APITestCase): def setUp(self): super().setUp() - def test_thing(self): + def test_reverser(self): response = self.client.post(reverse('reverser'), data={'ident': 'tests:root'}) - self.assertEqual(response.data, '/tests/') + self.assertEqual(response.data, reverse('tests:root')) + + def test_invalid(self): + response = self.client.post(reverse('reverser'), data={'ident': 'tests:not_a_thing'}) + self.assertEqual(response.status_code, 404) diff --git a/django_client_reverse/views.py b/django_client_reverse/views.py index cc2606f..4c6cf93 100644 --- a/django_client_reverse/views.py +++ b/django_client_reverse/views.py @@ -1,7 +1,7 @@ from rest_framework.views import APIView from rest_framework.response import Response from rest_framework.renderers import JSONRenderer -from django.core.urlresolvers import reverse +from django.core.urlresolvers import reverse, NoReverseMatch from .serializers import ReverserInputSerializer @@ -11,5 +11,8 @@ class Reverser(APIView): def post(self, request, format=None): input_serializer = ReverserInputSerializer(data=request.data) input_serializer.is_valid(raise_exception=True) - url = reverse(input_serializer.validated_data['ident']) - return Response(url) + try: + url = reverse(input_serializer.validated_data['ident']) + except NoReverseMatch: + return Response(status=404) + return Response(url, 302)