def handle_exception(e):
    """Return JSON instead of HTML for non-HTTP errors."""
    # pass through HTTP errors
    if isinstance(e, HTTPException):
        return e

    logger.exception(e)

    # now handling non-HTTP exceptions only
    resp = Response()
    resp.response = json.dumps(
        {
            "message": "internal server error",
            "detail": f"{type(e).__name__}: {e}",
        }
    )
    resp.status_code = 500
    resp.mimetype = "application/json"
    return resp
Return JSON instead of HTML for non-HTTP errors.
backend/director/entrypoint/api/errors.py