diff --git a/batch-quartz/src/main/java/com/spring/common/error/BizBaseException.java b/batch-quartz/src/main/java/com/spring/common/error/BizBaseException.java index 9082e1a..7234b4d 100644 --- a/batch-quartz/src/main/java/com/spring/common/error/BizBaseException.java +++ b/batch-quartz/src/main/java/com/spring/common/error/BizBaseException.java @@ -24,6 +24,8 @@ public class BizBaseException extends RuntimeException { */ private final ErrorRule errorRule; + private final String customMessage; + /** * 기본 생성자입니다. * @@ -32,6 +34,7 @@ public class BizBaseException extends RuntimeException { public BizBaseException() { super(ExceptionRule.SYSTEM_ERROR.getMessage()); this.errorRule = ExceptionRule.SYSTEM_ERROR; + this.customMessage = null; } /** @@ -42,11 +45,19 @@ public class BizBaseException extends RuntimeException { public BizBaseException(ErrorRule exceptionRule) { super(exceptionRule.getMessage()); this.errorRule = exceptionRule; + this.customMessage = null; + } + + public BizBaseException(ErrorRule errorRule, String message) { + super(message); + this.errorRule = errorRule; + this.customMessage = message; } public BizBaseException(String message) { - super(message); - this.errorRule = ExceptionRule.SYSTEM_ERROR.message(message); + super(ExceptionRule.SYSTEM_ERROR.getMessage()); + this.errorRule = ExceptionRule.SYSTEM_ERROR; + this.customMessage = message; } } diff --git a/batch-quartz/src/main/java/com/spring/common/error/BizErrorResponse.java b/batch-quartz/src/main/java/com/spring/common/error/BizErrorResponse.java index 8907dd5..fdafd6a 100644 --- a/batch-quartz/src/main/java/com/spring/common/error/BizErrorResponse.java +++ b/batch-quartz/src/main/java/com/spring/common/error/BizErrorResponse.java @@ -34,9 +34,10 @@ public class BizErrorResponse extends ErrorResponse { * @param exceptionRule 발생한 예외의 오류 규칙 * @param errors 발생한 오류 목록 */ - public BizErrorResponse(ErrorRule exceptionRule, List errors) { + public BizErrorResponse(ErrorRule exceptionRule, List errors, String customMessage) { super(exceptionRule); this.errors = errors; + this.customMessage = customMessage; } /** @@ -47,8 +48,12 @@ public class BizErrorResponse extends ErrorResponse { * @param exceptionRule 발생한 예외의 오류 규칙 * @return 생성된 BizErrorResponse 객체 */ - public static BizErrorResponse valueOf(ErrorRule exceptionRule) { - return new BizErrorResponse(exceptionRule, null); + public static BizErrorResponse fromErrorRule(ErrorRule exceptionRule) { + return new BizErrorResponse(exceptionRule, null, null); + } + + public static BizErrorResponse of(ErrorRule exceptionRule, String message) { + return new BizErrorResponse(exceptionRule, null, message); } /** @@ -63,7 +68,7 @@ public class BizErrorResponse extends ErrorResponse { List errors = fieldErrors.stream() .map(fieldError -> RejectedValue.of(fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage())) .collect(Collectors.toList()); - return new BizErrorResponse(ExceptionRule.UNPROCESSABLE_ENTITY, errors); + return new BizErrorResponse(ExceptionRule.UNPROCESSABLE_ENTITY, errors, null); } } diff --git a/batch-quartz/src/main/java/com/spring/common/error/ErrorResponse.java b/batch-quartz/src/main/java/com/spring/common/error/ErrorResponse.java index 6760757..ee3e3a6 100644 --- a/batch-quartz/src/main/java/com/spring/common/error/ErrorResponse.java +++ b/batch-quartz/src/main/java/com/spring/common/error/ErrorResponse.java @@ -33,5 +33,7 @@ public abstract class ErrorResponse { *

비즈니스 로직에서 발생한 오류에 대한 세부 정보를 포함합니다.

*/ protected List errors; + + protected String customMessage; } diff --git a/batch-quartz/src/main/java/com/spring/common/error/ExceptionRule.java b/batch-quartz/src/main/java/com/spring/common/error/ExceptionRule.java index ef4c263..f5abcab 100644 --- a/batch-quartz/src/main/java/com/spring/common/error/ExceptionRule.java +++ b/batch-quartz/src/main/java/com/spring/common/error/ExceptionRule.java @@ -30,16 +30,6 @@ public enum ExceptionRule implements ErrorRule { UNPROCESSABLE_ENTITY(HttpStatus.UNPROCESSABLE_ENTITY, "요청을 처리할 수 없습니다."); private final HttpStatus status; - private String message; - - ExceptionRule(HttpStatus status, String message) { - this.status = status; - this.message = message; - } - - public ExceptionRule message(final String message) { - this.message = message; - return this; - } + private final String message; } diff --git a/batch-quartz/src/main/java/com/spring/common/error/GlobalErrorController.java b/batch-quartz/src/main/java/com/spring/common/error/GlobalErrorController.java index a384b96..a07c9f9 100644 --- a/batch-quartz/src/main/java/com/spring/common/error/GlobalErrorController.java +++ b/batch-quartz/src/main/java/com/spring/common/error/GlobalErrorController.java @@ -66,7 +66,7 @@ public class GlobalErrorController implements ErrorController { Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); int statusCode = status != null ? Integer.parseInt(status.toString()) : HttpStatus.INTERNAL_SERVER_ERROR.value(); HttpStatus httpStatus = HttpStatus.valueOf(statusCode); - return new ResponseEntity<>(BizErrorResponse.valueOf(ExceptionRule.NOT_FOUND), httpStatus); + return new ResponseEntity<>(BizErrorResponse.fromErrorRule(ExceptionRule.NOT_FOUND), httpStatus); } /** diff --git a/batch-quartz/src/main/java/com/spring/common/error/GlobalExceptionHandler.java b/batch-quartz/src/main/java/com/spring/common/error/GlobalExceptionHandler.java index 938fb75..e480867 100644 --- a/batch-quartz/src/main/java/com/spring/common/error/GlobalExceptionHandler.java +++ b/batch-quartz/src/main/java/com/spring/common/error/GlobalExceptionHandler.java @@ -37,7 +37,7 @@ public class GlobalExceptionHandler { */ @ExceptionHandler(BizBaseException.class) public BizErrorResponse handleCustomException(BizBaseException e) { - return BizErrorResponse.valueOf(e.getErrorRule()); + return BizErrorResponse.fromErrorRule(e.getErrorRule()); } /** @@ -50,7 +50,7 @@ public class GlobalExceptionHandler { */ @ExceptionHandler(Exception.class) public BizErrorResponse handleException(Exception e) { - return BizErrorResponse.valueOf(ExceptionRule.SYSTEM_ERROR); + return BizErrorResponse.fromErrorRule(ExceptionRule.SYSTEM_ERROR); } /** @@ -99,12 +99,11 @@ public class GlobalExceptionHandler { @ExceptionHandler(DataAccessException.class) public BizErrorResponse handleDataAccessException(DataAccessException e) { Throwable cause = e.getMostSpecificCause(); - return BizErrorResponse.valueOf( - new BizBaseException( + return BizErrorResponse.of( + ExceptionRule.SYSTEM_ERROR, Optional.ofNullable(cause) - .map(Throwable::getMessage) - .orElse(ExceptionRule.SYSTEM_ERROR.getMessage())) - .getErrorRule() + .map(Throwable::getMessage) + .orElse(ExceptionRule.SYSTEM_ERROR.getMessage()) ); } diff --git a/batch-quartz/src/main/java/com/spring/common/support/ResponseWrapper.java b/batch-quartz/src/main/java/com/spring/common/support/ResponseWrapper.java index 889f89a..9fb572a 100644 --- a/batch-quartz/src/main/java/com/spring/common/support/ResponseWrapper.java +++ b/batch-quartz/src/main/java/com/spring/common/support/ResponseWrapper.java @@ -118,7 +118,7 @@ public class ResponseWrapper implements ResponseBodyAdvice { .statusCode(errorRule.getStatus().value()) .path(path) .data(errorData.getErrors()) - .message(errorRule.getMessage()) + .message(errorData.getCustomMessage() != null ? errorData.getCustomMessage() : errorRule.getMessage()) .build(); } diff --git a/batch-quartz/src/main/java/com/spring/domain/user/error/UserRule.java b/batch-quartz/src/main/java/com/spring/domain/user/error/UserRule.java index 41c564d..ded2a3e 100644 --- a/batch-quartz/src/main/java/com/spring/domain/user/error/UserRule.java +++ b/batch-quartz/src/main/java/com/spring/domain/user/error/UserRule.java @@ -19,16 +19,6 @@ public enum UserRule implements ErrorRule { NEW_PASSWORD_SAME_AS_CURRENT(HttpStatus.BAD_REQUEST, "새 비밀번호는 현재 비밀번호와 달라야 합니다."); private final HttpStatus status; - private String message; - - UserRule(HttpStatus status, String message) { - this.status = status; - this.message = message; - } - - public UserRule message(final String message) { - this.message = message; - return this; - } + private final String message; } diff --git a/batch-quartz/src/main/java/com/spring/infra/security/error/SecurityExceptionRule.java b/batch-quartz/src/main/java/com/spring/infra/security/error/SecurityExceptionRule.java index 40f4ea7..4b84892 100644 --- a/batch-quartz/src/main/java/com/spring/infra/security/error/SecurityExceptionRule.java +++ b/batch-quartz/src/main/java/com/spring/infra/security/error/SecurityExceptionRule.java @@ -25,16 +25,6 @@ public enum SecurityExceptionRule implements ErrorRule { EXPIRED_JWT_ERROR(HttpStatus.UNAUTHORIZED, "토큰이 만료되었습니다. 다시 로그인해주세요."); private final HttpStatus status; - private String message; - - SecurityExceptionRule(HttpStatus status, String message) { - this.status = status; - this.message = message; - } - - public SecurityExceptionRule message(final String message) { - this.message = message; - return this; - } + private final String message; }