|
@@ -0,0 +1,49 @@
|
|
|
+package cn.rankin.apiweb.configuration;
|
|
|
+
|
|
|
+import cn.rankin.common.utils.api.model.APICode;
|
|
|
+import cn.rankin.common.utils.api.model.APIResult;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
+import org.springframework.web.bind.annotation.ResponseBody;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
|
|
+import org.springframework.web.servlet.NoHandlerFoundException;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.nio.file.AccessDeniedException;
|
|
|
+
|
|
|
+@RestController
|
|
|
+@ControllerAdvice
|
|
|
+public class GlobalExceptionHandler {
|
|
|
+ private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 系统异常处理,比如:404,500
|
|
|
+ * @param request
|
|
|
+ * @param e
|
|
|
+ * @return
|
|
|
+ * @throws Exception
|
|
|
+ */
|
|
|
+ @ExceptionHandler(value = Exception.class)
|
|
|
+ @ResponseBody
|
|
|
+ public APIResult defaultErrorHandler(HttpServletRequest request, Exception e) throws Exception {
|
|
|
+ logger.error("{}", e);
|
|
|
+
|
|
|
+ APIResult apiResult = new APIResult();
|
|
|
+ apiResult.setMessage(e.getMessage());
|
|
|
+ apiResult.setSuccess(false);
|
|
|
+
|
|
|
+ if (e instanceof NoHandlerFoundException) {
|
|
|
+ apiResult.setCode(APICode._C_NOT_FOUND);
|
|
|
+ }else if (e instanceof AccessDeniedException){
|
|
|
+ apiResult.setCode(APICode._C_ACCESS_DENIED);
|
|
|
+ }else if (e instanceof MethodArgumentTypeMismatchException){
|
|
|
+ apiResult.setCode(400);
|
|
|
+ }else {
|
|
|
+ apiResult.setCode(500);
|
|
|
+ }
|
|
|
+ return apiResult;
|
|
|
+ }
|
|
|
+}
|