Http ошибки django

status.py

418 I’m a teapot — Any attempt to brew coffee with a teapot should result in the error code «418 I’m a teapot». The resulting entity body MAY be short and stout.

— RFC 2324, Hyper Text Coffee Pot Control Protocol

Using bare status codes in your responses isn’t recommended. REST framework includes a set of named constants that you can use to make your code more obvious and readable.

from rest_framework import status
from rest_framework.response import Response

def empty_view(self):
    content = {'please move along': 'nothing to see here'}
    return Response(content, status=status.HTTP_404_NOT_FOUND)

The full set of HTTP status codes included in the status module is listed below.

The module also includes a set of helper functions for testing if a status code is in a given range.

from rest_framework import status
from rest_framework.test import APITestCase

class ExampleTestCase(APITestCase):
    def test_url_root(self):
        url = reverse('index')
        response = self.client.get(url)
        self.assertTrue(status.is_success(response.status_code))

For more information on proper usage of HTTP status codes see RFC 2616
and RFC 6585.

Informational — 1xx

This class of status code indicates a provisional response. There are no 1xx status codes used in REST framework by default.

HTTP_100_CONTINUE
HTTP_101_SWITCHING_PROTOCOLS

Successful — 2xx

This class of status code indicates that the client’s request was successfully received, understood, and accepted.

HTTP_200_OK
HTTP_201_CREATED
HTTP_202_ACCEPTED
HTTP_203_NON_AUTHORITATIVE_INFORMATION
HTTP_204_NO_CONTENT
HTTP_205_RESET_CONTENT
HTTP_206_PARTIAL_CONTENT
HTTP_207_MULTI_STATUS
HTTP_208_ALREADY_REPORTED
HTTP_226_IM_USED

Redirection — 3xx

This class of status code indicates that further action needs to be taken by the user agent in order to fulfill the request.

HTTP_300_MULTIPLE_CHOICES
HTTP_301_MOVED_PERMANENTLY
HTTP_302_FOUND
HTTP_303_SEE_OTHER
HTTP_304_NOT_MODIFIED
HTTP_305_USE_PROXY
HTTP_306_RESERVED
HTTP_307_TEMPORARY_REDIRECT
HTTP_308_PERMANENT_REDIRECT

Client Error — 4xx

The 4xx class of status code is intended for cases in which the client seems to have erred. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition.

HTTP_400_BAD_REQUEST
HTTP_401_UNAUTHORIZED
HTTP_402_PAYMENT_REQUIRED
HTTP_403_FORBIDDEN
HTTP_404_NOT_FOUND
HTTP_405_METHOD_NOT_ALLOWED
HTTP_406_NOT_ACCEPTABLE
HTTP_407_PROXY_AUTHENTICATION_REQUIRED
HTTP_408_REQUEST_TIMEOUT
HTTP_409_CONFLICT
HTTP_410_GONE
HTTP_411_LENGTH_REQUIRED
HTTP_412_PRECONDITION_FAILED
HTTP_413_REQUEST_ENTITY_TOO_LARGE
HTTP_414_REQUEST_URI_TOO_LONG
HTTP_415_UNSUPPORTED_MEDIA_TYPE
HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE
HTTP_417_EXPECTATION_FAILED
HTTP_422_UNPROCESSABLE_ENTITY
HTTP_423_LOCKED
HTTP_424_FAILED_DEPENDENCY
HTTP_426_UPGRADE_REQUIRED
HTTP_428_PRECONDITION_REQUIRED
HTTP_429_TOO_MANY_REQUESTS
HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE
HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS

Server Error — 5xx

Response status codes beginning with the digit «5» indicate cases in which the server is aware that it has erred or is incapable of performing the request. Except when responding to a HEAD request, the server SHOULD include an entity containing an explanation of the error situation, and whether it is a temporary or permanent condition.

HTTP_500_INTERNAL_SERVER_ERROR
HTTP_501_NOT_IMPLEMENTED
HTTP_502_BAD_GATEWAY
HTTP_503_SERVICE_UNAVAILABLE
HTTP_504_GATEWAY_TIMEOUT
HTTP_505_HTTP_VERSION_NOT_SUPPORTED
HTTP_506_VARIANT_ALSO_NEGOTIATES
HTTP_507_INSUFFICIENT_STORAGE
HTTP_508_LOOP_DETECTED
HTTP_509_BANDWIDTH_LIMIT_EXCEEDED
HTTP_510_NOT_EXTENDED
HTTP_511_NETWORK_AUTHENTICATION_REQUIRED

Helper functions

The following helper functions are available for identifying the category of the response code.

is_informational()  # 1xx
is_success()        # 2xx
is_redirect()       # 3xx
is_client_error()   # 4xx
is_server_error()   # 5xx

Django Exceptions¶

Django raises some of its own exceptions as well as standard Python exceptions.

Django Core Exceptions¶

Django core exception classes are defined in django.core.exceptions.

AppRegistryNotReady

exception AppRegistryNotReady[source]

This exception is raised when attempting to use models before the app
loading process
, which initializes the ORM, is
complete.

EmptyResultSet

exception EmptyResultSet[source]

EmptyResultSet may be raised during query generation if a query won’t
return any results. Most Django projects won’t encounter this exception,
but it might be useful for implementing custom lookups and expressions.

FullResultSet

exception FullResultSet[source]

New in Django 4.2:

FullResultSet may be raised during query generation if a query will
match everything. Most Django projects won’t encounter this exception, but
it might be useful for implementing custom lookups and expressions.

FieldDoesNotExist

exception FieldDoesNotExist[source]

The FieldDoesNotExist exception is raised by a model’s
_meta.get_field() method when the requested field does not exist on the
model or on the model’s parents.

SuspiciousOperation

exception SuspiciousOperation[source]

The SuspiciousOperation exception is raised when a user has
performed an operation that should be considered suspicious from a security
perspective, such as tampering with a session cookie. Subclasses of
SuspiciousOperation include:

  • DisallowedHost
  • DisallowedModelAdminLookup
  • DisallowedModelAdminToField
  • DisallowedRedirect
  • InvalidSessionKey
  • RequestDataTooBig
  • SuspiciousFileOperation
  • SuspiciousMultipartForm
  • SuspiciousSession
  • TooManyFieldsSent
  • TooManyFilesSent

If a SuspiciousOperation exception reaches the ASGI/WSGI handler level
it is logged at the Error level and results in
a HttpResponseBadRequest. See the logging
documentation
for more information.

Changed in Django 3.2.18:

SuspiciousOperation is raised when too many files are submitted.

PermissionDenied

exception PermissionDenied[source]

The PermissionDenied exception is raised when a user does not have
permission to perform the action requested.

ViewDoesNotExist

exception ViewDoesNotExist[source]

The ViewDoesNotExist exception is raised by
django.urls when a requested view does not exist.

MiddlewareNotUsed

exception MiddlewareNotUsed[source]

The MiddlewareNotUsed exception is raised when a middleware is not
used in the server configuration.

ImproperlyConfigured

exception ImproperlyConfigured[source]

The ImproperlyConfigured exception is raised when Django is
somehow improperly configured – for example, if a value in settings.py
is incorrect or unparseable.

FieldError

exception FieldError[source]

The FieldError exception is raised when there is a problem with a
model field. This can happen for several reasons:

  • A field in a model clashes with a field of the same name from an
    abstract base class
  • An infinite loop is caused by ordering
  • A keyword cannot be parsed from the filter parameters
  • A field cannot be determined from a keyword in the query
    parameters
  • A join is not permitted on the specified field
  • A field name is invalid
  • A query contains invalid order_by arguments

ValidationError

exception ValidationError[source]

The ValidationError exception is raised when data fails form or
model field validation. For more information about validation, see
Form and Field Validation,
Model Field Validation and the
Validator Reference.

NON_FIELD_ERRORS

NON_FIELD_ERRORS

ValidationErrors that don’t belong to a particular field in a form
or model are classified as NON_FIELD_ERRORS. This constant is used
as a key in dictionaries that otherwise map fields to their respective
list of errors.

BadRequest

exception BadRequest[source]

The BadRequest exception is raised when the request cannot be
processed due to a client error. If a BadRequest exception reaches the
ASGI/WSGI handler level it results in a
HttpResponseBadRequest.

RequestAborted

exception RequestAborted[source]

The RequestAborted exception is raised when an HTTP body being read
in by the handler is cut off midstream and the client connection closes,
or when the client does not send data and hits a timeout where the server
closes the connection.

It is internal to the HTTP handler modules and you are unlikely to see
it elsewhere. If you are modifying HTTP handling code, you should raise
this when you encounter an aborted request to make sure the socket is
closed cleanly.

SynchronousOnlyOperation

exception SynchronousOnlyOperation[source]

The SynchronousOnlyOperation exception is raised when code that
is only allowed in synchronous Python code is called from an asynchronous
context (a thread with a running asynchronous event loop). These parts of
Django are generally heavily reliant on thread-safety to function and don’t
work correctly under coroutines sharing the same thread.

If you are trying to call code that is synchronous-only from an
asynchronous thread, then create a synchronous thread and call it in that.
You can accomplish this is with asgiref.sync.sync_to_async().

URL Resolver exceptions¶

URL Resolver exceptions are defined in django.urls.

Resolver404

exception Resolver404

The Resolver404 exception is raised by
resolve() if the path passed to resolve() doesn’t
map to a view. It’s a subclass of django.http.Http404.

NoReverseMatch

exception NoReverseMatch

The NoReverseMatch exception is raised by django.urls when a
matching URL in your URLconf cannot be identified based on the parameters
supplied.

Database Exceptions¶

Database exceptions may be imported from django.db.

Django wraps the standard database exceptions so that your Django code has a
guaranteed common implementation of these classes.

exception Error
exception InterfaceError
exception DatabaseError
exception DataError
exception OperationalError
exception IntegrityError
exception InternalError
exception ProgrammingError
exception NotSupportedError

The Django wrappers for database exceptions behave exactly the same as
the underlying database exceptions. See PEP 249, the Python Database API
Specification v2.0, for further information.

As per PEP 3134, a __cause__ attribute is set with the original
(underlying) database exception, allowing access to any additional
information provided.

exception models.ProtectedError

Raised to prevent deletion of referenced objects when using
django.db.models.PROTECT. models.ProtectedError is a subclass
of IntegrityError.

exception models.RestrictedError

Raised to prevent deletion of referenced objects when using
django.db.models.RESTRICT. models.RestrictedError is a subclass
of IntegrityError.

HTTP Exceptions¶

HTTP exceptions may be imported from django.http.

UnreadablePostError

exception UnreadablePostError

UnreadablePostError is raised when a user cancels an upload.

Sessions Exceptions¶

Sessions exceptions are defined in django.contrib.sessions.exceptions.

SessionInterrupted

exception SessionInterrupted[source]

SessionInterrupted is raised when a session is destroyed in a
concurrent request. It’s a subclass of
BadRequest.

Transaction Exceptions¶

Transaction exceptions are defined in django.db.transaction.

TransactionManagementError

exception TransactionManagementError

TransactionManagementError is raised for any and all problems
related to database transactions.

Testing Framework Exceptions¶

Exceptions provided by the django.test package.

RedirectCycleError

exception client.RedirectCycleError

RedirectCycleError is raised when the test client detects a
loop or an overly long chain of redirects.

Python Exceptions¶

Django raises built-in Python exceptions when appropriate as well. See the
Python documentation for further information on the Built-in Exceptions.

Django Exceptions¶

Django raises some of its own exceptions as well as standard Python exceptions.

Django Core Exceptions¶

Django core exception classes are defined in django.core.exceptions.

AppRegistryNotReady

exception AppRegistryNotReady[source]

This exception is raised when attempting to use models before the app
loading process
, which initializes the ORM, is
complete.

ObjectDoesNotExist

exception ObjectDoesNotExist[source]

The base class for Model.DoesNotExist exceptions. A try/except for
ObjectDoesNotExist will catch
DoesNotExist exceptions for all models.

See get().

EmptyResultSet

exception EmptyResultSet[source]

EmptyResultSet may be raised during query generation if a query won’t
return any results. Most Django projects won’t encounter this exception,
but it might be useful for implementing custom lookups and expressions.

FullResultSet

exception FullResultSet[source]

FullResultSet may be raised during query generation if a query will
match everything. Most Django projects won’t encounter this exception, but
it might be useful for implementing custom lookups and expressions.

FieldDoesNotExist

exception FieldDoesNotExist[source]

The FieldDoesNotExist exception is raised by a model’s
_meta.get_field() method when the requested field does not exist on the
model or on the model’s parents.

MultipleObjectsReturned

exception MultipleObjectsReturned[source]

The base class for Model.MultipleObjectsReturned exceptions. A
try/except for MultipleObjectsReturned will catch
MultipleObjectsReturned exceptions for all
models.

See get().

SuspiciousOperation

exception SuspiciousOperation[source]

The SuspiciousOperation exception is raised when a user has
performed an operation that should be considered suspicious from a security
perspective, such as tampering with a session cookie. Subclasses of
SuspiciousOperation include:

  • DisallowedHost

  • DisallowedModelAdminLookup

  • DisallowedModelAdminToField

  • DisallowedRedirect

  • InvalidSessionKey

  • RequestDataTooBig

  • SuspiciousFileOperation

  • SuspiciousMultipartForm

  • SuspiciousSession

  • TooManyFieldsSent

  • TooManyFilesSent

If a SuspiciousOperation exception reaches the ASGI/WSGI handler level
it is logged at the Error level and results in
a HttpResponseBadRequest. See the logging
documentation
for more information.

SuspiciousOperation is raised when too many files are submitted.

PermissionDenied

exception PermissionDenied[source]

The PermissionDenied exception is raised when a user does not have
permission to perform the action requested.

ViewDoesNotExist

exception ViewDoesNotExist[source]

The ViewDoesNotExist exception is raised by
django.urls when a requested view does not exist.

MiddlewareNotUsed

exception MiddlewareNotUsed[source]

The MiddlewareNotUsed exception is raised when a middleware is not
used in the server configuration.

ImproperlyConfigured

exception ImproperlyConfigured[source]

The ImproperlyConfigured exception is raised when Django is
somehow improperly configured – for example, if a value in settings.py
is incorrect or unparseable.

FieldError

exception FieldError[source]

The FieldError exception is raised when there is a problem with a
model field. This can happen for several reasons:

  • A field in a model clashes with a field of the same name from an
    abstract base class

  • An infinite loop is caused by ordering

  • A keyword cannot be parsed from the filter parameters

  • A field cannot be determined from a keyword in the query
    parameters

  • A join is not permitted on the specified field

  • A field name is invalid

  • A query contains invalid order_by arguments

ValidationError

exception ValidationError[source]

The ValidationError exception is raised when data fails form or
model field validation. For more information about validation, see
Form and Field Validation,
Model Field Validation and the
Validator Reference.

NON_FIELD_ERRORS

NON_FIELD_ERRORS

ValidationErrors that don’t belong to a particular field in a form
or model are classified as NON_FIELD_ERRORS. This constant is used
as a key in dictionaries that otherwise map fields to their respective
list of errors.

BadRequest

exception BadRequest[source]

The BadRequest exception is raised when the request cannot be
processed due to a client error. If a BadRequest exception reaches the
ASGI/WSGI handler level it results in a
HttpResponseBadRequest.

RequestAborted

exception RequestAborted[source]

The RequestAborted exception is raised when an HTTP body being read
in by the handler is cut off midstream and the client connection closes,
or when the client does not send data and hits a timeout where the server
closes the connection.

It is internal to the HTTP handler modules and you are unlikely to see
it elsewhere. If you are modifying HTTP handling code, you should raise
this when you encounter an aborted request to make sure the socket is
closed cleanly.

SynchronousOnlyOperation

exception SynchronousOnlyOperation[source]

The SynchronousOnlyOperation exception is raised when code that
is only allowed in synchronous Python code is called from an asynchronous
context (a thread with a running asynchronous event loop). These parts of
Django are generally heavily reliant on thread-safety to function and don’t
work correctly under coroutines sharing the same thread.

If you are trying to call code that is synchronous-only from an
asynchronous thread, then create a synchronous thread and call it in that.
You can accomplish this is with asgiref.sync.sync_to_async().

URL Resolver exceptions¶

URL Resolver exceptions are defined in django.urls.

Resolver404

exception Resolver404

The Resolver404 exception is raised by
resolve() if the path passed to resolve() doesn’t
map to a view. It’s a subclass of django.http.Http404.

NoReverseMatch

exception NoReverseMatch

The NoReverseMatch exception is raised by django.urls when a
matching URL in your URLconf cannot be identified based on the parameters
supplied.

Database Exceptions¶

Database exceptions may be imported from django.db.

Django wraps the standard database exceptions so that your Django code has a
guaranteed common implementation of these classes.

exception Error
exception InterfaceError
exception DatabaseError
exception DataError
exception OperationalError
exception IntegrityError
exception InternalError
exception ProgrammingError
exception NotSupportedError

The Django wrappers for database exceptions behave exactly the same as
the underlying database exceptions. See PEP 249, the Python Database API
Specification v2.0, for further information.

As per PEP 3134, a __cause__ attribute is set with the original
(underlying) database exception, allowing access to any additional
information provided.

exception models.ProtectedError

Raised to prevent deletion of referenced objects when using
django.db.models.PROTECT. models.ProtectedError is a subclass
of IntegrityError.

exception models.RestrictedError

Raised to prevent deletion of referenced objects when using
django.db.models.RESTRICT. models.RestrictedError is a subclass
of IntegrityError.

HTTP Exceptions¶

HTTP exceptions may be imported from django.http.

UnreadablePostError

exception UnreadablePostError

UnreadablePostError is raised when a user cancels an upload.

Sessions Exceptions¶

Sessions exceptions are defined in django.contrib.sessions.exceptions.

SessionInterrupted

exception SessionInterrupted[source]

SessionInterrupted is raised when a session is destroyed in a
concurrent request. It’s a subclass of
BadRequest.

Transaction Exceptions¶

Transaction exceptions are defined in django.db.transaction.

TransactionManagementError

exception TransactionManagementError

TransactionManagementError is raised for any and all problems
related to database transactions.

Testing Framework Exceptions¶

Exceptions provided by the django.test package.

RedirectCycleError

exception client.RedirectCycleError

RedirectCycleError is raised when the test client detects a
loop or an overly long chain of redirects.

Python Exceptions¶

Django raises built-in Python exceptions when appropriate as well. See the
Python documentation for further information on the Built-in Exceptions.

HTTP_402_PAYMENT_REQUIRED

HTTP_405_METHOD_NOT_ALLOWED

HTTP_407_PROXY_AUTHENTICATION_REQUIRED

HTTP_412_PRECONDITION_FAILED

HTTP_413_REQUEST_ENTITY_TOO_LARGE

HTTP_414_REQUEST_URI_TOO_LONG

HTTP_415_UNSUPPORTED_MEDIA_TYPE

HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE

HTTP_417_EXPECTATION_FAILED

HTTP_421_MISDIRECTED_REQUEST

HTTP_422_UNPROCESSABLE_ENTITY

HTTP_424_FAILED_DEPENDENCY

HTTP_426_UPGRADE_REQUIRED

HTTP_428_PRECONDITION_REQUIRED

HTTP_429_TOO_MANY_REQUESTS

HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE

HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS

Исключения¶

Исключения… позволяют чисто организовать обработку ошибок в центральном или высокоуровневом месте в структуре программы.

‒ Doug Hellmann, Python Exception Handling Techniques

Представления фреймворка REST обрабатывают различные исключения и возвращают соответствующие ответы на ошибки.

Обрабатываемыми исключениями являются:

  • Подклассы APIException, поднятые внутри фреймворка REST.

  • Исключение Django Http404.

  • Исключение Django PermissionDenied.

В каждом случае фреймворк REST возвращает ответ с соответствующим кодом состояния и типом содержимого. В теле ответа будут содержаться любые дополнительные сведения о характере ошибки.

Большинство ответов на ошибки будут содержать ключ detail в теле ответа.

Например, следующий запрос:

DELETE http://api.example.com/foo/bar HTTP/1.1
Accept: application/json

Может быть получен ответ об ошибке, указывающий на то, что метод DELETE не разрешен на данном ресурсе:

HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 42

{"detail": "Method 'DELETE' not allowed."}

Ошибки валидации обрабатываются несколько иначе, и в качестве ключей в ответе будут указаны имена полей. Если ошибка валидации не относится к конкретному полю, то будет использован ключ «non_field_errors» или любое строковое значение, установленное для параметра NON_FIELD_ERRORS_KEY.

Пример ошибки валидации может выглядеть следующим образом:

HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 94

{"amount": ["A valid integer is required."], "description": ["This field may not be blank."]}

Пользовательская обработка исключений¶

Вы можете реализовать пользовательскую обработку исключений, создав функцию-обработчик, которая преобразует исключения, возникающие в представлениях вашего API, в объекты ответа. Это позволит вам контролировать стиль ответов на ошибки, используемый вашим API.

Функция должна принимать пару аргументов, первый из которых — обрабатываемое исключение, а второй — словарь, содержащий любой дополнительный контекст, например, обрабатываемое в данный момент представление. Функция обработчика исключения должна либо вернуть объект Response, либо вернуть None, если исключение не может быть обработано. Если обработчик возвращает None, то исключение будет повторно поднято и Django вернет стандартный ответ HTTP 500 „server error“.

Например, вы можете захотеть убедиться, что все ответы на ошибки включают код состояния HTTP в теле ответа, например, так:

HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 62

{"status_code": 405, "detail": "Method 'DELETE' not allowed."}

Чтобы изменить стиль ответа, вы можете написать следующий пользовательский обработчик исключений:

from rest_framework.views import exception_handler

def custom_exception_handler(exc, context):
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)

    # Now add the HTTP status code to the response.
    if response is not None:
        response.data['status_code'] = response.status_code

    return response

Аргумент context не используется обработчиком по умолчанию, но может быть полезен, если обработчику исключения нужна дополнительная информация, например, обрабатываемое в данный момент представление, доступ к которому можно получить в виде context['view'].

Обработчик исключений также должен быть настроен в ваших настройках, используя клавишу настройки EXCEPTION_HANDLER. Например:

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}

Если параметр 'EXCEPTION_HANDLER' не указан, то по умолчанию используется стандартный обработчик исключений, предоставляемый фреймворком REST:

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

Обратите внимание, что обработчик исключений будет вызываться только для ответов, сгенерированных поднятыми исключениями. Он не будет использоваться для любых ответов, возвращаемых непосредственно представлением, например, ответов HTTP_400_BAD_REQUEST, которые возвращаются общими представлениями при неудачной проверке сериализатора.


Справочник по API¶

APIException¶

Подпись: APIException()

базовый класс для всех исключений, возникающих внутри класса APIView или @api_view.

Чтобы обеспечить пользовательское исключение, подкласс APIException и установите атрибуты .status_code , .default_detail , и default_code на класс.

Например, если ваш API полагается на сторонний сервис, который иногда может быть недоступен, вы можете захотеть реализовать исключение для кода ответа HTTP «503 Service Unavailable». Это можно сделать следующим образом:

from rest_framework.exceptions import APIException

class ServiceUnavailable(APIException):
    status_code = 503
    default_detail = 'Service temporarily unavailable, try again later.'
    default_code = 'service_unavailable'

Проверка исключений API¶

Существует ряд различных свойств, доступных для проверки состояния исключения API. Вы можете использовать их для создания пользовательской обработки исключений в вашем проекте.

Доступными атрибутами и методами являются:

  • .detail — Возвращает текстовое описание ошибки.

  • .get_codes() — Возвращает идентификатор кода ошибки.

  • .get_full_details() — Возвращает как текстовое описание, так и идентификатор кода.

В большинстве случаев деталь ошибки будет простым элементом:

>>> print(exc.detail)
You do not have permission to perform this action.
>>> print(exc.get_codes())
permission_denied
>>> print(exc.get_full_details())
{'message':'You do not have permission to perform this action.','code':'permission_denied'}

В случае ошибок валидации деталь ошибки будет представлять собой список или словарь элементов:

>>> print(exc.detail)
{"name":"This field is required.","age":"A valid integer is required."}
>>> print(exc.get_codes())
{"name":"required","age":"invalid"}
>>> print(exc.get_full_details())
{"name":{"message":"This field is required.","code":"required"},"age":{"message":"A valid integer is required.","code":"invalid"}}

ParseError¶

Подпись: ParseError(detail=None, code=None)

Возникает, если запрос содержит неправильно сформированные данные при доступе к request.data.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «400 Bad Request».

AuthenticationFailed¶

Подпись: AuthenticationFailed(detail=None, code=None)

Возникает, когда входящий запрос содержит неправильную аутентификацию.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «401 Unauthenticated», но оно также может привести к ответу «403 Forbidden», в зависимости от используемой схемы аутентификации. Более подробную информацию см. в authentication documentation.

NotAuthenticated¶

Подпись: NotAuthenticated(detail=None, code=None)

Возникает, когда неаутентифицированный запрос не прошел проверку на разрешение.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «401 Unauthenticated», но оно также может привести к ответу «403 Forbidden», в зависимости от используемой схемы аутентификации. Более подробную информацию см. в authentication documentation.

PermissionDenied¶

Подпись: PermissionDenied(detail=None, code=None)

Возникает, когда аутентифицированный запрос не прошел проверку на разрешение.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «403 Forbidden».

NotFound¶

Подпись: NotFound(detail=None, code=None)

Возникает, когда ресурс не существует по заданному URL. Это исключение эквивалентно стандартному исключению Django Http404.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «404 Not Found».

MethodNotAllowed¶

Подпись: MethodNotAllowed(method, detail=None, code=None)

Возникает, когда происходит входящий запрос, который не сопоставлен с методом-обработчиком на представлении.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «405 Method Not Allowed».

Неприемлемо¶

Подпись: NotAcceptable(detail=None, code=None)

Возникает, когда поступает запрос с заголовком Accept, который не может быть удовлетворен ни одним из доступных рендереров.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «406 Not Acceptable».

Дросселированный¶

Подпись: Throttled(wait=None, detail=None, code=None)

Возникает, когда входящий запрос не проходит проверку на дросселирование.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «429 Too Many Requests».

ValidationError¶

Подпись: ValidationError(detail, code=None)

Исключение ValidationError несколько отличается от других классов APIException:

  • Аргумент detail является обязательным, а не опциональным.

  • Аргумент detail может представлять собой список или словарь сведений об ошибках, а также может быть вложенной структурой данных. Используя словарь, вы можете указать ошибки на уровне полей при выполнении проверки на уровне объектов в методе validate() сериализатора. Например. raise serializers.ValidationError({'name': 'Please enter a valid name.'})

  • По соглашению вы должны импортировать модуль serializers и использовать полностью квалифицированный стиль ValidationError, чтобы отличить его от встроенной ошибки валидации Django. Например. raise serializers.ValidationError('This field must be an integer value.')

Класс ValidationError следует использовать для сериализатора и валидации полей, а также классами валидаторов. Он также возникает при вызове serializer.is_valid с аргументом ключевого слова raise_exception:

serializer.is_valid(raise_exception=True)

Общие представления используют флаг raise_exception=True, что означает, что вы можете переопределить стиль ответов на ошибки валидации глобально в вашем API. Для этого используйте пользовательский обработчик исключений, как описано выше.

По умолчанию это исключение приводит к ответу с кодом состояния HTTP «400 Bad Request».


Общие представления ошибок¶

Django REST Framework предоставляет два представления ошибок, подходящих для предоставления общих JSON 500 Server Error и 400 Bad Request ответов. (Стандартные представления ошибок Django предоставляют ответы в формате HTML, что может не подойти для приложения, использующего только API).

Используйте их в соответствии с Django’s Customizing error views documentation.

rest_framework.exceptions.server_error

Возвращает ответ с кодом состояния 500 и типом содержимого application/json.

Установить как handler500 :

handler500 = 'rest_framework.exceptions.server_error'

rest_framework.exceptions.bad_request

Возвращает ответ с кодом состояния 400 и типом содержимого application/json.

Установить как handler400 :

handler400 = 'rest_framework.exceptions.bad_request'

Вернуться на верх

Status Codes

418 Я заварочный чайник-любая попытка заварить кофе с заварочным чайником должна приводить к коду ошибки «418 Я заварочный чайник».Результирующее тело сущности ДОЛЖНО быть коротким и крепким.

— RFC 2324 , Протокол управления гипертекстовым кофейником.

Использование пустых кодов состояния в ответах не рекомендуется.Фреймворк REST включает набор именованных констант,которые вы можете использовать для того,чтобы сделать ваш код более понятным и читабельным.

from rest_framework import status
from rest_framework.response import Response

def empty_view(self):
    content = {'please move along': 'nothing to see here'}
    return Response(content, status=status.HTTP_404_NOT_FOUND)

Полный набор кодов состояния HTTP, включенных в модуль status приведен ниже.

Модуль также включает в себя набор вспомогательных функций для тестирования,если код состояния находится в заданном диапазоне.

from rest_framework import status
from rest_framework.test import APITestCase

class ExampleTestCase(APITestCase):
    def test_url_root(self):
        url = reverse('index')
        response = self.client.get(url)
        self.assertTrue(status.is_success(response.status_code))

Для получения дополнительной информации о правильном использовании кодов состояния HTTP см. RFC 2616 и RFC 6585 .

Информационный-1xx

Данный класс кода статуса указывает на предварительную реакцию.По умолчанию в структуре REST не используются коды статуса 1xx.

HTTP_100_CONTINUE
HTTP_101_SWITCHING_PROTOCOLS

Успешный-2хх

Данный класс кода статуса указывает на то,что запрос клиента был успешно принят,понят и принят.

HTTP_200_OK
HTTP_201_CREATED
HTTP_202_ACCEPTED
HTTP_203_NON_AUTHORITATIVE_INFORMATION
HTTP_204_NO_CONTENT
HTTP_205_RESET_CONTENT
HTTP_206_PARTIAL_CONTENT
HTTP_207_MULTI_STATUS
HTTP_208_ALREADY_REPORTED
HTTP_226_IM_USED

Переадресация-3xx

Данный класс кода статуса указывает на то,что для выполнения запроса необходимо дальнейшее действие агента пользователя.

HTTP_300_MULTIPLE_CHOICES
HTTP_301_MOVED_PERMANENTLY
HTTP_302_FOUND
HTTP_303_SEE_OTHER
HTTP_304_NOT_MODIFIED
HTTP_305_USE_PROXY
HTTP_306_RESERVED
HTTP_307_TEMPORARY_REDIRECT
HTTP_308_PERMANENT_REDIRECT

Ошибка клиента-4xx

Статусный код класса 4xx предназначен для случаев,в которых клиент,похоже,ошибся.За исключением ответа на запрос HEAD,сервер SHOULD включает в себя сущность,содержащую объяснение ситуации ошибки,и является ли она временным или постоянным состоянием.

HTTP_400_BAD_REQUEST
HTTP_401_UNAUTHORIZED
HTTP_402_PAYMENT_REQUIRED
HTTP_403_FORBIDDEN
HTTP_404_NOT_FOUND
HTTP_405_METHOD_NOT_ALLOWED
HTTP_406_NOT_ACCEPTABLE
HTTP_407_PROXY_AUTHENTICATION_REQUIRED
HTTP_408_REQUEST_TIMEOUT
HTTP_409_CONFLICT
HTTP_410_GONE
HTTP_411_LENGTH_REQUIRED
HTTP_412_PRECONDITION_FAILED
HTTP_413_REQUEST_ENTITY_TOO_LARGE
HTTP_414_REQUEST_URI_TOO_LONG
HTTP_415_UNSUPPORTED_MEDIA_TYPE
HTTP_416_REQUESTED_RANGE_NOT_SATISFIABLE
HTTP_417_EXPECTATION_FAILED
HTTP_422_UNPROCESSABLE_ENTITY
HTTP_423_LOCKED
HTTP_424_FAILED_DEPENDENCY
HTTP_426_UPGRADE_REQUIRED
HTTP_428_PRECONDITION_REQUIRED
HTTP_429_TOO_MANY_REQUESTS
HTTP_431_REQUEST_HEADER_FIELDS_TOO_LARGE
HTTP_451_UNAVAILABLE_FOR_LEGAL_REASONS

Ошибка сервера-5xx

Коды статуса ответа,начинающиеся с цифры «5»,указывают на случаи,когда сервер знает,что он ошибся или не может выполнить запрос.За исключением ответа на запрос HEAD,сервер ДОЛЖЕН включать сущность,содержащую объяснение ситуации ошибки,и является ли она временным или постоянным состоянием.

HTTP_500_INTERNAL_SERVER_ERROR
HTTP_501_NOT_IMPLEMENTED
HTTP_502_BAD_GATEWAY
HTTP_503_SERVICE_UNAVAILABLE
HTTP_504_GATEWAY_TIMEOUT
HTTP_505_HTTP_VERSION_NOT_SUPPORTED
HTTP_506_VARIANT_ALSO_NEGOTIATES
HTTP_507_INSUFFICIENT_STORAGE
HTTP_508_LOOP_DETECTED
HTTP_509_BANDWIDTH_LIMIT_EXCEEDED
HTTP_510_NOT_EXTENDED
HTTP_511_NETWORK_AUTHENTICATION_REQUIRED

Helper functions

Для определения категории кода ответа доступны следующие вспомогательные функции.

is_informational()  # 1xx
is_success()        # 2xx
is_redirect()       # 3xx
is_client_error()   # 4xx
is_server_error()   # 5xx

status.py


Django REST Framework

3.14

  • Расширенное использование сериализатора

    Если вам нужно изменить поведение сериализации или десериализации класса сериализатора,вы можете сделать это,переопределив .to_representation().to_internal_value()

  • Settings

    Пространства имен — отличная идея, давайте сделаем больше таких! Конфигурация Zen of Python для среды REST находится внутри одного пространства имен Django.

  • Testing

    Код без тестов взломан по назначению.

  • Throttling

    HTTP/1.1 420 Enhance Your Calm Twitter API rate limiting response Дросселирование похоже на разрешения,поскольку оно определяет,должен ли запрос быть авторизован.

source

exceptions.py

Exceptions… allow error handling to be organized cleanly in a central or high-level place within the program structure.

— Doug Hellmann, Python Exception Handling Techniques

Exception handling in REST framework views

REST framework’s views handle various exceptions, and deal with returning appropriate error responses.

The handled exceptions are:

  • Subclasses of APIException raised inside REST framework.
  • Django’s Http404 exception.
  • Django’s PermissionDenied exception.

In each case, REST framework will return a response with an appropriate status code and content-type. The body of the response will include any additional details regarding the nature of the error.

Most error responses will include a key detail in the body of the response.

For example, the following request:

DELETE http://api.example.com/foo/bar HTTP/1.1
Accept: application/json

Might receive an error response indicating that the DELETE method is not allowed on that resource:

HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 42

{"detail": "Method 'DELETE' not allowed."}

Validation errors are handled slightly differently, and will include the field names as the keys in the response. If the validation error was not specific to a particular field then it will use the «non_field_errors» key, or whatever string value has been set for the NON_FIELD_ERRORS_KEY setting.

An example validation error might look like this:

HTTP/1.1 400 Bad Request
Content-Type: application/json
Content-Length: 94

{"amount": ["A valid integer is required."], "description": ["This field may not be blank."]}

Custom exception handling

You can implement custom exception handling by creating a handler function that converts exceptions raised in your API views into response objects. This allows you to control the style of error responses used by your API.

The function must take a pair of arguments, the first is the exception to be handled, and the second is a dictionary containing any extra context such as the view currently being handled. The exception handler function should either return a Response object, or return None if the exception cannot be handled. If the handler returns None then the exception will be re-raised and Django will return a standard HTTP 500 ‘server error’ response.

For example, you might want to ensure that all error responses include the HTTP status code in the body of the response, like so:

HTTP/1.1 405 Method Not Allowed
Content-Type: application/json
Content-Length: 62

{"status_code": 405, "detail": "Method 'DELETE' not allowed."}

In order to alter the style of the response, you could write the following custom exception handler:

from rest_framework.views import exception_handler

def custom_exception_handler(exc, context):
    # Call REST framework's default exception handler first,
    # to get the standard error response.
    response = exception_handler(exc, context)

    # Now add the HTTP status code to the response.
    if response is not None:
        response.data['status_code'] = response.status_code

    return response

The context argument is not used by the default handler, but can be useful if the exception handler needs further information such as the view currently being handled, which can be accessed as context['view'].

The exception handler must also be configured in your settings, using the EXCEPTION_HANDLER setting key. For example:

REST_FRAMEWORK = {
    'EXCEPTION_HANDLER': 'my_project.my_app.utils.custom_exception_handler'
}

If not specified, the 'EXCEPTION_HANDLER' setting defaults to the standard exception handler provided by REST framework:

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

Note that the exception handler will only be called for responses generated by raised exceptions. It will not be used for any responses returned directly by the view, such as the HTTP_400_BAD_REQUEST responses that are returned by the generic views when serializer validation fails.


API Reference

APIException

Signature: APIException()

The base class for all exceptions raised inside an APIView class or @api_view.

To provide a custom exception, subclass APIException and set the .status_code, .default_detail, and default_code attributes on the class.

For example, if your API relies on a third party service that may sometimes be unreachable, you might want to implement an exception for the «503 Service Unavailable» HTTP response code. You could do this like so:

from rest_framework.exceptions import APIException

class ServiceUnavailable(APIException):
    status_code = 503
    default_detail = 'Service temporarily unavailable, try again later.'
    default_code = 'service_unavailable'

Inspecting API exceptions

There are a number of different properties available for inspecting the status
of an API exception. You can use these to build custom exception handling
for your project.

The available attributes and methods are:

  • .detail — Return the textual description of the error.
  • .get_codes() — Return the code identifier of the error.
  • .get_full_details() — Return both the textual description and the code identifier.

In most cases the error detail will be a simple item:

>>> print(exc.detail)
You do not have permission to perform this action.
>>> print(exc.get_codes())
permission_denied
>>> print(exc.get_full_details())
{'message':'You do not have permission to perform this action.','code':'permission_denied'}

In the case of validation errors the error detail will be either a list or
dictionary of items:

>>> print(exc.detail)
{"name":"This field is required.","age":"A valid integer is required."}
>>> print(exc.get_codes())
{"name":"required","age":"invalid"}
>>> print(exc.get_full_details())
{"name":{"message":"This field is required.","code":"required"},"age":{"message":"A valid integer is required.","code":"invalid"}}

ParseError

Signature: ParseError(detail=None, code=None)

Raised if the request contains malformed data when accessing request.data.

By default this exception results in a response with the HTTP status code «400 Bad Request».

AuthenticationFailed

Signature: AuthenticationFailed(detail=None, code=None)

Raised when an incoming request includes incorrect authentication.

By default this exception results in a response with the HTTP status code «401 Unauthenticated», but it may also result in a «403 Forbidden» response, depending on the authentication scheme in use. See the authentication documentation for more details.

NotAuthenticated

Signature: NotAuthenticated(detail=None, code=None)

Raised when an unauthenticated request fails the permission checks.

By default this exception results in a response with the HTTP status code «401 Unauthenticated», but it may also result in a «403 Forbidden» response, depending on the authentication scheme in use. See the authentication documentation for more details.

PermissionDenied

Signature: PermissionDenied(detail=None, code=None)

Raised when an authenticated request fails the permission checks.

By default this exception results in a response with the HTTP status code «403 Forbidden».

NotFound

Signature: NotFound(detail=None, code=None)

Raised when a resource does not exist at the given URL. This exception is equivalent to the standard Http404 Django exception.

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

MethodNotAllowed

Signature: MethodNotAllowed(method, detail=None, code=None)

Raised when an incoming request occurs that does not map to a handler method on the view.

By default this exception results in a response with the HTTP status code «405 Method Not Allowed».

NotAcceptable

Signature: NotAcceptable(detail=None, code=None)

Raised when an incoming request occurs with an Accept header that cannot be satisfied by any of the available renderers.

By default this exception results in a response with the HTTP status code «406 Not Acceptable».

UnsupportedMediaType

Signature: UnsupportedMediaType(media_type, detail=None, code=None)

Raised if there are no parsers that can handle the content type of the request data when accessing request.data.

By default this exception results in a response with the HTTP status code «415 Unsupported Media Type».

Throttled

Signature: Throttled(wait=None, detail=None, code=None)

Raised when an incoming request fails the throttling checks.

By default this exception results in a response with the HTTP status code «429 Too Many Requests».

ValidationError

Signature: ValidationError(detail=None, code=None)

The ValidationError exception is slightly different from the other APIException classes:

  • The detail argument may be a list or dictionary of error details, and may also be a nested data structure. By using a dictionary, you can specify field-level errors while performing object-level validation in the validate() method of a serializer. For example. raise serializers.ValidationError({'name': 'Please enter a valid name.'})
  • By convention you should import the serializers module and use a fully qualified ValidationError style, in order to differentiate it from Django’s built-in validation error. For example. raise serializers.ValidationError('This field must be an integer value.')

The ValidationError class should be used for serializer and field validation, and by validator classes. It is also raised when calling serializer.is_valid with the raise_exception keyword argument:

serializer.is_valid(raise_exception=True)

The generic views use the raise_exception=True flag, which means that you can override the style of validation error responses globally in your API. To do so, use a custom exception handler, as described above.

By default this exception results in a response with the HTTP status code «400 Bad Request».


Generic Error Views

Django REST Framework provides two error views suitable for providing generic JSON 500 Server Error and
400 Bad Request responses. (Django’s default error views provide HTML responses, which may not be appropriate for an
API-only application.)

Use these as per Django’s Customizing error views documentation.

rest_framework.exceptions.server_error

Returns a response with status code 500 and application/json content type.

Set as handler500:

handler500 = 'rest_framework.exceptions.server_error'

rest_framework.exceptions.bad_request

Returns a response with status code 400 and application/json content type.

Set as handler400:

handler400 = 'rest_framework.exceptions.bad_request'

Third party packages

The following third-party packages are also available.

DRF Standardized Errors

The drf-standardized-errors package provides an exception handler that generates the same format for all 4xx and 5xx responses. It is a drop-in replacement for the default exception handler and allows customizing the error response format without rewriting the whole exception handler. The standardized error response format is easier to document and easier to handle by API consumers.

Whenever one tries to visit a link that doesn’t exist on a website, it gives a 404 Error, that this page is not found. Similarly, there are more error codes such as 500, 403, etc. Django has some default functions to for handle HTTP errors. Let’s explore them one-by-one.

Built-in Error Views in Django – 

404 view (page not found) – 

Normally this view is used by to render basic 404 template when requested URL is not available on the server. This situation happens by default Django raises django.views.defaults.page_not_found() view and render default404 error template. You can customize it by adding custom 404.html page inside templates folder.

500 view (server error) – 

This view is used when the server has been crashed or didn’t respond to anything. In this case Django has default function django.views.defaults.server_error() to render server error for this view. You can also customize it by adding your 500.html page inside the templates folder.

403 view (Forbidden) –

When user does not have permission to view particular page but he/she requests to view that page. In that case 403 view comes into play. It says page is forbidden and will not show page to that user. For that Django has django.views.defaults.permission_denied() function to render forbidden template. It maintains your privacy and only permitted users can access certain pages. You can also customize it by adding your 403.html page inside the templates folder.

Suppose you have an ecommerce website and you only want authenticated merchants to list products. Buyers and normal users can’t list their products. You can add this functionality in Django like this:

from django.core.exceptions import PermissionDenied

def upload(request, pk):
    if not request.user.is_merchant:
        raise PermissionDenied
 
    
    # your code here.....
        

400 view (Bad Request):

The 400 view or Bad Request view is used by Django when someone try to access confidential page of your site which may lead your site to be hacked or leak confidential data. So, you don’t want  anyone to access that page. Django has django.views.defaults.bad_request() to raise 400 view for any kind of SuspiciousOperation. This prevents your site from Bad peoples.

Customize Built-in Error Views in Django – 

Now, let’s see how to customize these error views. First of all you need to go into settings.py and set Debug=False.

DEBUG = False
ALLOWED_HOSTS =  ['localhost', '127.0.0.1']

Make folder inside the project and name it anything, here I am giving name ‘templates‘ to that folder.  Now go to settings.py and set templates directory.

TEMPLATE_DIR = os.path.join(BASE_DIR, 'templates')

Now, inside the templates directory you can create html files ‘404.html’, ‘500.html’, ‘403.html’, ‘400.html’, etc. After creating these pages show your HTML skills and customize pages by yourself. Add your app name into settings.py.

INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'jquery',
'geeks'
]

Add the handler method to urls.py

handler404 = 'myappname.views.error_404'
handler500 = 'myappname.views.error_500'
handler403 = 'myappname.views.error_403'
handler400 = 'myappname.views.error_400'

Now set logic to show these pages in views.py

from django.shortcuts import render

def error_404(request, exception):

        return render(request,'404.html')

def error_500(request,  exception):
        return render(request,'500.html', data)
        
def error_403(request, exception):

        return render(request,'403.html')

def error_400(request,  exception):
        return render(request,'400.html', data)    

Now, you are all set to run the server and see these error handling pages made by you.

To run server: python manage.py runserver

Last Updated :
01 Nov, 2020

Like Article

Save Article

Понравилась статья? Поделить с друзьями:

Не пропустите эти материалы по теме:

  • Яндекс еда ошибка привязки карты
  • Huawei ошибка драйвера
  • Http ошибка при загрузке файла 413
  • Huawei код ошибки 13 сбой обновления huawei
  • Http ошибка 600

  • 0 0 голоса
    Рейтинг статьи
    Подписаться
    Уведомить о
    guest

    0 комментариев
    Старые
    Новые Популярные
    Межтекстовые Отзывы
    Посмотреть все комментарии