ghamry03 / ft_transcendence

0 stars 0 forks source link

Tournament API bug #31

Closed MehrinFirdousi closed 6 months ago

MehrinFirdousi commented 7 months ago

Tour app crashes while fetching the tournament history when there are games in the DB that aren't associated with a tournament.

Steps to reproduce

Traceback

HTTP GET /api/tourhistory/93164/ 500 [0.38, 172.25.0.7:49974]
Internal Server Error: /api/tourhistory/88381/
Traceback (most recent call last):
  File "/usr/local/lib/python3.12/site-packages/asgiref/sync.py", line 534, in thread_handler
    raise exc_info[1]
  File "/usr/local/lib/python3.12/site-packages/django/core/handlers/exception.py", line 42, in inner
    response = await get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/asgiref/sync.py", line 534, in thread_handler
    raise exc_info[1]
  File "/usr/local/lib/python3.12/site-packages/django/core/handlers/base.py", line 253, in _get_response_async
    response = await wrapped_callback(
               ^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/asgiref/sync.py", line 479, in __call__
    ret: _R = await loop.run_in_executor(
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/concurrent/futures/thread.py", line 58, in run
    result = self.fn(*self.args, **self.kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/asgiref/sync.py", line 538, in thread_handler
    return func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/views/decorators/csrf.py", line 56, in wrapper_view
    return view_func(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/rest_framework/views.py", line 509, in dispatch
    response = self.handle_exception(exc)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/rest_framework/views.py", line 469, in handle_exception
    self.raise_uncaught_exception(exc)
  File "/usr/local/lib/python3.12/site-packages/rest_framework/views.py", line 480, in raise_uncaught_exception
    raise exc
  File "/usr/local/lib/python3.12/site-packages/rest_framework/views.py", line 506, in dispatch
    response = handler(request, *args, **kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/tour_app/tour_api/views.py", line 78, in get
    tid = game.game.tournament.id
          ^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'id'

Code throwing the error

class TournamentHistoryApiView(APIView):
    def get(self, request, user_id):
        games = OnlinePlayermatch.objects.filter(player=user_id)
        logger.info(games)
        tournament_details = []
        for game in games:
            logger.debug(game.game.tournament)
            tid = game.game.tournament.id

https://github.com/ghamry03/ft_transcendence/blob/main/srcs/tour_app/tour_api/views.py#L78

The last line in this snippet causes the crash. It happens because there are games where the tournament is None, so it crashes when you try to access its ID. Please protect this call and make sure there aren't any other cases where the tournament is accessed without checking for None.

lde-alen commented 6 months ago

Fixed