Skip to content

UserTasks delete handler catches wrong exception type, causing 500 errors #202

@amaydixit11

Description

@amaydixit11

File: api/leaderboard/views.py

Line: 657

Problem:

The delete method on UserTasksManage catches UserTasks.DoesNotExist instead of User.DoesNotExist when trying to look up the user:

# Line 655-660
def delete(self, request, *args, **kwargs):
    try:
        user = User.objects.get(username=request.data["username"])
    except UserTasks.DoesNotExist:  # WRONG: should be User.DoesNotExist
        return Response(
            {"error": "User Not Found"}, status=status.HTTP_404_NOT_FOUND
        )

If the user doesn't exist, a User.DoesNotExist exception is raised but NOT caught, causing an unhandled 500 error. The caught exception type (UserTasks.DoesNotExist) never occurs at this location.

Impact: Deleting tasks for non-existent users crashes the API with a 500 server error instead of returning a proper 404 response.

Fix: Change line 657 to except User.DoesNotExist:

Metadata

Metadata

Assignees

No one assigned

    Labels

    backendDjango backend related tasksbugbug d73a4a 'Something isn't working'🙏🏻 help wantedExtra attention is needed🚀 enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions