Raising Exceptions in Django REST

felix001

I'm trying to test the exceptions within the django rest framework. Based on http://www.django-rest-framework.org/api-guide/exceptions/ raising NotFound,

By default this exception results in a response with the HTTP status code "404 Not Found".

However when I issue a GET (to /example1) I get a 500 with ,

Request Method: GET
Request URL:    http://192.168.59.103:8002/example1
Django Version: 1.8.3
Exception Type: NotFound
Exception Value:    
not found
Exception Location: /home/djangoapp/testtools/api/views.py in example1, line 7
Python Executable:  /usr/bin/python

details below,

settings.py

REST_FRAMEWORK = {'EXCEPTION_HANDLER':'rest_framework.views.exception_handler'}

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
)

urls.py

from django.conf.urls import include, url
from api import views

urlpatterns = [
    url(r'example1', views.example1),
]

views.py

from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from rest_framework.exceptions import APIException,ParseError,NotFound

def example1(request):                                                                                                                               
    raise NotFound("not found")

Any ideas ?

Arpit Goyal
from rest_framework.exceptions import NotFound

The class NotFound is extended from APIException. The default status code for APIException is 500 Internal Server Error, whereas for NotFound is 404.

So according to me here you are trying to throw a rest framework exception in a pure django view. I wrote a test case here to check what are the odds of getting the error raised. Guess what a simple django view trying to raise a rest framework exception is not recognized as an error to the view. But on providing the decorator and declaring it an API View it enters the exception_handler

So when you do:

from rest_framework.decorators import api_view

@api_view()
def example1(request):
    raise NotFound('not found')

This is now recognized as an exception thrown by the API which enters the default rest_framework EXCEPTION_HANDLER provided by you, where it returns the response for any given exception.

Its docstring says:

Returns the response that should be used for any given exception. By default we handle the REST framework APIException, and also Django's built-in ValidationError, Http404 and PermissionDenied exceptions. Any unhandled exceptions may return None, which will cause a 500 error to be raised.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to convert functions raising exceptions to functions returning Either?

From Dev

How to handle multiple requests without SQLAlchemy crashing/raising exceptions?

From Dev

Django model choice not raising error for an invalid choice

From Dev

validating email in registration is not raising validationError in Django?

From Dev

Ruby - Arrays, bounds, and raising exceptions

From Dev

Is it better for a method to allow raising multiple types of exceptions or just one?

From Dev

Raising exceptions in Python

From Dev

Django annotate raising AttributeError 'object has no attribute'

From Dev

Django data migration raising a Type Error

From Dev

RxJava and Retrofit - Raising custom exceptions depending on server response

From Dev

Django REST Exceptions

From Dev

Django Rest Framework's serializer.is_valid() raising validation errors even though required=false

From Dev

Raising Exceptions in Django REST

From Dev

Raising exceptions in Deferred callbacks

From Dev

Django REST Framework how to specify error code when raising validation error in serializer

From Dev

Raising non-Exception based exceptions in Python gives different results

From Dev

Applications of explicitly raising exceptions

From Dev

Why my Django form is not raising validation error?

From Dev

What is the point of re-raising exceptions?

From Dev

Raising multiple exceptions from threads in Python

From Dev

django rest framework has_object_permission raising PermissionDenied exception

From Dev

Using ErrorController without raising exceptions

From Dev

Expect Scripting - Raising Exceptions

From Dev

Raising exceptions in Deferred callbacks

From Dev

Raising non-Exception based exceptions in Python gives different results

From Dev

Applications of explicitly raising exceptions

From Dev

Django - Checking if objects exists and raising error if it does

From Dev

Django model field validator is not raising an Exception as it should

From Dev

How do I exit a for loop in python 2.7 without raising Exceptions

Related Related

  1. 1

    How to convert functions raising exceptions to functions returning Either?

  2. 2

    How to handle multiple requests without SQLAlchemy crashing/raising exceptions?

  3. 3

    Django model choice not raising error for an invalid choice

  4. 4

    validating email in registration is not raising validationError in Django?

  5. 5

    Ruby - Arrays, bounds, and raising exceptions

  6. 6

    Is it better for a method to allow raising multiple types of exceptions or just one?

  7. 7

    Raising exceptions in Python

  8. 8

    Django annotate raising AttributeError 'object has no attribute'

  9. 9

    Django data migration raising a Type Error

  10. 10

    RxJava and Retrofit - Raising custom exceptions depending on server response

  11. 11

    Django REST Exceptions

  12. 12

    Django Rest Framework's serializer.is_valid() raising validation errors even though required=false

  13. 13

    Raising Exceptions in Django REST

  14. 14

    Raising exceptions in Deferred callbacks

  15. 15

    Django REST Framework how to specify error code when raising validation error in serializer

  16. 16

    Raising non-Exception based exceptions in Python gives different results

  17. 17

    Applications of explicitly raising exceptions

  18. 18

    Why my Django form is not raising validation error?

  19. 19

    What is the point of re-raising exceptions?

  20. 20

    Raising multiple exceptions from threads in Python

  21. 21

    django rest framework has_object_permission raising PermissionDenied exception

  22. 22

    Using ErrorController without raising exceptions

  23. 23

    Expect Scripting - Raising Exceptions

  24. 24

    Raising exceptions in Deferred callbacks

  25. 25

    Raising non-Exception based exceptions in Python gives different results

  26. 26

    Applications of explicitly raising exceptions

  27. 27

    Django - Checking if objects exists and raising error if it does

  28. 28

    Django model field validator is not raising an Exception as it should

  29. 29

    How do I exit a for loop in python 2.7 without raising Exceptions

HotTag

Archive