generated from pricelees/issue-pr-template
refactor: ErrorType enum 코틀린 전환
This commit is contained in:
parent
a9dc42263a
commit
f82ad248b5
@ -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
|
||||||
import com.fasterxml.jackson.annotation.JsonCreator;
|
import org.springframework.http.HttpStatus
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
|
|
||||||
public enum ErrorType {
|
|
||||||
|
|
||||||
|
enum class ErrorType(
|
||||||
|
@JvmField val description: String
|
||||||
|
) {
|
||||||
// 400 Bad Request
|
// 400 Bad Request
|
||||||
REQUEST_DATA_BLANK("요청 데이터에 유효하지 않은 값(null OR 공백)이 포함되어있습니다."),
|
REQUEST_DATA_BLANK("요청 데이터에 유효하지 않은 값(null OR 공백)이 포함되어있습니다."),
|
||||||
INVALID_REQUEST_DATA_TYPE("요청 데이터 형식이 올바르지 않습니다."),
|
INVALID_REQUEST_DATA_TYPE("요청 데이터 형식이 올바르지 않습니다."),
|
||||||
@ -54,21 +54,17 @@ public enum ErrorType {
|
|||||||
PAYMENT_ERROR("결제(취소)에 실패했습니다. 결제(취소) 정보를 확인해주세요."),
|
PAYMENT_ERROR("결제(취소)에 실패했습니다. 결제(취소) 정보를 확인해주세요."),
|
||||||
PAYMENT_SERVER_ERROR("결제 서버에서 에러가 발생하였습니다. 잠시 후 다시 시도해주세요.");
|
PAYMENT_SERVER_ERROR("결제 서버에서 에러가 발생하였습니다. 잠시 후 다시 시도해주세요.");
|
||||||
|
|
||||||
private final String description;
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
ErrorType(String description) {
|
|
||||||
this.description = description;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getDescription() {
|
|
||||||
return description;
|
|
||||||
}
|
|
||||||
|
|
||||||
@JsonCreator
|
@JsonCreator
|
||||||
public static ErrorType from(@JsonProperty("errorType") String errorType) {
|
fun from(@JsonProperty("errorType") errorType: String): ErrorType {
|
||||||
return Arrays.stream(ErrorType.values())
|
return entries.toTypedArray()
|
||||||
.filter(type -> type.name().equalsIgnoreCase(errorType))
|
.firstOrNull { it.name == errorType }
|
||||||
.findFirst()
|
?: throw RoomEscapeException(
|
||||||
.orElseThrow(() -> new IllegalArgumentException("Invalid error type: " + errorType));
|
INVALID_REQUEST_DATA,
|
||||||
|
"[ErrorType: ${errorType}]",
|
||||||
|
HttpStatus.BAD_REQUEST
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,7 +33,7 @@ public class ExceptionControllerAdvice {
|
|||||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
public ErrorResponse handleResourceAccessException(ResourceAccessException e) {
|
public ErrorResponse handleResourceAccessException(ResourceAccessException e) {
|
||||||
logger.error(e.getMessage(), 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)
|
@ExceptionHandler(value = HttpMessageNotReadableException.class)
|
||||||
@ -41,7 +41,7 @@ public class ExceptionControllerAdvice {
|
|||||||
public ErrorResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
public ErrorResponse handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
return ErrorResponse.of(ErrorType.INVALID_REQUEST_DATA_TYPE,
|
return ErrorResponse.of(ErrorType.INVALID_REQUEST_DATA_TYPE,
|
||||||
ErrorType.INVALID_REQUEST_DATA_TYPE.getDescription());
|
ErrorType.INVALID_REQUEST_DATA_TYPE.description);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
@ExceptionHandler(value = MethodArgumentNotValidException.class)
|
||||||
@ -59,13 +59,13 @@ public class ExceptionControllerAdvice {
|
|||||||
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
|
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
|
||||||
public ErrorResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
public ErrorResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
||||||
logger.error(e.getMessage(), 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)
|
@ExceptionHandler(value = Exception.class)
|
||||||
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||||
public ErrorResponse handleException(Exception e) {
|
public ErrorResponse handleException(Exception e) {
|
||||||
logger.error(e.getMessage(), 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,7 @@ public class RoomEscapeException extends RuntimeException {
|
|||||||
|
|
||||||
public RoomEscapeException(ErrorType errorType, String invalidValue, HttpStatusCode httpStatus) {
|
public RoomEscapeException(ErrorType errorType, String invalidValue, HttpStatusCode httpStatus) {
|
||||||
this.errorType = errorType;
|
this.errorType = errorType;
|
||||||
this.message = errorType.getDescription();
|
this.message = errorType.description;
|
||||||
this.invalidValue = invalidValue;
|
this.invalidValue = invalidValue;
|
||||||
this.httpStatus = httpStatus;
|
this.httpStatus = httpStatus;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user