Skip to content

Commit

Permalink
Merge pull request #90 from gamultong/enhancement/proper-error-msg
Browse files Browse the repository at this point in the history
try catch 시 exception 제대로 메시지로 변환
  • Loading branch information
onee-only authored Dec 28, 2024
2 parents f7647a7 + b2c88e5 commit 154327a
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 40 deletions.
12 changes: 10 additions & 2 deletions board/data/internal/exceptions.py
Original file line number Diff line number Diff line change
@@ -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}"
36 changes: 22 additions & 14 deletions board/data/internal/tile.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,24 +58,30 @@ 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,
is_flag=is_flag,
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
Expand All @@ -87,16 +93,18 @@ 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,
is_flag=is_flag,
color=color,
number=number
)

if t.is_open and t.is_flag:
# 열려있는 타일은 깃발이 꽂혀있을 수 없음
raise InvalidTileException(t.__dict__)

return t


Expand Down
23 changes: 19 additions & 4 deletions cursor/data/handler/internal/cursor_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
8 changes: 5 additions & 3 deletions event/internal/exceptions.py
Original file line number Diff line number Diff line change
@@ -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}'"
2 changes: 1 addition & 1 deletion event/test/event_broker_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
8 changes: 5 additions & 3 deletions message/internal/exceptions.py
Original file line number Diff line number Diff line change
@@ -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}'"
9 changes: 4 additions & 5 deletions message/payload/internal/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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"
2 changes: 1 addition & 1 deletion message/payload/test/base_payload_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
2 changes: 1 addition & 1 deletion message/test/message_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__":
Expand Down
8 changes: 2 additions & 6 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 154327a

Please sign in to comment.