feat(order, store, user): 전역 에러 핸들링 클래스 추가
- RestControllerAdvice 추가 - 사용자 에러 클래스 (CustomException) - Spring Validation Exception (BindException, MethodArgumentNotValidException)
This commit is contained in:
@@ -30,7 +30,7 @@ public class OrderController {
|
|||||||
private final OrderService orderService;
|
private final OrderService orderService;
|
||||||
|
|
||||||
@GetMapping("/orderMain")
|
@GetMapping("/orderMain")
|
||||||
public ResponseEntity orderMain(@Valid OrderController.OrderMainRequest orderMainRequest) {
|
public ResponseEntity orderMain(@Valid OrderMainRequest orderMainRequest) {
|
||||||
|
|
||||||
List<OrderDto> orderDto = orderService.findOrderMain(orderMainRequest.convertOrderTimeToLocalDate());
|
List<OrderDto> orderDto = orderService.findOrderMain(orderMainRequest.convertOrderTimeToLocalDate());
|
||||||
|
|
||||||
@@ -45,7 +45,8 @@ public class OrderController {
|
|||||||
@Data @NoArgsConstructor @AllArgsConstructor
|
@Data @NoArgsConstructor @AllArgsConstructor
|
||||||
static class OrderMainRequest {
|
static class OrderMainRequest {
|
||||||
// yyyy-mm-dd 형태를 가지는 패턴 조사
|
// yyyy-mm-dd 형태를 가지는 패턴 조사
|
||||||
@Pattern(regexp = "^(19|20)\\d{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[0-1])$")
|
@Pattern(regexp = "^(19|20)\\d{2}-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[0-1])$",
|
||||||
|
message = "YYYY-MM-DD 형식에 맞게 작성되지 않았습니다.")
|
||||||
private String orderTime;
|
private String orderTime;
|
||||||
|
|
||||||
public LocalDate convertOrderTimeToLocalDate() {
|
public LocalDate convertOrderTimeToLocalDate() {
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.justpickup.orderservice.global.exception;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class CustomException extends RuntimeException {
|
||||||
|
|
||||||
|
private ErrorEnum errorEnum;
|
||||||
|
|
||||||
|
protected CustomException(ErrorEnum errorEnum) {
|
||||||
|
super(errorEnum.getMessage());
|
||||||
|
this.errorEnum = errorEnum;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.justpickup.orderservice.global.exception;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||||
|
public enum ErrorEnum {
|
||||||
|
|
||||||
|
NOT_EXIST_ORDER(HttpStatus.CONFLICT, "존재하지 않은 주문입니다.");
|
||||||
|
|
||||||
|
private final HttpStatus httpStatus;
|
||||||
|
private final String message;
|
||||||
|
}
|
||||||
@@ -0,0 +1,73 @@
|
|||||||
|
package com.justpickup.orderservice.global.exception;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.validation.BindException;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
@Slf4j
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(CustomException.class)
|
||||||
|
public ResponseEntity<ErrorBody> customExceptionHandler(CustomException ce) {
|
||||||
|
ErrorEnum errorEnum = ce.getErrorEnum();
|
||||||
|
|
||||||
|
log.warn("##################################################");
|
||||||
|
log.warn("## CustomException = {}", errorEnum);
|
||||||
|
log.warn("##################################################");
|
||||||
|
|
||||||
|
HttpStatus errorHttpStatus = errorEnum.getHttpStatus();
|
||||||
|
|
||||||
|
return ResponseEntity.status(errorHttpStatus)
|
||||||
|
.body(new ErrorBody(errorEnum.getMessage(), errorHttpStatus.getReasonPhrase()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(BindException.class)
|
||||||
|
public ResponseEntity<ErrorBody> bindExceptionHandler(BindException exception) {
|
||||||
|
return getValidationErrorBody(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
public ResponseEntity<ErrorBody> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {
|
||||||
|
return getValidationErrorBody(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<ErrorBody> getValidationErrorBody(BindException exception) {
|
||||||
|
BindingResult bindingResult = exception.getBindingResult();
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
bindingResult.getFieldErrors()
|
||||||
|
.forEach(fieldError -> {
|
||||||
|
builder.append("[");
|
||||||
|
builder.append(fieldError.getField());
|
||||||
|
builder.append("](은)는 ");
|
||||||
|
builder.append(fieldError.getDefaultMessage());
|
||||||
|
builder.append(" 입력된 값: [");
|
||||||
|
builder.append(fieldError.getRejectedValue());
|
||||||
|
builder.append("]");
|
||||||
|
});
|
||||||
|
|
||||||
|
ErrorBody errorBody = new ErrorBody(builder.toString(), HttpStatus.BAD_REQUEST.getReasonPhrase());
|
||||||
|
|
||||||
|
log.warn("##################################################");
|
||||||
|
log.warn("## getValidationErrorBody = {}", errorBody);
|
||||||
|
log.warn("##################################################");
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body(errorBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data @NoArgsConstructor @AllArgsConstructor
|
||||||
|
static class ErrorBody {
|
||||||
|
private String message;
|
||||||
|
private String httpStatus;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.justpickup.storeservice.global.exception;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class CustomException extends RuntimeException {
|
||||||
|
|
||||||
|
private ErrorEnum errorEnum;
|
||||||
|
|
||||||
|
protected CustomException(ErrorEnum errorEnum) {
|
||||||
|
super(errorEnum.getMessage());
|
||||||
|
this.errorEnum = errorEnum;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.justpickup.storeservice.global.exception;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||||
|
public enum ErrorEnum {
|
||||||
|
|
||||||
|
NOT_EXIST_ITEM(HttpStatus.CONFLICT, "존재하지 않은 상품입니다.");
|
||||||
|
|
||||||
|
private final HttpStatus httpStatus;
|
||||||
|
private final String message;
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package com.justpickup.storeservice.global.exception;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.validation.BindException;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
@Slf4j
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(CustomException.class)
|
||||||
|
public ResponseEntity<ErrorBody> customExceptionHandler(CustomException ce) {
|
||||||
|
ErrorEnum errorEnum = ce.getErrorEnum();
|
||||||
|
|
||||||
|
log.warn("##################################################");
|
||||||
|
log.warn("## CustomException = {}", errorEnum);
|
||||||
|
log.warn("##################################################");
|
||||||
|
|
||||||
|
HttpStatus errorHttpStatus = errorEnum.getHttpStatus();
|
||||||
|
|
||||||
|
return ResponseEntity.status(errorHttpStatus)
|
||||||
|
.body(new ErrorBody(errorEnum.getMessage(), errorHttpStatus.getReasonPhrase()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(BindException.class)
|
||||||
|
public ResponseEntity<ErrorBody> bindExceptionHandler(BindException exception) {
|
||||||
|
return getValidationErrorBody(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
public ResponseEntity<ErrorBody> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {
|
||||||
|
return getValidationErrorBody(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<ErrorBody> getValidationErrorBody(BindException exception) {
|
||||||
|
BindingResult bindingResult = exception.getBindingResult();
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
bindingResult.getFieldErrors()
|
||||||
|
.forEach(fieldError -> {
|
||||||
|
builder.append("[");
|
||||||
|
builder.append(fieldError.getField());
|
||||||
|
builder.append("](은)는 ");
|
||||||
|
builder.append(fieldError.getDefaultMessage());
|
||||||
|
builder.append(" 입력된 값: [");
|
||||||
|
builder.append(fieldError.getRejectedValue());
|
||||||
|
builder.append("]");
|
||||||
|
});
|
||||||
|
|
||||||
|
ErrorBody errorBody = new ErrorBody(builder.toString(), HttpStatus.BAD_REQUEST.getReasonPhrase());
|
||||||
|
|
||||||
|
log.warn("##################################################");
|
||||||
|
log.warn("## getValidationErrorBody = {}", errorBody);
|
||||||
|
log.warn("##################################################");
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body(errorBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data @NoArgsConstructor @AllArgsConstructor
|
||||||
|
static class ErrorBody {
|
||||||
|
private String message;
|
||||||
|
private String httpStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.justpickup.userservice.global.exception;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
public class CustomException extends RuntimeException {
|
||||||
|
|
||||||
|
private ErrorEnum errorEnum;
|
||||||
|
|
||||||
|
protected CustomException(ErrorEnum errorEnum) {
|
||||||
|
super(errorEnum.getMessage());
|
||||||
|
this.errorEnum = errorEnum;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
package com.justpickup.userservice.global.exception;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Getter;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@AllArgsConstructor
|
||||||
|
@JsonFormat(shape = JsonFormat.Shape.OBJECT)
|
||||||
|
public enum ErrorEnum {
|
||||||
|
|
||||||
|
NOT_EXIST_USER(HttpStatus.CONFLICT, "존재하지 않은 사용자입니다.");
|
||||||
|
|
||||||
|
private final HttpStatus httpStatus;
|
||||||
|
private final String message;
|
||||||
|
}
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
package com.justpickup.userservice.global.exception;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.validation.BindException;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
import org.springframework.validation.FieldError;
|
||||||
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||||
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||||
|
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||||
|
|
||||||
|
@RestControllerAdvice
|
||||||
|
@Slf4j
|
||||||
|
public class GlobalExceptionHandler {
|
||||||
|
|
||||||
|
@ExceptionHandler(CustomException.class)
|
||||||
|
public ResponseEntity<ErrorBody> customExceptionHandler(CustomException ce) {
|
||||||
|
ErrorEnum errorEnum = ce.getErrorEnum();
|
||||||
|
|
||||||
|
log.warn("##################################################");
|
||||||
|
log.warn("## CustomException = {}", errorEnum);
|
||||||
|
log.warn("##################################################");
|
||||||
|
|
||||||
|
HttpStatus errorHttpStatus = errorEnum.getHttpStatus();
|
||||||
|
|
||||||
|
return ResponseEntity.status(errorHttpStatus)
|
||||||
|
.body(new ErrorBody(errorEnum.getMessage(), errorHttpStatus.getReasonPhrase()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(BindException.class)
|
||||||
|
public ResponseEntity<ErrorBody> bindExceptionHandler(BindException exception) {
|
||||||
|
return getValidationErrorBody(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
||||||
|
public ResponseEntity<ErrorBody> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException exception) {
|
||||||
|
return getValidationErrorBody(exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ResponseEntity<ErrorBody> getValidationErrorBody(BindException exception) {
|
||||||
|
BindingResult bindingResult = exception.getBindingResult();
|
||||||
|
|
||||||
|
StringBuilder builder = new StringBuilder();
|
||||||
|
bindingResult.getFieldErrors()
|
||||||
|
.forEach(fieldError -> {
|
||||||
|
builder.append("[");
|
||||||
|
builder.append(fieldError.getField());
|
||||||
|
builder.append("](은)는 ");
|
||||||
|
builder.append(fieldError.getDefaultMessage());
|
||||||
|
builder.append(" 입력된 값: [");
|
||||||
|
builder.append(fieldError.getRejectedValue());
|
||||||
|
builder.append("]");
|
||||||
|
});
|
||||||
|
|
||||||
|
ErrorBody errorBody = new ErrorBody(builder.toString(), HttpStatus.BAD_REQUEST.getReasonPhrase());
|
||||||
|
|
||||||
|
log.warn("##################################################");
|
||||||
|
log.warn("## getValidationErrorBody = {}", errorBody);
|
||||||
|
log.warn("##################################################");
|
||||||
|
|
||||||
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST)
|
||||||
|
.body(errorBody);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Data @NoArgsConstructor @AllArgsConstructor
|
||||||
|
static class ErrorBody {
|
||||||
|
private String message;
|
||||||
|
private String httpStatus;
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user