Skip to content

Commit

Permalink
Merge pull request #366 from Whist-Team/fix/invalid_room_id
Browse files Browse the repository at this point in the history
  • Loading branch information
iTitus authored Oct 23, 2022
2 parents 2c3906b + aa9f217 commit fa15d3f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
5 changes: 5 additions & 0 deletions tests/whist_server/services/test_room_db_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ def test_not_existing(self):
with self.assertRaisesRegex(RoomNotFoundError, error_msg):
self.service.get(game_id)

def test_get_invalid_room_id(self):
room_id = '1'
error_msg = f'Room with id "{room_id}" not found.'
with self.assertRaisesRegex(RoomNotFoundError, error_msg):
self.service.get(room_id)
def test_get_by_name(self):
game_id = self.service.add(self.room)
self.room.id = ObjectId(game_id)
Expand Down
6 changes: 5 additions & 1 deletion whist_server/services/room_db_service.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Room database connector"""
from typing import Optional

import bson.errors
from bson import ObjectId
from whist_core.user.player import Player

Expand Down Expand Up @@ -70,7 +71,10 @@ def get(cls, room_id: str) -> RoomInDb:
:param room_id: of the room
:return: the room database object
"""
room = cls._rooms.find_one(ObjectId(room_id))
try:
room = cls._rooms.find_one(ObjectId(room_id))
except bson.errors.InvalidId as id_error:
raise RoomNotFoundError(room_id) from id_error
if room is None:
raise RoomNotFoundError(room_id)
return RoomInDb(**room)
Expand Down

0 comments on commit fa15d3f

Please sign in to comment.