AC-612 Return appropriate status code for server errors. Catch IntegrityError via the API and return a 400 response with error message instead of server error.

This commit is contained in:
Chris Church
2013-11-06 23:44:48 -05:00
parent 475c0d87c2
commit 2b01cc7059
4 changed files with 21 additions and 5 deletions

View File

@@ -12,18 +12,20 @@ from django.conf import settings
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.db.models import Q
from django.db import IntegrityError
from django.shortcuts import get_object_or_404
from django.utils.datastructures import SortedDict
from django.utils.timezone import now
# Django REST Framework
from rest_framework.authtoken.views import ObtainAuthToken
from rest_framework.exceptions import PermissionDenied
from rest_framework.exceptions import PermissionDenied, ParseError
from rest_framework.parsers import YAMLParser
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.renderers import YAMLRenderer
from rest_framework.response import Response
from rest_framework.settings import api_settings
from rest_framework.views import exception_handler
from rest_framework import status
# AWX
@@ -36,6 +38,15 @@ from awx.api.serializers import *
from awx.api.generics import *
def api_exception_handler(exc):
'''
Override default API exception handler to catch IntegrityError exceptions.
'''
if isinstance(exc, IntegrityError):
exc = ParseError(exc.args[0])
return exception_handler(exc)
class ApiRootView(APIView):
permission_classes = (AllowAny,)