Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

에러 핸들링 개선 #69

Merged
merged 6 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion board/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .internal.section import Section, for_each_neighbor
from .internal.tile import Tile
from .internal.tiles import Tiles
from .internal.tile_exception import InvalidTileException
from .internal.exceptions import InvalidTileException, InvalidDataLengthException
7 changes: 7 additions & 0 deletions board/data/internal/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class InvalidTileException(Exception):
msg: str = "invalid tile"


class InvalidDataLengthException(Exception):
def __init__(self, expected: int, actual: int):
self.msg = f"invalid data length. expected: {expected}, actual: {actual}"
3 changes: 2 additions & 1 deletion board/data/internal/section.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .point import Point
from .tile import Tile
from .tiles import Tiles
from .exceptions import InvalidDataLengthException
from random import randint
from typing import Callable

Expand Down Expand Up @@ -34,7 +35,7 @@ def update(self, data: Tiles, start: Point, end: Point | None = None) -> None:

expected_len = (end.x - start.x + 1) * (start.y - end.y + 1)
if len(data.data) != expected_len:
raise "예상 길이와 다름"
raise InvalidDataLengthException(expected=expected_len, actual=len(data.data))

x_gap = end.x - start.x + 1

Expand Down
2 changes: 1 addition & 1 deletion board/data/internal/tile.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from dataclasses import dataclass
from cursor.data import Color
from .tile_exception import InvalidTileException
from .exceptions import InvalidTileException


@dataclass
Expand Down
2 changes: 0 additions & 2 deletions board/data/internal/tile_exception.py

This file was deleted.

7 changes: 2 additions & 5 deletions board/event/handler/internal/board_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,10 @@ async def _publish_tiles(start: Point, end: Point, to: list[str]):
@EventBroker.add_receiver(PointEvent.TRY_POINTING)
@staticmethod
async def receive_try_pointing(message: Message[TryPointingPayload]):
pointer = message.payload.new_pointer

if "sender" not in message.header:
raise "header 없음"

sender = message.header["sender"]

pointer = message.payload.new_pointer

tiles = BoardHandler.fetch(
Point(pointer.x-1, pointer.y+1),
Point(pointer.x+1, pointer.y-1)
Expand Down
6 changes: 3 additions & 3 deletions conn/manager/internal/connection_manager.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from fastapi.websockets import WebSocket
from conn import Conn
from message import Message
from message.payload import NewConnEvent, NewConnPayload, ConnClosedPayload
from message.payload import NewConnEvent, NewConnPayload, ConnClosedPayload, DumbHumanException
from event import EventBroker
from uuid import uuid4

Expand Down Expand Up @@ -74,11 +74,11 @@ async def receive_broadcast_event(message: Message):
async def receive_multicast_event(message: Message):
overwrite_event(message)
if "target_conns" not in message.header:
raise "header에 target_conns 없음"
raise DumbHumanException()
for conn_id in message.header["target_conns"]:
conn = ConnectionManager.get_conn(conn_id)
if not conn:
raise "connection 없음"
raise DumbHumanException()

await conn.send(message)

Expand Down
15 changes: 11 additions & 4 deletions cursor/data/handler/internal/cursor_exception.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
from board.data import Point


class AlreadyWatchingException(Exception):
pass
def __init__(self, watcher: str, watching: str):
self.msg = f"cursor {watcher} is already watching {watching}"


class NoMatchingCursorException(Exception):
pass
def __init__(self, cursor_id: str):
self.msg = f"no matching cursor with id: {cursor_id}"


class NotWatchableException(Exception):
pass
def __init__(self, p: Point, cursor_id: str):
self.msg = f"position: ({p.x}, {p.y}) is not watchable to cursor: {cursor_id}"


class NotWatchingException(Exception):
pass
def __init__(self, watcher: str, watching: str):
self.msg = f"cursor {watcher} is not watching {watching}"
27 changes: 15 additions & 12 deletions cursor/data/handler/internal/cursor_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def create_cursor(conn_id: str):

@staticmethod
def remove_cursor(conn_id: str):
# TODO: 예외 처리?
if conn_id in CursorHandler.cursor_dict:
del CursorHandler.cursor_dict[conn_id]

Expand Down Expand Up @@ -79,16 +78,18 @@ def add_watcher(watcher: Cursor, watching: Cursor) -> None:
watching_id = watching.conn_id

watcher_exists = CursorHandler.check_cursor_exists(watcher_id)
watching_exists = CursorHandler.check_cursor_exists(watching_id)
if not watcher_exists:
raise NoMatchingCursorException(watcher_id)

if not (watcher_exists and watching_exists):
raise NoMatchingCursorException()
watching_exists = CursorHandler.check_cursor_exists(watching_id)
if not watching_exists:
raise NoMatchingCursorException(watching_id)

if CursorHandler.check_cursor_watching(watching_id, watcher_id):
raise AlreadyWatchingException()
raise AlreadyWatchingException(watcher=watcher_id, watching=watching_id)

if not watcher.check_in_view(watching.position):
raise NotWatchableException()
raise NotWatchableException(p=watching.position, cursor_id=watcher.conn_id)

if not watcher_id in CursorHandler.watching:
CursorHandler.watching[watcher_id] = []
Expand All @@ -104,13 +105,15 @@ def remove_watcher(watcher: Cursor, watching: Cursor):
watching_id = watching.conn_id

watcher_exists = CursorHandler.check_cursor_exists(watcher_id)
watching_exists = CursorHandler.check_cursor_exists(watching_id)
if not watcher_exists:
raise NoMatchingCursorException(watcher_id)

if not (watcher_exists and watching_exists):
raise NoMatchingCursorException()
watching_exists = CursorHandler.check_cursor_exists(watching_id)
if not watching_exists:
raise NoMatchingCursorException(watching_id)

if not CursorHandler.check_cursor_watching(watching_id, watcher_id):
raise NotWatchingException()
raise NotWatchingException(watcher=watcher_id, watching=watching_id)

CursorHandler.watching[watcher_id].remove(watching_id)
if len(CursorHandler.watching[watcher_id]) == 0:
Expand All @@ -123,7 +126,7 @@ def remove_watcher(watcher: Cursor, watching: Cursor):
@staticmethod
def get_watchers(cursor_id: str) -> list[str]:
if not CursorHandler.check_cursor_exists(cursor_id):
raise NoMatchingCursorException()
raise NoMatchingCursorException(cursor_id)

if cursor_id in CursorHandler.watchers:
return CursorHandler.watchers[cursor_id].copy()
Expand All @@ -133,7 +136,7 @@ def get_watchers(cursor_id: str) -> list[str]:
@staticmethod
def get_watching(cursor_id: str) -> list[str]:
if not CursorHandler.check_cursor_exists(cursor_id):
raise NoMatchingCursorException()
raise NoMatchingCursorException(cursor_id)

if cursor_id in CursorHandler.watching:
return CursorHandler.watching[cursor_id].copy()
Expand Down
45 changes: 38 additions & 7 deletions cursor/event/handler/internal/cursor_event_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
YouDiedPayload,
ConnClosedPayload,
CursorQuitPayload,
SetViewSizePayload
SetViewSizePayload,
ErrorEvent,
ErrorPayload
)


Expand Down Expand Up @@ -97,8 +99,15 @@ async def receive_pointing(message: Message[PointingPayload]):

# 뷰 바운더리 안에서 포인팅하는지 확인
if not cursor.check_in_view(new_pointer):
# TODO: 예외 처리?
raise "커서 뷰 바운더리 벗어난 곳에 포인팅함"
await EventBroker.publish(Message(
event="multicast",
header={
"origin_event": ErrorEvent.ERROR,
"target_conns": [sender]
},
payload=ErrorPayload(msg="pointer is out of cursor view")
))
return

message = Message(
event=PointEvent.TRY_POINTING,
Expand Down Expand Up @@ -155,11 +164,26 @@ async def receive_moving(message: Message[MovingPayload]):
new_position = message.payload.position

if new_position == cursor.position:
# TODO: 예외 처리
raise "기존 위치와 같은 위치로 이동"
await EventBroker.publish(Message(
event="multicast",
header={
"origin_event": ErrorEvent.ERROR,
"target_conns": [sender]
},
payload=ErrorPayload(msg="moving to current position is not allowed")
))
return

if not cursor.check_interactable(new_position):
raise "주변 8칸 벗어남"
await EventBroker.publish(Message(
event="multicast",
header={
"origin_event": ErrorEvent.ERROR,
"target_conns": [sender]
},
payload=ErrorPayload(msg="only moving to 8 nearby tiles is allowed")
))
return

message = Message(
event=MoveEvent.CHECK_MOVABLE,
Expand All @@ -179,7 +203,14 @@ async def receive_movable_result(message: Message[MovableResultPayload]):
cursor = CursorHandler.get_cursor(receiver)

if not message.payload.movable:
# TODO: 사용자에게 알리기?
await EventBroker.publish(Message(
event="multicast",
header={
"origin_event": ErrorEvent.ERROR,
"target_conns": [receiver]
},
payload=ErrorPayload(msg="moving to given tile is not available")
))
return

new_position = message.payload.position
Expand Down
Loading
Loading