commit
This commit is contained in:
@@ -24,6 +24,8 @@ public class BizBaseException extends RuntimeException {
|
|||||||
*/
|
*/
|
||||||
private final ErrorRule errorRule;
|
private final ErrorRule errorRule;
|
||||||
|
|
||||||
|
private final String customMessage;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 기본 생성자입니다.
|
* 기본 생성자입니다.
|
||||||
*
|
*
|
||||||
@@ -32,6 +34,7 @@ public class BizBaseException extends RuntimeException {
|
|||||||
public BizBaseException() {
|
public BizBaseException() {
|
||||||
super(ExceptionRule.SYSTEM_ERROR.getMessage());
|
super(ExceptionRule.SYSTEM_ERROR.getMessage());
|
||||||
this.errorRule = ExceptionRule.SYSTEM_ERROR;
|
this.errorRule = ExceptionRule.SYSTEM_ERROR;
|
||||||
|
this.customMessage = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -42,11 +45,19 @@ public class BizBaseException extends RuntimeException {
|
|||||||
public BizBaseException(ErrorRule exceptionRule) {
|
public BizBaseException(ErrorRule exceptionRule) {
|
||||||
super(exceptionRule.getMessage());
|
super(exceptionRule.getMessage());
|
||||||
this.errorRule = exceptionRule;
|
this.errorRule = exceptionRule;
|
||||||
|
this.customMessage = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BizBaseException(ErrorRule errorRule, String message) {
|
||||||
|
super(message);
|
||||||
|
this.errorRule = errorRule;
|
||||||
|
this.customMessage = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
public BizBaseException(String message) {
|
public BizBaseException(String message) {
|
||||||
super(message);
|
super(ExceptionRule.SYSTEM_ERROR.getMessage());
|
||||||
this.errorRule = ExceptionRule.SYSTEM_ERROR.message(message);
|
this.errorRule = ExceptionRule.SYSTEM_ERROR;
|
||||||
|
this.customMessage = message;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,9 +34,10 @@ public class BizErrorResponse extends ErrorResponse {
|
|||||||
* @param exceptionRule 발생한 예외의 오류 규칙
|
* @param exceptionRule 발생한 예외의 오류 규칙
|
||||||
* @param errors 발생한 오류 목록
|
* @param errors 발생한 오류 목록
|
||||||
*/
|
*/
|
||||||
public BizErrorResponse(ErrorRule exceptionRule, List<RejectedValue> errors) {
|
public BizErrorResponse(ErrorRule exceptionRule, List<RejectedValue> errors, String customMessage) {
|
||||||
super(exceptionRule);
|
super(exceptionRule);
|
||||||
this.errors = errors;
|
this.errors = errors;
|
||||||
|
this.customMessage = customMessage;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -47,8 +48,12 @@ public class BizErrorResponse extends ErrorResponse {
|
|||||||
* @param exceptionRule 발생한 예외의 오류 규칙
|
* @param exceptionRule 발생한 예외의 오류 규칙
|
||||||
* @return 생성된 BizErrorResponse 객체
|
* @return 생성된 BizErrorResponse 객체
|
||||||
*/
|
*/
|
||||||
public static BizErrorResponse valueOf(ErrorRule exceptionRule) {
|
public static BizErrorResponse fromErrorRule(ErrorRule exceptionRule) {
|
||||||
return new BizErrorResponse(exceptionRule, null);
|
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()
|
List<RejectedValue> errors = fieldErrors.stream()
|
||||||
.map(fieldError -> RejectedValue.of(fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage()))
|
.map(fieldError -> RejectedValue.of(fieldError.getField(), fieldError.getRejectedValue(), fieldError.getDefaultMessage()))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
return new BizErrorResponse(ExceptionRule.UNPROCESSABLE_ENTITY, errors);
|
return new BizErrorResponse(ExceptionRule.UNPROCESSABLE_ENTITY, errors, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,4 +34,6 @@ public abstract class ErrorResponse {
|
|||||||
*/
|
*/
|
||||||
protected List<RejectedValue> errors;
|
protected List<RejectedValue> errors;
|
||||||
|
|
||||||
|
protected String customMessage;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,16 +30,6 @@ public enum ExceptionRule implements ErrorRule {
|
|||||||
UNPROCESSABLE_ENTITY(HttpStatus.UNPROCESSABLE_ENTITY, "요청을 처리할 수 없습니다.");
|
UNPROCESSABLE_ENTITY(HttpStatus.UNPROCESSABLE_ENTITY, "요청을 처리할 수 없습니다.");
|
||||||
|
|
||||||
private final HttpStatus status;
|
private final HttpStatus status;
|
||||||
private String message;
|
private final String message;
|
||||||
|
|
||||||
ExceptionRule(HttpStatus status, String message) {
|
|
||||||
this.status = status;
|
|
||||||
this.message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ExceptionRule message(final String message) {
|
|
||||||
this.message = message;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -66,7 +66,7 @@ public class GlobalErrorController implements ErrorController {
|
|||||||
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
|
Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE);
|
||||||
int statusCode = status != null ? Integer.parseInt(status.toString()) : HttpStatus.INTERNAL_SERVER_ERROR.value();
|
int statusCode = status != null ? Integer.parseInt(status.toString()) : HttpStatus.INTERNAL_SERVER_ERROR.value();
|
||||||
HttpStatus httpStatus = HttpStatus.valueOf(statusCode);
|
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)
|
@ExceptionHandler(BizBaseException.class)
|
||||||
public BizErrorResponse handleCustomException(BizBaseException e) {
|
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)
|
@ExceptionHandler(Exception.class)
|
||||||
public BizErrorResponse handleException(Exception e) {
|
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)
|
@ExceptionHandler(DataAccessException.class)
|
||||||
public BizErrorResponse handleDataAccessException(DataAccessException e) {
|
public BizErrorResponse handleDataAccessException(DataAccessException e) {
|
||||||
Throwable cause = e.getMostSpecificCause();
|
Throwable cause = e.getMostSpecificCause();
|
||||||
return BizErrorResponse.valueOf(
|
return BizErrorResponse.of(
|
||||||
new BizBaseException(
|
ExceptionRule.SYSTEM_ERROR,
|
||||||
Optional.ofNullable(cause)
|
Optional.ofNullable(cause)
|
||||||
.map(Throwable::getMessage)
|
.map(Throwable::getMessage)
|
||||||
.orElse(ExceptionRule.SYSTEM_ERROR.getMessage()))
|
.orElse(ExceptionRule.SYSTEM_ERROR.getMessage())
|
||||||
.getErrorRule()
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -118,7 +118,7 @@ public class ResponseWrapper implements ResponseBodyAdvice<Object> {
|
|||||||
.statusCode(errorRule.getStatus().value())
|
.statusCode(errorRule.getStatus().value())
|
||||||
.path(path)
|
.path(path)
|
||||||
.data(errorData.getErrors())
|
.data(errorData.getErrors())
|
||||||
.message(errorRule.getMessage())
|
.message(errorData.getCustomMessage() != null ? errorData.getCustomMessage() : errorRule.getMessage())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,16 +19,6 @@ public enum UserRule implements ErrorRule {
|
|||||||
NEW_PASSWORD_SAME_AS_CURRENT(HttpStatus.BAD_REQUEST, "새 비밀번호는 현재 비밀번호와 달라야 합니다.");
|
NEW_PASSWORD_SAME_AS_CURRENT(HttpStatus.BAD_REQUEST, "새 비밀번호는 현재 비밀번호와 달라야 합니다.");
|
||||||
|
|
||||||
private final HttpStatus status;
|
private final HttpStatus status;
|
||||||
private String message;
|
private final String message;
|
||||||
|
|
||||||
UserRule(HttpStatus status, String message) {
|
|
||||||
this.status = status;
|
|
||||||
this.message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public UserRule message(final String message) {
|
|
||||||
this.message = message;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,16 +25,6 @@ public enum SecurityExceptionRule implements ErrorRule {
|
|||||||
EXPIRED_JWT_ERROR(HttpStatus.UNAUTHORIZED, "토큰이 만료되었습니다. 다시 로그인해주세요.");
|
EXPIRED_JWT_ERROR(HttpStatus.UNAUTHORIZED, "토큰이 만료되었습니다. 다시 로그인해주세요.");
|
||||||
|
|
||||||
private final HttpStatus status;
|
private final HttpStatus status;
|
||||||
private String message;
|
private final String message;
|
||||||
|
|
||||||
SecurityExceptionRule(HttpStatus status, String message) {
|
|
||||||
this.status = status;
|
|
||||||
this.message = message;
|
|
||||||
}
|
|
||||||
|
|
||||||
public SecurityExceptionRule message(final String message) {
|
|
||||||
this.message = message;
|
|
||||||
return this;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user