|
|
@@ -1,57 +0,0 @@
|
|
|
-package cn.seecoder.data.server.controller;
|
|
|
-
|
|
|
-import cn.seecoder.data.server.controller.response.ErrorResponse;
|
|
|
-import cn.seecoder.data.server.exception.SeecDataException;
|
|
|
-import lombok.extern.slf4j.Slf4j;
|
|
|
-import org.springframework.http.HttpStatus;
|
|
|
-import org.springframework.http.ResponseEntity;
|
|
|
-import org.springframework.validation.BindException;
|
|
|
-import org.springframework.web.HttpRequestMethodNotSupportedException;
|
|
|
-import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
-import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
-import org.springframework.web.bind.annotation.RestControllerAdvice;
|
|
|
-import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
|
-
|
|
|
-import javax.validation.ConstraintViolationException;
|
|
|
-import java.util.Objects;
|
|
|
-
|
|
|
-@Slf4j
|
|
|
-@RestControllerAdvice
|
|
|
-public class ControllerAdvice {
|
|
|
- @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
- public ResponseEntity<ErrorResponse> handleMethodArgumentNotValidatedException(MethodArgumentNotValidException e) {
|
|
|
- return ResponseEntity.badRequest().body(ErrorResponse.of(Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage()));
|
|
|
- }
|
|
|
-
|
|
|
- @ExceptionHandler(MethodArgumentTypeMismatchException.class)
|
|
|
- public ResponseEntity<ErrorResponse> handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e) {
|
|
|
- log.error(e.getParameter().getParameterName(), e);
|
|
|
- return ResponseEntity.badRequest().body(ErrorResponse.of("路径参数错误"));
|
|
|
- }
|
|
|
-
|
|
|
- @ExceptionHandler(ConstraintViolationException.class)
|
|
|
- public ResponseEntity<ErrorResponse> handleConstraintViolationException(ConstraintViolationException e) {
|
|
|
- return ResponseEntity.badRequest().body(ErrorResponse.of(e.getLocalizedMessage()));
|
|
|
- }
|
|
|
-
|
|
|
- @ExceptionHandler(BindException.class)
|
|
|
- public ResponseEntity<ErrorResponse> handleBindException(BindException e) {
|
|
|
- return ResponseEntity.badRequest().body(ErrorResponse.of(Objects.requireNonNull(e.getFieldError()).getDefaultMessage()));
|
|
|
- }
|
|
|
-
|
|
|
- @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
|
|
|
- public ResponseEntity<ErrorResponse> handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
|
|
|
- return ResponseEntity.status(405).body(ErrorResponse.of(e.getLocalizedMessage()));
|
|
|
- }
|
|
|
-
|
|
|
- @ExceptionHandler(SeecDataException.class)
|
|
|
- public ResponseEntity<ErrorResponse> handleHelperException(SeecDataException e) {
|
|
|
- return ResponseEntity.status(e.getType().getStatus()).body(ErrorResponse.of(e.getMsg()));
|
|
|
- }
|
|
|
-
|
|
|
- @ExceptionHandler(Exception.class)
|
|
|
- public ResponseEntity<ErrorResponse> handleException(Exception e) {
|
|
|
- log.error(e.getLocalizedMessage(), e);
|
|
|
- return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ErrorResponse.of("系统异常,请重试"));
|
|
|
- }
|
|
|
-}
|