{"record":{"id":"254c3cb501edf07a","repo":"apache/superset","slug":"user-doesn-t-exist","errorCode":null,"errorMessage":"User doesn't exist","messagePattern":"User doesn't exist","errorType":"exception","errorClass":"MissingUserContextException","httpStatus":422,"severity":"error","filePath":"superset/daos/tag.py","lineNumber":275,"sourceCode":"\n        tag_ids = [tag.id for tag in tags]\n        return TagDAO.get_tagged_objects_by_tag_ids(tag_ids, obj_types)\n\n    @staticmethod\n    def favorite_tag_by_id_for_current_user(  # pylint: disable=invalid-name\n        tag_id: int,\n    ) -> None:\n        \"\"\"\n        Marks a specific tag as a favorite for the current user.\n\n        :param tag_id: The id of the tag that is to be marked as favorite\n        \"\"\"\n\n        tag = TagDAO.find_by_id(tag_id)\n        user = g.user\n\n        if not user:\n            raise MissingUserContextException(message=\"User doesn't exist\")\n        if not tag:\n            raise TagNotFoundError()\n\n        tag.users_favorited.append(user)\n\n    @staticmethod\n    def remove_user_favorite_tag(tag_id: int) -> None:\n        \"\"\"\n        Removes a tag from the current user's favorite tags.\n\n        :param tag_id: The id of the tag that is to be removed from the favorite tags\n        \"\"\"\n        tag = TagDAO.find_by_id(tag_id)\n        user = g.user\n\n        if not user:\n            raise MissingUserContextException(message=\"User doesn't exist\")\n        if not tag:","sourceCodeStart":257,"sourceCodeEnd":293,"githubUrl":"https://github.com/apache/superset/blob/f4587218dd19d046c3e4d00063e7d27f8a2ed354/superset/daos/tag.py#L257-L293","documentation":"Raised by TagDAO.add_user_favorite_tag when g.user is falsy — i.e. there is no authenticated user bound to the Flask request context. The favorite-tag feature is inherently per-user (it appends the user to tag.users_favorited), so an anonymous or missing user context is a hard error (MissingUserContextException, status 422). Note this is a context error, not an authentication failure: the request reached DAO code without a user attached.","triggerScenarios":"Calling the tag-favorite API/DAO from a background job or script with no request context; an anonymous session hitting an endpoint whose auth decorator was loosened; test code invoking the DAO without logging a user in; misconfigured auth middleware leaving g.user as an AnonymousUser that evaluates falsy.","commonSituations":"Unit/integration tests forgetting g.user setup or the login step; custom celery tasks that call DAO methods designed for request scope; embedded/anonymous deployments where guest access bypasses normal auth.","solutions":["Ensure the request is authenticated before calling favorite-tag endpoints (standard FAB login/session or token).","In tests, set g.user (e.g. flask_appbuilder security login or g.user = admin_user fixture) before invoking the DAO.","Do not call add_user_favorite_tag from non-request contexts; if needed there, pass the user explicitly via a refactored API.","Check authentication middleware ordering if g.user is unexpectedly unset in real requests."],"exampleFix":"# before (test/job context)\nTagDAO.add_user_favorite_tag(tag_id)  # g.user is None -> MissingUserContextException 422\n\n# after\nfrom flask import g\nwith app.test_request_context():\n    g.user = admin_user  # or login via test client first\n    TagDAO.add_user_favorite_tag(tag_id)","handlingStrategy":"validation","validationCode":"from flask import g\n\ndef has_user_context() -> bool:\n    user = getattr(g, 'user', None)\n    return bool(user) and not getattr(user, 'is_anonymous', False)","typeGuard":null,"tryCatchPattern":"from superset.exceptions import MissingUserContextException\ntry:\n    TagDAO.add_user_favorite_tag(tag_id)\nexcept MissingUserContextException:\n    redirect_to_login()  # re-authenticate; do not retry the mutation","preventionTips":["Protect favorite endpoints with authentication decorators so DAO code always runs with g.user bound.","In tests, set g.user or log in via the test client before invoking user-scoped DAOs.","Never call user-scoped DAOs from celery/background contexts."],"tags":["tags","favorites","auth-context","flask","testing"],"backgroundTag":null,"analyzedSha":"f4587218dd19d046c3e4d00063e7d27f8a2ed354","analyzedAt":"2026-08-14T22:39:27.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}