Browse Source

【更新】优化全局异常页面处理器的逻辑判断

pull/120/head v2.2.9
徐玉祥 2 years ago
parent
commit
1477ed0ac9
  1. 28
      snowy-web-app/src/main/java/vip/xiaonuo/core/handler/GlobalErrorViewController.java

28
snowy-web-app/src/main/java/vip/xiaonuo/core/handler/GlobalErrorViewController.java

@ -18,6 +18,7 @@ import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import vip.xiaonuo.common.exception.CommonException;
import vip.xiaonuo.common.pojo.CommonResult;
import javax.servlet.http.HttpServletRequest;
@ -43,19 +44,22 @@ public class GlobalErrorViewController {
CommonResult<String> commonResult = new CommonResult<>(404, "路径不存在", null);
Object model = request.getAttribute("model");
if(ObjectUtil.isNotEmpty(model)) {
JSONObject errorObj = JSONUtil.parseObj(model);
Integer code = errorObj.getInt("code");
String msg = errorObj.getStr("msg");
if(ObjectUtil.isAllNotEmpty(code, msg)) {
commonResult.setCode(code).setMsg(msg);
} else if(ObjectUtil.isNotEmpty(msg)) {
commonResult = CommonResult.error(msg);
} else {
commonResult = CommonResult.error();
}
if(model instanceof Exception){
Exception e = (Exception) model;
log.error(">>> 服务器未知异常,具体信息:", e);
if(model instanceof CommonException) {
JSONObject errorObj = JSONUtil.parseObj(model);
Integer code = errorObj.getInt("code");
String msg = errorObj.getStr("msg");
if(ObjectUtil.isAllNotEmpty(code, msg)) {
commonResult.setCode(code).setMsg(msg);
} else if(ObjectUtil.isNotEmpty(msg)) {
commonResult = CommonResult.error(msg);
} else {
commonResult = CommonResult.error();
}
} else {
commonResult = CommonResult.error();
log.error(">>> 服务器未知异常,具体信息:", (Exception) model);
}
}
}
return commonResult;

Loading…
Cancel
Save