1

I am following the answer to the question Django REST Framework how to specify error code when raising validation error in serializer in order to custom exception handling, but i get an error.

This is my custom exception handler:

from rest_framework.exceptions import APIException
from django.utils.encoding import force_text
from rest_framework import status

class CustomValidation(APIException):
    status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
    default_detail = 'A server error ocurred'

    def __init__(self, detail, field, status_code):
        if status_code is not None:
            self.status_code = status_code
            if detail is not None:
                self.detail = {field: force_text(detail)}
            else:
                self.detail = {'detail': force_text(self.default_detail)}

This is where I raise the exception:

class UserSerializer(serializers.HyperlinkedModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'username', 'first_name', 'last_name', 'email', 'password')

    def create(self, validated_data):
        try:
            user = models.User.objects.get(email=validated_data.get('email'))
        except User.DoesNotExist:
            user = models.User.objects.create(**validated_data)

            user.set_password(user.password)
            user.save()
            Token.objects.create(user=user)

            return user
        else:
            raise CustomValidation('eMail already in use', 'email', status_code=status.HTTP_409_CONFLICT)

And this is the traceback that I get:

Internal Server Error: /es/users_manager/users/
Traceback (most recent call last):
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/views.py", line 480, in dispatch
    response = handler(request, *args, **kwargs)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/mixins.py", line 21, in create
    self.perform_create(serializer)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/mixins.py", line 26, in perform_create
    serializer.save()
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/serializers.py", line 214, in save
    self.instance = self.create(validated_data)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/Intellibook/UsersManagerApp/serializers.py", line 25, in create
    raise CustomValidation('eMail already in use', 'email', status_code=status.HTTP_409_CONFLICT)
GeneralApp.utils.CustomValidation: {'email': 'eMail already in use'}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 128, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/viewsets.py", line 103, in view
    return self.dispatch(request, *args, **kwargs)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/views.py", line 483, in dispatch
    response = self.handle_exception(exc)
  File "/Users/hugovillalobos/Documents/Code/IntellibookProject/IntellibookVenv/lib/python3.6/site-packages/rest_framework/views.py", line 440, in handle_exception
    response = exception_handler(exc, context)
TypeError: __init__() missing 1 required positional argument: 'status_code'
[07/Jun/2018 02:42:16] "POST /es/users_manager/users/ HTTP/1.1" 500 102763

I don't know what I am missing.

1
  • 1
    did you tried this, def __init__(self, detail, field, status_code=None) ? Commented Jun 7, 2018 at 4:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.