|
|
@@ -0,0 +1,49 @@
|
|
|
+package cn.seecoder.fdroidrepository.exception;
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+import cn.seecoder.fdroidrepository.result.SingleResult;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
+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 javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+
|
|
|
+@ControllerAdvice(annotations = Controller.class)
|
|
|
+public class ExceptionAdvice {
|
|
|
+
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(ExceptionAdvice.class);
|
|
|
+
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
+ public SingleResult<?> handlerBindException(MethodArgumentNotValidException e){
|
|
|
+ logger.error("服务器发生异常: "+e.getMessage());
|
|
|
+ BindingResult result = e.getBindingResult();
|
|
|
+ if(result.hasErrors()){
|
|
|
+ List<FieldError> fieldErrorList = result.getFieldErrors();
|
|
|
+ for(FieldError fieldError: fieldErrorList){
|
|
|
+ logger.error(fieldError.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return SingleResult.failure("500",e.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ @ExceptionHandler({Exception.class})
|
|
|
+ public SingleResult<?> handleException(Exception e, HttpServletRequest request, HttpServletResponse reponse) throws IOException {
|
|
|
+ logger.error("服务器发生异常: "+ e.getMessage());
|
|
|
+ for(StackTraceElement element: e.getStackTrace()) {
|
|
|
+ logger.error(element.toString());
|
|
|
+ }
|
|
|
+ return SingleResult.failure("500",e.getMessage());
|
|
|
+
|
|
|
+ }
|
|
|
+}
|