پرش به محتویات

exception handler

این صفحه از docstringهای خود کد ساخته می‌شود، که به انگلیسی‌اند.

handlers

The exception handler: every failure a DRF view raises, answered in the envelope.

REST_FRAMEWORK = {'EXCEPTION_HANDLER': 'drf_parcel.handlers.exception_handler'}

What it recognises, in order:

  1. Django's own Http404, PermissionDenied and ValidationError, turned into DRF's.
  2. A validation error - the first problem in message, every problem in data.fields, each with its code and a flat path.
  3. Any APIException - DRF's, the library's, a project's own: its status, its code as error_code, its detail as message, its data. DRF's own handler runs first, so the headers it adds - Retry-After, WWW-Authenticate - stay on the answer.
  4. An IntegrityError, when HANDLE_INTEGRITY_ERRORS is on: a unique or foreign-key constraint is a 409; every other is a bug.
  5. Anything else - a bug. A generic 500 whose text never leaves the server, logged with its traceback and reported. Under DEBUG it is raised instead, so Django's page shows it.

Every 5xx answered - a designed one too - is logged and reported, and every failure answered marks the request's transaction for rollback.

exception_handler

exception_handler(exc: Exception, context: Mapping[str, Any]) -> Response | None

Turn anything a DRF view raised into the envelope - or, under DEBUG, let a bug through.