1

I'm using the django-rest-passwordreset package to implement password reset endpoints. When a given email doesn't exist, it throws a ValidationError exception, causing a 500 error. How can I catch this error, and return a 400 instead? I simply added the urls as shown below, so I don't think I can surround it with an try: except or something similar.

urlpatterns=[
...
url(r'^password/recover/', include('django_rest_passwordreset.urls')),
... ]

Exception:

ValidationError at /password/recover/ {'email': ['There is no active user associated with this e-mail address or the password can not be changed']}
Request Method: POST Request URL: http://192.168.99.100:8000/password/recover/
Django Version: 2.0.6
Traceback:
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/exception.py" in inner 35. response = get_response(request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 128. response = self.process_exception_by_middleware(e, request)
File "/usr/local/lib/python3.6/site-packages/django/core/handlers/base.py" in _get_response 126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/usr/local/lib/python3.6/site-packages/django/views/decorators/csrf.py" in wrapped_view 54. return view_func(*args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django/views/generic/base.py" in view 69. return self.dispatch(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py" in dispatch 483. response = self.handle_exception(exc)
File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py" in handle_exception 443. self.raise_uncaught_exception(exc)
File "/usr/local/lib/python3.6/site-packages/rest_framework/views.py" in dispatch 480. response = handler(request, *args, **kwargs)
File "/usr/local/lib/python3.6/site-packages/django_rest_passwordreset/views.py" in post 116. code='invalid')}
Exception Type: ValidationError at /password/recover/ Exception Value: {'email': ['There is no active user associated with this e-mail address or the password can not be changed']}
Request information:
USER: AnonymousUser
GET: No GET data
POST: No POST data
FILES: No FILES data
COOKIES: No cookie data
HEADERS: "email" : "[email protected]"

4
  • ValidationError should throw 400 not 500. Can you post the exception message together with traceback? Commented Jul 3, 2018 at 16:17
  • error 500 occurs when your url is incorrect and cannot find the view. Commented Jul 4, 2018 at 4:06
  • can I see your urls.py file? Commented Jul 4, 2018 at 4:06
  • @mariodev: Added the ValidationError traceback. The relevant urlpattern is already there, and works fine for correct email addresses. Commented Jul 4, 2018 at 7:07

1 Answer 1

1

I managed to solve it by creating a custom Exception handler as described here.
Additional information:
- Filter on the specific error message to avoid catching ALL ValidationErrors,
- Otherwise return the standard Django exception response.

Sign up to request clarification or add additional context in comments.

1 Comment

I think you better make it manually to reset the password

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.