Skip to content

Commit c4834de

Browse files
authored
♻️ refactor: 커스텀 예외 적용
* ♻️ 커스텀 예외 적용 * chore: Java 스타일 수정 --------- Co-authored-by: github-actions <>
1 parent a2048dd commit c4834de

File tree

3 files changed

+19
-23
lines changed

3 files changed

+19
-23
lines changed

backend/src/main/java/io/f1/backend/domain/game/app/RoomService.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@
3131
import io.f1.backend.domain.game.store.RoomRepository;
3232
import io.f1.backend.domain.quiz.app.QuizService;
3333
import io.f1.backend.domain.quiz.entity.Quiz;
34+
import io.f1.backend.global.exception.CustomException;
35+
import io.f1.backend.global.exception.errorcode.RoomErrorCode;
3436

3537
import lombok.RequiredArgsConstructor;
3638
import lombok.extern.slf4j.Slf4j;
3739

38-
import org.hibernate.boot.model.naming.IllegalIdentifierException;
3940
import org.springframework.context.ApplicationEventPublisher;
4041
import org.springframework.stereotype.Service;
4142

@@ -91,18 +92,18 @@ public void enterRoom(RoomValidationRequest request) {
9192
Room room = findRoom(request.roomId());
9293

9394
if (room.getState().equals(RoomState.PLAYING)) {
94-
throw new IllegalArgumentException("403 게임이 진행중입니다.");
95+
throw new CustomException(RoomErrorCode.ROOM_GAME_IN_PROGRESS);
9596
}
9697

9798
int maxUserCnt = room.getRoomSetting().maxUserCount();
9899
int currentCnt = room.getUserIdSessionMap().size();
99100
if (maxUserCnt == currentCnt) {
100-
throw new IllegalArgumentException("403 정원이 모두 찼습니다.");
101+
throw new CustomException(RoomErrorCode.ROOM_USER_LIMIT_REACHED);
101102
}
102103

103104
if (room.getRoomSetting().locked()
104105
&& !room.getRoomSetting().password().equals(request.password())) {
105-
throw new IllegalArgumentException("401 비밀번호가 일치하지 않습니다.");
106+
throw new CustomException(RoomErrorCode.WRONG_PASSWORD);
106107
}
107108

108109
room.getUserIdSessionMap().put(getCurrentUserId(), PENDING_SESSION_ID);
@@ -201,7 +202,7 @@ private Player getRemovePlayer(Room room, String sessionId) {
201202
Player removePlayer = room.getPlayerSessionMap().get(sessionId);
202203
if (removePlayer == null) {
203204
room.removeUserId(getCurrentUserId());
204-
throw new IllegalIdentifierException("404 세션 없음 비정상적인 퇴장 요청");
205+
throw new CustomException(RoomErrorCode.SOCKET_SESSION_NOT_FOUND);
205206
}
206207
return removePlayer;
207208
}
@@ -217,7 +218,7 @@ private Player createPlayer() {
217218
private Room findRoom(Long roomId) {
218219
return roomRepository
219220
.findRoom(roomId)
220-
.orElseThrow(() -> new IllegalArgumentException("404 존재하지 않는 방입니다."));
221+
.orElseThrow(() -> new CustomException(RoomErrorCode.ROOM_NOT_FOUND));
221222
}
222223

223224
private boolean isLastPlayer(Room room, String sessionId) {
@@ -244,9 +245,7 @@ private void changeHost(Room room, String hostSessionId) {
244245
Player nextHost =
245246
playerSessionMap.get(
246247
nextHostSessionId.orElseThrow(
247-
() ->
248-
new IllegalArgumentException(
249-
"방장 교체 불가 - 404 해당 세션 플레이어는 존재하지않습니다.")));
248+
() -> new CustomException(RoomErrorCode.SOCKET_SESSION_NOT_FOUND)));
250249

251250
room.updateHost(nextHost);
252251
log.info("user_id:{} 방장 변경 완료 ", nextHost.getId());

backend/src/main/java/io/f1/backend/global/config/StompChannelInterceptor.java

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,16 @@ public Message<?> preSend(Message<?> message, MessageChannel channel) {
2727
throw new IllegalArgumentException("Stomp command required");
2828
}
2929

30-
switch (command) {
31-
case CONNECT -> log.info("CONNECT : 세션 연결 - sessionId = {}", sessionId);
32-
33-
case SUBSCRIBE -> {
34-
if (destination != null && sessionId != null) {
35-
log.info("SUBSCRIBE : 구독 시작 destination = {}", destination);
36-
}
30+
if (command.equals(StompCommand.CONNECT)) {
31+
log.info("CONNECT : 세션 연결 - sessionId = {}", sessionId);
32+
} else if (command.equals(StompCommand.SUBSCRIBE)) {
33+
if (destination != null && sessionId != null) {
34+
log.info("SUBSCRIBE : 구독 시작 destination = {}", destination);
3735
}
38-
39-
case SEND -> log.info("SEND : 요청 destination = {}", destination);
40-
41-
case DISCONNECT -> log.info("DISCONNECT : 연결 해제 sessionId = {}", sessionId);
42-
43-
default -> throw new IllegalStateException("Unexpected command: " + command);
36+
} else if (command.equals(StompCommand.SEND)) {
37+
log.info("SEND : 요청 destination = {}", destination);
38+
} else if (command.equals(StompCommand.DISCONNECT)) {
39+
log.info("DISCONNECT : 연결 해제 sessionId = {}", sessionId);
4440
}
4541

4642
return message;

backend/src/main/java/io/f1/backend/global/exception/errorcode/RoomErrorCode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public enum RoomErrorCode implements ErrorCode {
1111
ROOM_USER_LIMIT_REACHED("E403002", HttpStatus.FORBIDDEN, "정원이 모두 찼습니다."),
1212
ROOM_GAME_IN_PROGRESS("E403003", HttpStatus.FORBIDDEN, "게임이 진행 중 입니다."),
1313
ROOM_NOT_FOUND("E404005", HttpStatus.NOT_FOUND, "존재하지 않는 방입니다."),
14-
WRONG_PASSWORD("E401006", HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지않습니다.");
14+
WRONG_PASSWORD("E401006", HttpStatus.UNAUTHORIZED, "비밀번호가 일치하지않습니다."),
15+
SOCKET_SESSION_NOT_FOUND("E404006", HttpStatus.NOT_FOUND, "존재하지 않는 소켓 세션입니다.");
1516

1617
private final String code;
1718

0 commit comments

Comments
 (0)