commit
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -34,9 +34,10 @@ public class BizErrorResponse extends ErrorResponse {
|
||||
* @param exceptionRule 발생한 예외의 오류 규칙
|
||||
* @param errors 발생한 오류 목록
|
||||
*/
|
||||
public BizErrorResponse(ErrorRule exceptionRule, List<RejectedValue> errors) {
|
||||
public BizErrorResponse(ErrorRule exceptionRule, List<RejectedValue> 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<RejectedValue> 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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -33,5 +33,7 @@ public abstract class ErrorResponse {
|
||||
* <p>비즈니스 로직에서 발생한 오류에 대한 세부 정보를 포함합니다.</p>
|
||||
*/
|
||||
protected List<RejectedValue> errors;
|
||||
|
||||
protected String customMessage;
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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())
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -118,7 +118,7 @@ public class ResponseWrapper implements ResponseBodyAdvice<Object> {
|
||||
.statusCode(errorRule.getStatus().value())
|
||||
.path(path)
|
||||
.data(errorData.getErrors())
|
||||
.message(errorRule.getMessage())
|
||||
.message(errorData.getCustomMessage() != null ? errorData.getCustomMessage() : errorRule.getMessage())
|
||||
.build();
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user