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

pull/120/head v2.2.9
徐玉祥 2 years ago
parent 738ca7802e
commit 1477ed0ac9

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

Loading…
Cancel
Save