|
@@ -1,91 +1,110 @@
|
|
|
package com.its.api.its.exception;
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.context.support.DefaultMessageSourceResolvable;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
+import org.springframework.validation.FieldError;
|
|
|
import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
import java.time.LocalDateTime;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
import java.util.List;
|
|
|
-import java.util.stream.Collectors;
|
|
|
+import java.util.NoSuchElementException;
|
|
|
|
|
|
@Slf4j
|
|
|
@ControllerAdvice
|
|
|
public class ExceptionControllerAdvisor {//} extends ResponseEntityExceptionHandler {
|
|
|
|
|
|
- /*@Override
|
|
|
- protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
|
|
|
-
|
|
|
- List<String> errorList = ex
|
|
|
- .getBindingResult()
|
|
|
- .getFieldErrors()
|
|
|
- .stream()
|
|
|
- .map(DefaultMessageSourceResolvable::getDefaultMessage)
|
|
|
- .collect(Collectors.toList());
|
|
|
-
|
|
|
- return new ResponseEntity<>(
|
|
|
- NotValidExceptionResponse.builder()
|
|
|
- .timestamp(LocalDateTime.now())
|
|
|
- .status(HttpStatus.BAD_REQUEST.value())
|
|
|
- .title("Arguments Not Valid")
|
|
|
- .developerMessage(ex.getClass().getName())
|
|
|
- .err(errorList)
|
|
|
- .build(), HttpStatus.BAD_REQUEST
|
|
|
- );
|
|
|
- }*/
|
|
|
-
|
|
|
+ /**
|
|
|
+ * Validation error
|
|
|
+ * @param ex
|
|
|
+ * @param request
|
|
|
+ * @return
|
|
|
+ */
|
|
|
@ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
- public ResponseEntity<ErrorResponse> methodValidException(MethodArgumentNotValidException ex, HttpServletRequest request) {
|
|
|
- log.warn("MethodArgumentNotValidException 발생!!! url:{}, trace:{}",request.getRequestURI(), ex.getStackTrace());
|
|
|
- List<String> errorList = ex
|
|
|
- .getBindingResult()
|
|
|
- .getFieldErrors()
|
|
|
- .stream()
|
|
|
- .map(DefaultMessageSourceResolvable::getDefaultMessage)
|
|
|
- .collect(Collectors.toList());
|
|
|
- //ErrorResponse errorResponse = makeErrorResponse(e.getBindingResult());
|
|
|
- //return new ResponseEntity<ErrorResponse>(errorResponse, HttpStatus.BAD_REQUEST);
|
|
|
- return new ResponseEntity<>(
|
|
|
- ErrorResponse.builder()
|
|
|
- .timestamp(LocalDateTime.now())
|
|
|
- .status(HttpStatus.BAD_REQUEST.value())
|
|
|
- .title("Arguments Not Valid")
|
|
|
- .message(ex.getClass().getName())
|
|
|
- .errors(errorList)
|
|
|
- .build(), HttpStatus.BAD_REQUEST);
|
|
|
- }
|
|
|
-/*
|
|
|
-
|
|
|
- private ErrorResponse makeErrorResponse(BindingResult bindingResult) {
|
|
|
- String code = "";
|
|
|
- String description = "";
|
|
|
- String detail = "";
|
|
|
-
|
|
|
- //에러가 있다면
|
|
|
- if (bindingResult.hasErrors()) {
|
|
|
- //DTO에 설정한 meaasge값을 가져온다
|
|
|
- detail = bindingResult.getFieldError().getDefaultMessage();
|
|
|
-
|
|
|
- //DTO에 유효성체크를 걸어놓은 어노테이션명을 가져온다.
|
|
|
- String bindResultCode = bindingResult.getFieldError().getCode();
|
|
|
-
|
|
|
- switch (bindResultCode){
|
|
|
- case "NotNull":
|
|
|
- //code = ErrorCode.NOT_NULL.getCode();
|
|
|
- //description = ErrorCode.NOT_NULL.getDescription();
|
|
|
- break;
|
|
|
- case "Min":
|
|
|
- //code = ErrorCode.MIN_VALUE.getCode();
|
|
|
- //description = ErrorCode.MIN_VALUE.getDescription();
|
|
|
- break;
|
|
|
- }
|
|
|
+ public ResponseEntity<ErrorResponse> methodValidException(HttpServletRequest request, MethodArgumentNotValidException ex) {
|
|
|
+ List<String> errorList = new ArrayList<>();
|
|
|
+ BindingResult bindingResult = ex.getBindingResult();
|
|
|
+ for (FieldError fieldError : bindingResult.getFieldErrors()) {
|
|
|
+ StringBuilder builder = new StringBuilder();
|
|
|
+ builder.append("[");
|
|
|
+ builder.append(fieldError.getField());
|
|
|
+ builder.append("](은)는 ");
|
|
|
+ builder.append(fieldError.getDefaultMessage());
|
|
|
+ builder.append(" 입력된 값: [");
|
|
|
+ builder.append(fieldError.getRejectedValue());
|
|
|
+ builder.append("]");
|
|
|
+ errorList.add(builder.toString());
|
|
|
}
|
|
|
- return new ErrorResponse(0, code, null);
|
|
|
+// List<String> errorList = ex
|
|
|
+// .getBindingResult()
|
|
|
+// .getFieldErrors()
|
|
|
+// .stream()
|
|
|
+// .map(DefaultMessageSourceResolvable::getDefaultMessage)
|
|
|
+// .collect(Collectors.toList());
|
|
|
+ ErrorResponse response = ErrorResponse.builder()
|
|
|
+ .timestamp(LocalDateTime.now())
|
|
|
+ .status(HttpStatus.BAD_REQUEST.value())
|
|
|
+ .title("Arguments Not Valid(Bad Request)")
|
|
|
+ .message("요청 데이터가 유효하지 않습니다.")
|
|
|
+ .errors(errorList)
|
|
|
+ .path(request.getRequestURI())
|
|
|
+ .build();
|
|
|
+ log.error("{}", response.toString());
|
|
|
+ return new ResponseEntity<>(response, HttpStatus.BAD_REQUEST);
|
|
|
}
|
|
|
-*/
|
|
|
|
|
|
+ /**
|
|
|
+ * Data not found
|
|
|
+ * @param request
|
|
|
+ * @param ex
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ExceptionHandler({NoSuchElementException.class})
|
|
|
+ public ResponseEntity<ErrorResponse> exceptionHandler(HttpServletRequest request, final NoSuchElementException ex) {
|
|
|
+ List<String> errorList = new ArrayList<>(Collections.singletonList(ex.getMessage()));
|
|
|
+ ErrorResponse response = ErrorResponse.builder()
|
|
|
+ .timestamp(LocalDateTime.now())
|
|
|
+ .status(HttpStatus.NOT_FOUND.value())
|
|
|
+ .title("Data Not Found")
|
|
|
+ .message(ex.getMessage())
|
|
|
+ .path(request.getRequestURI())
|
|
|
+ .errors(errorList)
|
|
|
+ .build();
|
|
|
+ log.error("{}", response.toString());
|
|
|
+ return new ResponseEntity<>(response, HttpStatus.NOT_FOUND);
|
|
|
+ }
|
|
|
+ @ExceptionHandler({RuntimeException.class})
|
|
|
+ public ResponseEntity<ErrorResponse> exceptionHandler(HttpServletRequest request, final RuntimeException ex) {
|
|
|
+ List<String> errorList = new ArrayList<>(Collections.singletonList(ex.getMessage()));
|
|
|
+ ErrorResponse response = ErrorResponse.builder()
|
|
|
+ .timestamp(LocalDateTime.now())
|
|
|
+ .status(HttpStatus.EXPECTATION_FAILED.value())
|
|
|
+ .title("RuntimeException")
|
|
|
+ .message(ex.getMessage())
|
|
|
+ .path(request.getRequestURI())
|
|
|
+ .errors(errorList)
|
|
|
+ .build();
|
|
|
+ log.error("{}", response.toString());
|
|
|
+ return new ResponseEntity<>(response, HttpStatus.EXPECTATION_FAILED);
|
|
|
+ }
|
|
|
+ @ExceptionHandler({Exception.class})
|
|
|
+ public ResponseEntity<ErrorResponse> exceptionHandler(HttpServletRequest request, final Exception ex) {
|
|
|
+ List<String> errorList = new ArrayList<>(Collections.singletonList(ex.getMessage()));
|
|
|
+ ErrorResponse response = ErrorResponse.builder()
|
|
|
+ .timestamp(LocalDateTime.now())
|
|
|
+ .status(HttpStatus.EXPECTATION_FAILED.value())
|
|
|
+ .title("Exception")
|
|
|
+ .message(ex.getMessage())
|
|
|
+ .path(request.getRequestURI())
|
|
|
+ .errors(errorList)
|
|
|
+ .build();
|
|
|
+ log.error("{}", response.toString());
|
|
|
+ return new ResponseEntity<>(response, HttpStatus.EXPECTATION_FAILED);
|
|
|
+ }
|
|
|
}
|