1
Fork 0

Added error catching

This commit is contained in:
Jake Howard 2015-12-10 14:25:08 +00:00
parent 44bfa68abf
commit 2620ccac96
2 changed files with 12 additions and 5 deletions

View file

@ -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)

View file

@ -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)