From e628d9eb9b7deac2ecffe23eace5c72709887f8f Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Fri, 6 Mar 2015 12:05:16 +0000 Subject: Update documentation --- api-guide/exceptions/index.html | 51 ++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 21 deletions(-) (limited to 'api-guide/exceptions/index.html') diff --git a/api-guide/exceptions/index.html b/api-guide/exceptions/index.html index f9403f1d..c0ddeb4f 100644 --- a/api-guide/exceptions/index.html +++ b/api-guide/exceptions/index.html @@ -188,6 +188,10 @@ Pagination +
  • + Versioning +
  • +
  • Content negotiation
  • @@ -231,6 +235,10 @@ Documenting your API +
  • + Internationalization +
  • +
  • AJAX, CSRF & CORS
  • @@ -260,23 +268,11 @@
  • - 2.0 Announcement -
  • - -
  • - 2.2 Announcement -
  • - -
  • - 2.3 Announcement -
  • - -
  • - 2.4 Announcement + 3.0 Announcement
  • - 3.0 Announcement + 3.1 Announcement
  • @@ -287,10 +283,6 @@ Release Notes
  • -
  • - Credits -
  • - @@ -393,10 +385,18 @@ PermissionDenied +
  • + NotFound +
  • +
  • MethodNotAllowed
  • +
  • + NotAcceptable +
  • +
  • UnsupportedMediaType
  • @@ -464,7 +464,7 @@ Content-Length: 94

    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 single argument, which is the exception to be handled, and 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.

    +

    The function must take a pair of arguments, this 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
    @@ -475,10 +475,10 @@ Content-Length: 62
     

    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):
    +def custom_exception_handler(exc, context):
         # Call REST framework's default exception handler first,
         # to get the standard error response.
    -    response = exception_handler(exc)
    +    response = exception_handler(exc, context)
     
         # Now add the HTTP status code to the response.
         if response is not None:
    @@ -486,6 +486,7 @@ def custom_exception_handler(exc):
     
         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'
    @@ -526,10 +527,18 @@ class ServiceUnavailable(APIException):
     

    Signature: PermissionDenied(detail=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)

    +

    Raised when a resource does not exists 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)

    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)

    +

    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)

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

    -- cgit v1.2.3