diff --git a/board/data/internal/exceptions.py b/board/data/internal/exceptions.py index 8577ff5..5489173 100644 --- a/board/data/internal/exceptions.py +++ b/board/data/internal/exceptions.py @@ -1,7 +1,15 @@ class InvalidTileException(Exception): - msg: str = "invalid tile" + def __init__(self, tile: dict): + self.tile = tile + + def __str__(self): + return f"invalid tile: {self.tile}" class InvalidDataLengthException(Exception): def __init__(self, expected: int, actual: int): - self.msg = f"invalid data length. expected: {expected}, actual: {actual}" + self.expected = expected + self.actual = actual + + def __str__(self): + return f"invalid data length. expected: {self.expected}, actual: {self.actual}" diff --git a/board/data/internal/tile.py b/board/data/internal/tile.py index f32e273..0e86364 100644 --- a/board/data/internal/tile.py +++ b/board/data/internal/tile.py @@ -58,17 +58,6 @@ def create( """ __init__ 대신 이걸 쓰기를 권장. """ - if number and (number >= 8 or number < 0): - raise InvalidTileException - if is_mine and number: - raise InvalidTileException - if is_open and is_flag: - raise InvalidTileException - if is_flag and (color is None): - raise InvalidTileException - if (not is_flag) and (color is not None): - raise InvalidTileException - t = Tile( is_open=is_open, is_mine=is_mine, @@ -76,6 +65,23 @@ def create( color=color, number=number ) + + if (t.number is not None) and (t.number >= 8 or t.number < 0): + # 숫자는 음수이거나 8 이상일 수 없음 + raise InvalidTileException(t.__dict__) + if t.is_mine and (t.number is not None): + # 지뢰 타일은 숫자를 가지고있지 않음 + raise InvalidTileException(t.__dict__) + if is_open and is_flag: + # 열려있는 타일은 깃발이 꽂혀있을 수 없음 + raise InvalidTileException(t.__dict__) + if is_flag and (color is None): + # 색깔 없이 깃발이 꽂혀있을 수 없음 + raise InvalidTileException(t.__dict__) + if (not is_flag) and (color is not None): + # 깃발이 없는 채로 색깔이 존재할 수 없음 + raise InvalidTileException(t.__dict__) + return t @staticmethod @@ -87,9 +93,6 @@ def from_int(b: int): color = extract_color(b) if is_flag else None number = extract_number(b) if not is_mine else None - if is_open and is_flag: - raise InvalidTileException() - t = Tile( is_open=is_open, is_mine=is_mine, @@ -97,6 +100,11 @@ def from_int(b: int): color=color, number=number ) + + if t.is_open and t.is_flag: + # 열려있는 타일은 깃발이 꽂혀있을 수 없음 + raise InvalidTileException(t.__dict__) + return t diff --git a/cursor/data/handler/internal/cursor_exception.py b/cursor/data/handler/internal/cursor_exception.py index 676a431..61ee29b 100644 --- a/cursor/data/handler/internal/cursor_exception.py +++ b/cursor/data/handler/internal/cursor_exception.py @@ -3,19 +3,34 @@ class AlreadyWatchingException(Exception): def __init__(self, watcher: str, watching: str): - self.msg = f"cursor {watcher} is already watching {watching}" + self.watcher = watcher + self.watchin = watching + + def __str__(self): + return f"cursor {self.watcher} is already watching {self.watching}" class NoMatchingCursorException(Exception): def __init__(self, cursor_id: str): - self.msg = f"no matching cursor with id: {cursor_id}" + self.cursor_id = cursor_id + + def __str__(self): + return f"no matching cursor with id: {self.cursor_id}" class NotWatchableException(Exception): def __init__(self, p: Point, cursor_id: str): - self.msg = f"position: ({p.x}, {p.y}) is not watchable to cursor: {cursor_id}" + self.p = p + self.cursor_id = cursor_id + + def __str__(self): + return f"position: ({self.p.x}, {self.p.y}) is not watchable to cursor: {self.cursor_id}" class NotWatchingException(Exception): def __init__(self, watcher: str, watching: str): - self.msg = f"cursor {watcher} is not watching {watching}" + self.watcher = watcher + self.watchin = watching + + def __str__(self): + return f"cursor {self.watcher} is not watching {self.watching}" diff --git a/event/internal/exceptions.py b/event/internal/exceptions.py index 4ff8ca9..a53c283 100644 --- a/event/internal/exceptions.py +++ b/event/internal/exceptions.py @@ -1,4 +1,6 @@ class NoMatchingReceiverException(Exception): - def __init__(self, event, *args): - self.msg = f"no matching receiver for '{event}'" - super().__init__(*args) + def __init__(self, event: str): + self.event = event + + def __str__(self): + return f"no matching receiver for '{self.event}'" diff --git a/event/test/event_broker_test.py b/event/test/event_broker_test.py index 23759b6..cde7619 100644 --- a/event/test/event_broker_test.py +++ b/event/test/event_broker_test.py @@ -65,7 +65,7 @@ async def test_publish_no_receiver(self): with self.assertRaises(NoMatchingReceiverException) as cm: await EventBroker.publish(message=message) - self.assertEqual(cm.exception.msg, "no matching receiver for 'invaild_event'") + self.assertEqual(cm.exception.__str__(), "no matching receiver for 'invaild_event'") if __name__ == "__main__": diff --git a/message/internal/exceptions.py b/message/internal/exceptions.py index 762c7ee..22fbb8d 100644 --- a/message/internal/exceptions.py +++ b/message/internal/exceptions.py @@ -1,4 +1,6 @@ class InvalidEventTypeException(Exception): - def __init__(self, event, *args): - self.msg = f"invalid event type: '{event}'" - super().__init__(*args) + def __init__(self, event: str): + self.event = event + + def __str__(self): + return f"invalid event type: '{self.event}'" diff --git a/message/payload/internal/exceptions.py b/message/payload/internal/exceptions.py index 4675574..8e344cc 100644 --- a/message/payload/internal/exceptions.py +++ b/message/payload/internal/exceptions.py @@ -3,8 +3,7 @@ def __init__(self, key, value): self.key = key self.value = value - @property - def msg(self): + def __str__(self): return f"invaild field: {self.get_key()} -> {self.get_value()}" def get_key(self): @@ -23,8 +22,7 @@ def __init__(self, key, value=None): self.key = key self.value = value - @property - def msg(self): + def __str__(self): return f"missing {len(tuple(self.get_keys()))} keys: {", ".join(self.get_keys())}" def get_keys(self): @@ -34,4 +32,5 @@ def get_keys(self): class DumbHumanException(Exception): - msg: str = "worng use. what have you done" + def __str__(self): + return "worng use. what have you done" diff --git a/message/payload/test/base_payload_test.py b/message/payload/test/base_payload_test.py index 61f0107..fabe837 100644 --- a/message/payload/test/base_payload_test.py +++ b/message/payload/test/base_payload_test.py @@ -27,7 +27,7 @@ def test_from_dict_wrongs(self, dict, payload, exp, msg): with self.assertRaises(exp) as cm: payload._from_dict(dict=dict) - self.assertEqual(cm.exception.msg, msg) + self.assertEqual(cm.exception.__str__(), msg) if __name__ == "__main__": diff --git a/message/test/message_test.py b/message/test/message_test.py index ec4dcbd..f096f87 100644 --- a/message/test/message_test.py +++ b/message/test/message_test.py @@ -64,7 +64,7 @@ def test_from_str_invalid_event(self): with self.assertRaises(InvalidEventTypeException) as cm: Message.from_str(socket_msg) - self.assertEqual(cm.exception.msg, "invalid event type: 'ayo invalid'") + self.assertEqual(cm.exception.__str__(), "invalid event type: 'ayo invalid'") if __name__ == "__main__": diff --git a/server.py b/server.py index 86909a0..60c24af 100644 --- a/server.py +++ b/server.py @@ -45,16 +45,12 @@ async def session(ws: WebSocket): # 연결 종료됨 break except Exception as e: - msg = e - if hasattr(e, "msg"): - msg = e.msg - await conn.send(Message( event=ErrorEvent.ERROR, - payload=ErrorPayload(msg=msg) + payload=ErrorPayload(msg=e) )) - print(f"Unhandled error while handling message: \n{message.__dict__}\n{type(e)}: '{msg}'") + print(f"Unhandled error while handling message: \n{message.__dict__}\n{type(e)}: '{e}'") break await ConnectionManager.close(conn)