完善异常处理

This commit is contained in:
2023-07-31 23:18:22 +08:00
parent 2c685d933d
commit c10e46fe1e
5 changed files with 79 additions and 20 deletions

View File

@@ -6,12 +6,12 @@ import cn.lihongjie.coal.annotation.SysAdmin;
import cn.lihongjie.coal.annotation.SysLog;
import cn.lihongjie.coal.common.Ctx;
import cn.lihongjie.coal.common.RequestUtils;
import cn.lihongjie.coal.dto.R;
import cn.lihongjie.coal.entity.*;
import cn.lihongjie.coal.exception.BizException;
import cn.lihongjie.coal.service.SessionService;
import cn.lihongjie.coal.service.SysLogService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
@@ -47,6 +47,7 @@ public class ControllerAop {
SessionService sessionService;
@SneakyThrows
@Around("controllerMethods()")
public Object call(ProceedingJoinPoint proceedingJoinPoint) {
@@ -59,12 +60,12 @@ public class ControllerAop {
if (!Ctx.isLoggedIn()) {
Anonymous anonymous = ObjectUtils.defaultIfNull(AnnotationUtils.findAnnotation(method, Anonymous.class), AnnotationUtils.findAnnotation(method.getClass(), Anonymous.class));
Anonymous anonymous = ObjectUtils.defaultIfNull(AnnotationUtils.findAnnotation(method, Anonymous.class), AnnotationUtils.findAnnotation(method.getDeclaringClass(), Anonymous.class));
if (anonymous == null || !anonymous.value()) {
return R.fail("invalidToken", "登录状态失效,请重新登录");
throw new BizException("invalidToken", "登录状态失效,请重新登录");
}
@@ -80,7 +81,7 @@ public class ControllerAop {
if (orgAdmin != null && orgAdmin.value() && !Ctx.isOrgAdmin()) {
return R.fail("invalidAccess", "非法访问,请联系机构管理员。");
throw new BizException("invalidAccess", "非法访问,请联系机构管理员。");
}
@@ -90,7 +91,7 @@ public class ControllerAop {
if (sysAdmin != null && sysAdmin.value() && !Ctx.isSysAdmin()) {
return R.fail("invalidAccess", "非法访问,请联系系统管理员。");
throw new BizException("invalidAccess", "非法访问,请联系系统管理员。");
}
@@ -108,7 +109,7 @@ public class ControllerAop {
}
{
return R.fail("invalidAccess", "当前资源未授权,请联系机构管理员处理。");
throw new BizException("invalidAccess", "当前资源未授权,请联系机构管理员处理。");
}
@@ -132,15 +133,9 @@ public class ControllerAop {
updateSysLog(e, sysLogEntity);
if (e instanceof BizException) {
return R.fail("bizError", e.getMessage());
} else if (e instanceof RuntimeException) {
throw e;
return R.fail("sysError", "系统异常");
} else {
return R.fail("sysError", "系统异常");
}
} finally {

View File

@@ -0,0 +1,39 @@
package cn.lihongjie.coal.config;
import cn.lihongjie.coal.dto.R;
import cn.lihongjie.coal.exception.BizException;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.HandlerMethod;
@RestControllerAdvice
@Slf4j
@Order(1)
public class GlobalExceptionHandler {
@ExceptionHandler(BizException.class)
public R handleException(BizException ex, HttpServletRequest request, HandlerMethod handlerMethod) {
return R.fail(ex.getCode(), ex.getMessage());
}
@ExceptionHandler(RuntimeException.class)
public R handleException(RuntimeException ex, HttpServletRequest request,HandlerMethod handlerMethod) {
return R.fail("sysError", "系统异常");
}
@ExceptionHandler(Exception.class)
public R handleException(Exception ex, HttpServletRequest request,HandlerMethod handlerMethod) {
return R.fail("sysError", "系统异常");
}
}

View File

@@ -1,5 +1,6 @@
package cn.lihongjie.coal.controller;
import cn.lihongjie.coal.annotation.Anonymous;
import cn.lihongjie.coal.dto.CoalBlendRequest;
import cn.lihongjie.coal.dto.CoalBlendResult;
import cn.lihongjie.coal.dto.base.CoalParameterDef;
@@ -14,6 +15,7 @@ import java.util.List;
@RestController
@RequestMapping("/coal")
@Anonymous
public class CoalController {
@Autowired

View File

@@ -1,8 +1,23 @@
package cn.lihongjie.coal.exception;
import lombok.Getter;
import lombok.experimental.StandardException;
@StandardException
@Getter
public class BizException extends RuntimeException{
String code;
public BizException(String message ) {
this("bizError", message);
}
public BizException(String code, String message ) {
super(message);
this.code = code;
}
public BizException(String code, String message, Throwable cause) {
super(message, cause);
this.code = code;
}
}

View File

@@ -5,7 +5,7 @@
"remarks": "单位质量的煤完全的燃烧时所产生的热量",
"order": "6",
"sortKey": "6",
"status": "1"
"status": "0"
},
{
"code": "param2",
@@ -37,7 +37,7 @@
"remarks": "固定碳含量是指除去水分、灰分和挥发分的残留物",
"order": "8",
"sortKey": "8",
"status": "1"
"status": "0"
},
{
"code": "param6",
@@ -61,7 +61,7 @@
"remarks": "是由植物变成煤时所含的水分",
"order": "9",
"sortKey": "9",
"status": "1"
"status": "0"
},
{
"code": "param9",
@@ -69,7 +69,7 @@
"remarks": "是在开采、运输等过程中附在煤表面和裂隙中的水分",
"order": "10",
"sortKey": "10",
"status": "1"
"status": "0"
},
{
"code": "param10",
@@ -77,6 +77,14 @@
"remarks": "煤的外在水分和内在水分总和",
"order": "5",
"sortKey": "5",
"status": "0"
},
{
"code": "param11",
"name": "成本",
"remarks": "如: 价格 运费 等",
"order": "100",
"sortKey": "100",
"status": "1"
}
]