Browse Source

修改异常捕获

tags/24080901
WendyYang 7 months ago
parent
commit
1472750160
2 changed files with 34 additions and 21 deletions
  1. +22
    -13
      hz-pm-api/src/main/java/com/hz/pm/api/common/handler/GlobalExceptionHandler.java
  2. +12
    -8
      hz-pm-api/src/main/java/com/hz/pm/api/meeting/manage/MeetingManage.java

+ 22
- 13
hz-pm-api/src/main/java/com/hz/pm/api/common/handler/GlobalExceptionHandler.java View File

@@ -2,6 +2,7 @@ package com.hz.pm.api.common.handler;


import com.hz.pm.api.common.exception.ReturnException; import com.hz.pm.api.common.exception.ReturnException;
import com.ningdatech.basic.enumeration.Status; import com.ningdatech.basic.enumeration.Status;
import com.ningdatech.basic.exception.BizException;
import com.ningdatech.basic.model.ApiResponse; import com.ningdatech.basic.model.ApiResponse;
import com.ningdatech.basic.util.CollUtils; import com.ningdatech.basic.util.CollUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@@ -33,46 +34,54 @@ import java.util.stream.Collectors;
@ResponseStatus(HttpStatus.BAD_REQUEST) @ResponseStatus(HttpStatus.BAD_REQUEST)
public class GlobalExceptionHandler { public class GlobalExceptionHandler {


public static final int ERROR_CODE = Status.BAD_REQUEST.getCode();

@ResponseBody @ResponseBody
@ExceptionHandler(value = NoHandlerFoundException.class) @ExceptionHandler(value = NoHandlerFoundException.class)
public ApiResponse<Void> noHandlerFoundException(NoHandlerFoundException e) {
public ApiResponse<Void> catchException(NoHandlerFoundException e) {
return ApiResponse.ofStatus(Status.REQUEST_NOT_FOUND); return ApiResponse.ofStatus(Status.REQUEST_NOT_FOUND);
} }


@ResponseBody @ResponseBody
@ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class}) @ExceptionHandler(value = {MethodArgumentNotValidException.class, BindException.class})
public ApiResponse<Void> bindException(BindException e) {
public ApiResponse<Void> catchException(BindException e) {
String msg = e.getAllErrors().stream() String msg = e.getAllErrors().stream()
.map(DefaultMessageSourceResolvable::getDefaultMessage) .map(DefaultMessageSourceResolvable::getDefaultMessage)
.collect(Collectors.joining(" ")); .collect(Collectors.joining(" "));
return ApiResponse.of(Status.BAD_REQUEST.getCode(), msg);
return ApiResponse.of(ERROR_CODE, msg);
} }


@ResponseBody @ResponseBody
@ExceptionHandler(value = ConstraintViolationException.class) @ExceptionHandler(value = ConstraintViolationException.class)
public ApiResponse<Void> constraintViolationException(ConstraintViolationException e) {
String msg = CollUtils.join(e.getConstraintViolations(), ConstraintViolation::getMessage, " ");
return ApiResponse.of(Status.BAD_REQUEST.getCode(), msg);
public ApiResponse<Void> catchException(ConstraintViolationException e) {
String msg = CollUtils.join(e.getConstraintViolations(),
ConstraintViolation::getMessage, " ");
return ApiResponse.of(ERROR_CODE, msg);
} }


@ResponseBody @ResponseBody
@ExceptionHandler(value = AccessDeniedException.class) @ExceptionHandler(value = AccessDeniedException.class)
public ApiResponse<Void> accessDeniedException(AccessDeniedException e) {
log.error("异常信息: {} ", e.getMessage(), e);
return ApiResponse.of(Status.BAD_REQUEST.getCode(), "暂无操作权限");
public ApiResponse<Void> catchException(AccessDeniedException e) {
return ApiResponse.of(ERROR_CODE, "暂无操作权限");
} }


@ResponseBody @ResponseBody
@ExceptionHandler(value = Exception.class) @ExceptionHandler(value = Exception.class)
public ApiResponse<Void> handlerException(Exception e) {
public ApiResponse<Void> catchException(Exception e) {
log.error("异常信息: {} ", e.getMessage(), e); log.error("异常信息: {} ", e.getMessage(), e);
return ApiResponse.of(Status.BAD_REQUEST.getCode(), "系统异常,请联系管理员");
return ApiResponse.of(ERROR_CODE, "系统异常,请联系管理员");
} }


@ResponseBody @ResponseBody
@ExceptionHandler(value = ReturnException.class) @ExceptionHandler(value = ReturnException.class)
public ApiResponse<Void> returnException(ReturnException e) {
return ApiResponse.of(Status.BAD_REQUEST.getCode(), e.getMessage());
public ApiResponse<Void> catchException(ReturnException e) {
return ApiResponse.of(ERROR_CODE, e.getMessage());
}

@ResponseBody
@ExceptionHandler(value = BizException.class)
public ApiResponse<Void> catchException(BizException e) {
return ApiResponse.of(ERROR_CODE, e.getMessage());
} }


} }

+ 12
- 8
hz-pm-api/src/main/java/com/hz/pm/api/meeting/manage/MeetingManage.java View File

@@ -16,6 +16,7 @@ import com.hz.pm.api.common.exception.ReturnException;
import com.hz.pm.api.common.helper.RegionCacheHelper; import com.hz.pm.api.common.helper.RegionCacheHelper;
import com.hz.pm.api.common.model.constant.BizConst; import com.hz.pm.api.common.model.constant.BizConst;
import com.hz.pm.api.common.util.BizUtils; import com.hz.pm.api.common.util.BizUtils;
import com.hz.pm.api.common.util.EnvironmentUtil;
import com.hz.pm.api.expert.entity.ExpertUserFullInfo; import com.hz.pm.api.expert.entity.ExpertUserFullInfo;
import com.hz.pm.api.expert.service.IExpertUserFullInfoService; import com.hz.pm.api.expert.service.IExpertUserFullInfoService;
import com.hz.pm.api.external.MhApiClient; import com.hz.pm.api.external.MhApiClient;
@@ -103,6 +104,7 @@ public class MeetingManage {
private final IMeetingExpertJudgeService meetingExpertJudgeService; private final IMeetingExpertJudgeService meetingExpertJudgeService;
private final MhApiClient mhApiClient; private final MhApiClient mhApiClient;
private final IUserInfoService userInfoService; private final IUserInfoService userInfoService;
private final EnvironmentUtil environmentUtil;


private static final String INVITED_RULE_CREATE = "INVITED_RULE_CREATE:"; private static final String INVITED_RULE_CREATE = "INVITED_RULE_CREATE:";


@@ -682,15 +684,15 @@ public class MeetingManage {
Long meetingId = req.getMeetingId(); Long meetingId = req.getMeetingId();
String key = "CANCEL_MEETING:" + meetingId; String key = "CANCEL_MEETING:" + meetingId;
if (!distributedLock.lock(key, RETRY_TIMES)) { if (!distributedLock.lock(key, RETRY_TIMES)) {
throw BizException.wrap("正在取消会议,请刷新后重试");
throw ReturnException.wrap("正在取消会议");
} }
try { try {
Meeting meeting = meetingService.getById(meetingId); Meeting meeting = meetingService.getById(meetingId);
if (MeetingStatusEnum.CANCELED.eq(meeting.getStatus())) { if (MeetingStatusEnum.CANCELED.eq(meeting.getStatus())) {
throw BizException.wrap("会议已取消");
throw ReturnException.wrap("会议已取消");
} }
if (meeting.getStartTime().isBefore(LocalDateTime.now())) { if (meeting.getStartTime().isBefore(LocalDateTime.now())) {
throw BizException.wrap("会议已开始,暂时无法取消");
throw ReturnException.wrap("会议已开始,暂时无法取消");
} }
Wrapper<Meeting> meetingUpdate = Wrappers.lambdaUpdate(Meeting.class) Wrapper<Meeting> meetingUpdate = Wrappers.lambdaUpdate(Meeting.class)
.set(Meeting::getStatus, MeetingStatusEnum.CANCELED.getCode()) .set(Meeting::getStatus, MeetingStatusEnum.CANCELED.getCode())
@@ -714,10 +716,10 @@ public class MeetingManage {
Long userId = expertId == null ? LoginUserUtil.getUserId() : expertId; Long userId = expertId == null ? LoginUserUtil.getUserId() : expertId;
MeetingExpert me = meetingExpertService.getByMeetingIdAndExpertId(meetingId, userId); MeetingExpert me = meetingExpertService.getByMeetingIdAndExpertId(meetingId, userId);
if (me == null) { if (me == null) {
throw BizException.wrap("未被邀请参加此会议");
throw ReturnException.wrap("未被邀请参加此会议");
} }
if (AGREED.eq(me.getStatus())) { if (AGREED.eq(me.getStatus())) {
throw BizException.wrap("未确认参加此会议");
throw ReturnException.wrap("未确认参加此会议");
} }
ExpertUserFullInfo userInfo = expertUserFullInfoService.getByUserId(userId); ExpertUserFullInfo userInfo = expertUserFullInfoService.getByUserId(userId);
Meeting meeting = meetingService.getById(meetingId); Meeting meeting = meetingService.getById(meetingId);
@@ -736,12 +738,12 @@ public class MeetingManage {
public void confirmAttendByManager(ExpertConfirmReq req) { public void confirmAttendByManager(ExpertConfirmReq req) {
String key = "CONFIRM_ATTEND:" + req.getExpertMeetingId(); String key = "CONFIRM_ATTEND:" + req.getExpertMeetingId();
if (!distributedLock.lock(key, RETRY_TIMES)) { if (!distributedLock.lock(key, RETRY_TIMES)) {
throw BizException.wrap("操作失败,请重试!");
throw ReturnException.wrap("操作失败,请重试!");
} }
try { try {
MeetingExpert me = meetingExpertService.getById(req.getExpertMeetingId()); MeetingExpert me = meetingExpertService.getById(req.getExpertMeetingId());
if (!NOTICING.eq(me.getStatus())) { if (!NOTICING.eq(me.getStatus())) {
throw BizException.wrap("操作失败,请重试!");
throw ReturnException.wrap("操作失败,请重试!");
} }
Integer status = (Boolean.TRUE.equals(req.getAgreed()) ? AGREED : REFUSED).getCode(); Integer status = (Boolean.TRUE.equals(req.getAgreed()) ? AGREED : REFUSED).getCode();
Wrapper<MeetingExpert> update = Wrappers.lambdaUpdate(MeetingExpert.class) Wrapper<MeetingExpert> update = Wrappers.lambdaUpdate(MeetingExpert.class)
@@ -867,7 +869,9 @@ public class MeetingManage {
.in(MeetingExpert::getId, currConfirmedMeIds) .in(MeetingExpert::getId, currConfirmedMeIds)
.set(MeetingExpert::getConfirmedRoster, Boolean.TRUE); .set(MeetingExpert::getConfirmedRoster, Boolean.TRUE);
meetingExpertService.update(meUpdate); meetingExpertService.update(meUpdate);
meetingNotifyHelper.sendConfirmedRosterMsg(expertNoticing, meeting);
if (environmentUtil.isProdEnv()) {
meetingNotifyHelper.sendConfirmedRosterMsg(expertNoticing, meeting);
}
} finally { } finally {
distributedLock.releaseLock(key); distributedLock.releaseLock(key);
} }


Loading…
Cancel
Save