apache/superset · error · MissingUserContextException

User doesn't exist

Error message

User doesn't exist

What it means

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.

Source

Thrown at superset/daos/tag.py:275

        tag_ids = [tag.id for tag in tags]
        return TagDAO.get_tagged_objects_by_tag_ids(tag_ids, obj_types)

    @staticmethod
    def favorite_tag_by_id_for_current_user(  # pylint: disable=invalid-name
        tag_id: int,
    ) -> None:
        """
        Marks a specific tag as a favorite for the current user.

        :param tag_id: The id of the tag that is to be marked as favorite
        """

        tag = TagDAO.find_by_id(tag_id)
        user = g.user

        if not user:
            raise MissingUserContextException(message="User doesn't exist")
        if not tag:
            raise TagNotFoundError()

        tag.users_favorited.append(user)

    @staticmethod
    def remove_user_favorite_tag(tag_id: int) -> None:
        """
        Removes a tag from the current user's favorite tags.

        :param tag_id: The id of the tag that is to be removed from the favorite tags
        """
        tag = TagDAO.find_by_id(tag_id)
        user = g.user

        if not user:
            raise MissingUserContextException(message="User doesn't exist")
        if not tag:

View on GitHub (pinned to f4587218dd)

Solutions

  1. Ensure the request is authenticated before calling favorite-tag endpoints (standard FAB login/session or token).
  2. In tests, set g.user (e.g. flask_appbuilder security login or g.user = admin_user fixture) before invoking the DAO.
  3. Do not call add_user_favorite_tag from non-request contexts; if needed there, pass the user explicitly via a refactored API.
  4. Check authentication middleware ordering if g.user is unexpectedly unset in real requests.

Example fix

# before (test/job context)
TagDAO.add_user_favorite_tag(tag_id)  # g.user is None -> MissingUserContextException 422

# after
from flask import g
with app.test_request_context():
    g.user = admin_user  # or login via test client first
    TagDAO.add_user_favorite_tag(tag_id)
Defensive patterns

Strategy: validation

Validate before calling

from flask import g

def has_user_context() -> bool:
    user = getattr(g, 'user', None)
    return bool(user) and not getattr(user, 'is_anonymous', False)

Try / catch

from superset.exceptions import MissingUserContextException
try:
    TagDAO.add_user_favorite_tag(tag_id)
except MissingUserContextException:
    redirect_to_login()  # re-authenticate; do not retry the mutation

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of apache/superset@f4587218dd (2026-08-14). Data as JSON: /api/errors/254c3cb501edf07a. Report an issue: GitHub.