Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions tabbycat/api/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import logging

from django.conf import settings
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import exception_handler as drf_exception_handler

logger = logging.getLogger(__name__)


def api_exception_handler(exc, context):
"""Return JSON for unhandled errors instead of letting Django serve the HTML 500 page."""
response = drf_exception_handler(exc, context)
if response is not None:
return response

if settings.DEBUG:
return

logger.exception("Unhandled exception in API request")

data = {'detail': 'A server error occurred.'}

return Response(data, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
12 changes: 12 additions & 0 deletions tabbycat/api/tests/test_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from unittest.mock import patch

from django.conf import settings
from django.test import Client
Expand Down Expand Up @@ -33,6 +34,17 @@ def test_get_v1_root(self):
})


class APIExceptionHandlerTests(CompletedTournamentTestMixin, APITestCase):

@patch('api.views.TournamentViewSet.list', side_effect=RuntimeError('deliberate failure'))
def test_unhandled_exception_returns_json(self, _mock):
response = self.client.get(reverse('api-tournament-list'))
self.assertEqual(response.status_code, 500)
self.assertIn('application/json', response['Content-Type'])
payload = json.loads(response.content)
self.assertIn('detail', payload)


class TournamentViewsetTests(CompletedTournamentTestMixin, APITestCase):

def test_get_tournament_list(self):
Expand Down
1 change: 1 addition & 0 deletions tabbycat/settings/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@
# ==============================================================================

REST_FRAMEWORK = {
'EXCEPTION_HANDLER': 'api.exceptions.api_exception_handler',
'DEFAULT_RENDERER_CLASSES': [
'rest_framework.renderers.JSONRenderer',
],
Expand Down
Loading