[#5]: 공통 기능 코틀린 마이그레이션 및 패키지 분리 #6

Merged
pricelees merged 20 commits from refactor/#5 into main 2025-07-14 05:05:48 +00:00
3 changed files with 64 additions and 68 deletions
Showing only changes of commit f82ad248b5 - Show all commits

View File

@ -1,12 +1,12 @@
package roomescape.common.exception;
package roomescape.common.exception
import java.util.Arrays;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public enum ErrorType {
import com.fasterxml.jackson.annotation.JsonCreator
import com.fasterxml.jackson.annotation.JsonProperty
import org.springframework.http.HttpStatus
enum class ErrorType(
@JvmField val description: String
) {
// 400 Bad Request
REQUEST_DATA_BLANK("요청 데이터에 유효하지 않은 값(null OR 공백)이 포함되어있습니다."),
INVALID_REQUEST_DATA_TYPE("요청 데이터 형식이 올바르지 않습니다."),
@ -54,21 +54,17 @@ public enum ErrorType {
PAYMENT_ERROR("결제(취소)에 실패했습니다. 결제(취소) 정보를 확인해주세요."),
PAYMENT_SERVER_ERROR("결제 서버에서 에러가 발생하였습니다. 잠시 후 다시 시도해주세요.");
private final String description;
ErrorType(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
companion object {
@JvmStatic
@JsonCreator
public static ErrorType from(@JsonProperty("errorType") String errorType) {
return Arrays.stream(ErrorType.values())
.filter(type -> type.name().equalsIgnoreCase(errorType))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("Invalid error type: " + errorType));
fun from(@JsonProperty("errorType") errorType: String): ErrorType {
return entries.toTypedArray()
.firstOrNull { it.name == errorType }
?: throw RoomEscapeException(
INVALID_REQUEST_DATA,
"[ErrorType: ${errorType}]",
HttpStatus.BAD_REQUEST
)
}
}
}

View File

@ -33,7 +33,7 @@ public class ExceptionControllerAdvice {
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleResourceAccessException(ResourceAccessException e) {
logger.error(e.getMessage(), e);
return ErrorResponse.of(ErrorType.PAYMENT_SERVER_ERROR, ErrorType.PAYMENT_SERVER_ERROR.getDescription());
return ErrorResponse.of(ErrorType.PAYMENT_SERVER_ERROR, ErrorType.PAYMENT_SERVER_ERROR.description);
}
@ExceptionHandler(value = HttpMessageNotReadableException.class)
@ -41,7 +41,7 @@ public class ExceptionControllerAdvice {
public ErrorResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
logger.error(e.getMessage(), e);
return ErrorResponse.of(ErrorType.INVALID_REQUEST_DATA_TYPE,
ErrorType.INVALID_REQUEST_DATA_TYPE.getDescription());
ErrorType.INVALID_REQUEST_DATA_TYPE.description);
}
@ExceptionHandler(value = MethodArgumentNotValidException.class)
@ -59,13 +59,13 @@ public class ExceptionControllerAdvice {
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
public ErrorResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
logger.error(e.getMessage(), e);
return ErrorResponse.of(ErrorType.METHOD_NOT_ALLOWED, ErrorType.METHOD_NOT_ALLOWED.getDescription());
return ErrorResponse.of(ErrorType.METHOD_NOT_ALLOWED, ErrorType.METHOD_NOT_ALLOWED.description);
}
@ExceptionHandler(value = Exception.class)
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public ErrorResponse handleException(Exception e) {
logger.error(e.getMessage(), e);
return ErrorResponse.of(ErrorType.INTERNAL_SERVER_ERROR, ErrorType.INTERNAL_SERVER_ERROR.getDescription());
return ErrorResponse.of(ErrorType.INTERNAL_SERVER_ERROR, ErrorType.INTERNAL_SERVER_ERROR.description);
}
}

View File

@ -17,7 +17,7 @@ public class RoomEscapeException extends RuntimeException {
public RoomEscapeException(ErrorType errorType, String invalidValue, HttpStatusCode httpStatus) {
this.errorType = errorType;
this.message = errorType.getDescription();
this.message = errorType.description;
this.invalidValue = invalidValue;
this.httpStatus = httpStatus;
}