Skip to content

Commit

Permalink
refactor: 메시지와 채팅 타입이 대소문자 구분없이 매핑되도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ503 committed Apr 16, 2024
1 parent b8f6d51 commit 4247351
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
import com.ddang.ddang.review.application.exception.ReviewNotFoundException;
import com.ddang.ddang.user.application.exception.AlreadyExistsNameException;
import com.ddang.ddang.user.application.exception.UserNotFoundException;
import com.ddang.ddang.websocket.handler.exception.UnsupportedChattingTypeException;
import com.ddang.ddang.websocket.handler.exception.UnsupportedTextMessageTypeException;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
Expand Down Expand Up @@ -436,6 +438,26 @@ public ResponseEntity<ExceptionResponse> handleWithdrawalNotAllowedException(fin
.body(new ExceptionResponse(ex.getMessage()));
}

@ExceptionHandler(UnsupportedTextMessageTypeException.class)
public ResponseEntity<ExceptionResponse> handleUnsupportedTextMessageTypeException(
final UnsupportedTextMessageTypeException ex
) {
logger.warn(String.format(LOG_MESSAGE_FORMAT, ex.getClass().getSimpleName(), ex.getMessage()));

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ExceptionResponse(ex.getMessage()));
}

@ExceptionHandler(UnsupportedChattingTypeException.class)
public ResponseEntity<ExceptionResponse> handleUnsupportedChattingTypeException(
final UnsupportedChattingTypeException ex
) {
logger.warn(String.format(LOG_MESSAGE_FORMAT, ex.getClass().getSimpleName(), ex.getMessage()));

return ResponseEntity.status(HttpStatus.BAD_REQUEST)
.body(new ExceptionResponse(ex.getMessage()));
}

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(
final MethodArgumentNotValidException ex,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
package com.ddang.ddang.websocket.handler.dto;

import com.ddang.ddang.websocket.handler.exception.UnsupportedChattingTypeException;

import java.util.Arrays;

public enum ChatMessageType {

MESSAGE("message"),
PING("ping"),
;

private final String value;

ChatMessageType(final String value) {
this.value = value;
}
MESSAGE,
PING;

public static ChatMessageType findMessageType(final String value) {
return Arrays.stream(ChatMessageType.values())
.filter(chattingType -> chattingType.value.equals(value))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("잘못된 채팅 타입입니다."));
.filter(chattingType -> chattingType.name().equalsIgnoreCase(value))
.findFirst()
.orElseThrow(() -> new UnsupportedChattingTypeException("잘못된 채팅 타입입니다."));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@
package com.ddang.ddang.websocket.handler.dto;

import com.ddang.ddang.websocket.handler.exception.UnsupportedTextMessageTypeException;
import com.fasterxml.jackson.annotation.JsonCreator;

import java.util.Arrays;

public enum TextMessageType {

CHATTINGS,
BIDS
BIDS;

@JsonCreator
public static TextMessageType fromString(String value) {
return Arrays.stream(TextMessageType.values())
.filter(messageType -> messageType.name().equalsIgnoreCase(value))
.findFirst()
.orElseThrow(() -> new UnsupportedTextMessageTypeException("잘못된 메시지 타입입니다."));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ddang.ddang.websocket.handler.exception;

public class UnsupportedChattingTypeException extends IllegalStateException {
public UnsupportedChattingTypeException(final String message) {
super(message);
}
}

0 comments on commit 4247351

Please sign in to comment.