Skip to content

exceptions#

TopLevelValidationError#

Bases: APIException

Validation error that is raised to top level instead of serializer trying to gather all errors.

Useful when same error output is desired outside serialization and e.g. in a nested serializer.

Source code in src/apps/common/exceptions.py
class TopLevelValidationError(APIException):
    """Validation error that is raised to top level instead of serializer trying to gather all errors.

    Useful when same error output is desired outside serialization and e.g. in a nested serializer.
    """

    status_code = status.HTTP_400_BAD_REQUEST

exception_handler#

Handler for DRF exceptions.

Source code in src/apps/common/exceptions.py
def exception_handler(exc, context):
    """Handler for DRF exceptions."""

    # Include error codes for authentication errors to
    # make it easier for other services to determine
    # cause of failure.
    response = default_handler(exc, context)

    # Now add the HTTP status code to the response.
    if response is not None and isinstance(exc, AuthenticationFailed):
        response.data["code"] = get_attr_or_item(exc.detail, "code")

    return response