|
|
@@ -0,0 +1,94 @@
|
|
|
+package com.mrxu.framework.boot.handle;
|
|
|
+
|
|
|
+import com.mrxu.framework.boot.entity.BaseCode;
|
|
|
+import com.mrxu.framework.boot.entity.BusinessException;
|
|
|
+import com.mrxu.framework.boot.entity.ResponseObj;
|
|
|
+import com.mrxu.framework.boot.web.ServletUtils;
|
|
|
+import com.mrxu.framework.common.util.StrFunc;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.context.annotation.Import;
|
|
|
+import org.springframework.http.HttpStatus;
|
|
|
+import org.springframework.validation.BindingResult;
|
|
|
+import org.springframework.validation.FieldError;
|
|
|
+import org.springframework.web.bind.MethodArgumentNotValidException;
|
|
|
+import org.springframework.web.bind.MissingServletRequestParameterException;
|
|
|
+import org.springframework.web.bind.annotation.ControllerAdvice;
|
|
|
+import org.springframework.web.bind.annotation.ExceptionHandler;
|
|
|
+import org.springframework.web.bind.annotation.ResponseStatus;
|
|
|
+import org.springframework.web.bind.annotation.RestController;
|
|
|
+import org.springframework.web.servlet.ModelAndView;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.lang.annotation.*;
|
|
|
+
|
|
|
+@ControllerAdvice
|
|
|
+@RestController
|
|
|
+public class WebExceptionHandler {
|
|
|
+
|
|
|
+ @Retention(RetentionPolicy.RUNTIME)
|
|
|
+ @Target({ElementType.TYPE})
|
|
|
+ @Documented
|
|
|
+ @Import({WebExceptionHandler.class})
|
|
|
+ public @interface EnableWebExceptionHandler {
|
|
|
+ }
|
|
|
+
|
|
|
+ private Logger logger = LoggerFactory.getLogger(this.getClass());
|
|
|
+
|
|
|
+ @ExceptionHandler(Exception.class)
|
|
|
+ public Object handleException(Exception e) {
|
|
|
+ logger.error("系统错误:{}",e.getMessage(), e);
|
|
|
+ return rendError(BaseCode.ERROR);
|
|
|
+ }
|
|
|
+
|
|
|
+ @ExceptionHandler(BusinessException.class)
|
|
|
+ public Object handleLogicalException(BusinessException e) {
|
|
|
+ logger.warn(e.getMessage());
|
|
|
+ return rendError(new BaseCode(e.getCode(),e.getMsg()));
|
|
|
+ }
|
|
|
+
|
|
|
+ @ResponseStatus(HttpStatus.BAD_REQUEST)
|
|
|
+ @ExceptionHandler(MethodArgumentNotValidException.class)
|
|
|
+ public Object handleValidException(MethodArgumentNotValidException e) {
|
|
|
+ BindingResult result = e.getBindingResult();
|
|
|
+ FieldError error = result.getFieldError();
|
|
|
+ logger.warn("请求参数错误:{}",error.getDefaultMessage());
|
|
|
+ return rendError(BaseCode.ERR_PARAMS_VALID);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 缺少必要参数
|
|
|
+ * @param e
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @ExceptionHandler(MissingServletRequestParameterException.class)
|
|
|
+ public Object handleValidException(MissingServletRequestParameterException e) {
|
|
|
+ logger.warn("请求缺少参数:{}",e.getMessage());
|
|
|
+ return rendError(BaseCode.ERR_PARAMS_MISS);
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object rendError(BaseCode code) {
|
|
|
+ HttpServletRequest request = ServletUtils.getRequest();
|
|
|
+ if(ServletUtils.isAjaxRequest(request)) {
|
|
|
+ return new ResponseObj<Object>(code);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ ModelAndView modelAndView = new ModelAndView();
|
|
|
+ modelAndView.addObject("message", code.getMsg());
|
|
|
+ modelAndView.setViewName("error/500.html");
|
|
|
+
|
|
|
+ //未登录
|
|
|
+ if(BaseCode.ERR_NOTAUTH.getCode() == code.getCode()) {
|
|
|
+ StringBuffer source = request.getRequestURL();
|
|
|
+ String queryString = request.getQueryString();
|
|
|
+ if(StrFunc.isNotEmpty(queryString)) {
|
|
|
+ source.append("?").append(queryString);
|
|
|
+ }
|
|
|
+ modelAndView.addObject("sourceUrl",source);
|
|
|
+ modelAndView.setViewName("login");
|
|
|
+ }
|
|
|
+ return modelAndView;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|