Преглед изворни кода

feat: serviceException统一异常处理

370774330@qq.com пре 5 година
родитељ
комит
2311f89a1b

+ 1 - 1
common/src/main/java/seecoder/devcloud/common/exceptions/ServiceException.java

@@ -6,7 +6,7 @@ import org.springframework.http.HttpStatus;
 
 @Getter
 @ToString
-public class ServiceException extends Exception {
+public class ServiceException extends RuntimeException {
 	private int error;
 
 	public ServiceException() {

+ 1 - 1
web/src/main/java/seecoder/devcloud/web/controller/user/UserController.java

@@ -45,7 +45,7 @@ public class UserController {
             @RequestParam("nickname")
             @Pattern(regexp = "^\\w+${4,23}", message = "昵称只能包含大小写字母、数字和下划线,且必须为4-23位!")
                     String nickName) {
-        return userService.changeUserInfo(userid, nickName);
+        return Response.ok(userService.changeUserInfo(userid, nickName));
     }
 
     @GetMapping("/id")

+ 27 - 0
web/src/main/java/seecoder/devcloud/web/infrastructure/GlobalExceptionHandler.java

@@ -0,0 +1,27 @@
+package seecoder.devcloud.web.infrastructure;
+
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+import seecoder.devcloud.common.exceptions.ServiceException;
+import seecoder.devcloud.web.vo.Response;
+
+/**
+ * @author PuHong Weng
+ * @date 2021/3/5
+ * @description: 统一异常返回处理
+ * todo 应为整个系统使用了学长的老API,是否有没捕捉的异常,待进一步验证
+ */
+@ControllerAdvice
+@Slf4j
+public class GlobalExceptionHandler {
+
+    @ExceptionHandler(ServiceException.class)
+    @ResponseBody
+    public Response serviceExceptionHandler(ServiceException e){
+        log.error(e.getMessage(),e);
+        return Response.serverInternalError(e.getMessage());
+    }
+
+}

+ 5 - 0
web/src/main/java/seecoder/devcloud/web/service/impl/user/GroupServiceImpl.java

@@ -1,6 +1,7 @@
 package seecoder.devcloud.web.service.impl.user;
 
 
+import lombok.extern.slf4j.Slf4j;
 import org.gitlab4j.api.GitLabApiException;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.context.ApplicationContext;
@@ -26,6 +27,7 @@ import java.util.UUID;
 
 
 @Service
+@Slf4j
 public class GroupServiceImpl implements GroupService {
     private final GroupMapper groupMapper;
     private final UserMapper userMapper;
@@ -54,6 +56,7 @@ public class GroupServiceImpl implements GroupService {
             gitGroup = gitlabApi.createGroup(name);
             gitlabApi.addMemberToGroup(gitGroup.getId(), user.getId());
         } catch (GitLabApiException e) {
+            log.error(e.getMessage(), e);
             throw new RuntimeException("创建Group时发生异常[" + e.getMessage() + "]");
         }
         Group group = Group.builder()
@@ -83,6 +86,7 @@ public class GroupServiceImpl implements GroupService {
         try {
             gitlabApi.addMemberToGroup(groupId, invitedUserId);
         } catch (GitLabApiException e) {
+            log.error(e.getMessage(), e);
             throw new RuntimeException("将成员加入Group时发生异常[" + e.getMessage() + "]");
         }
         groupMemberMapper.insert(groupId, invitedUserId);
@@ -96,6 +100,7 @@ public class GroupServiceImpl implements GroupService {
         try {
             gitlabApi.quitGroup(groupId,userId);
         } catch (GitLabApiException e) {
+            log.error(e.getMessage(), e);
             throw new ServiceException(e.getMessage());
         }
         groupMemberMapper.delete(groupId,userId);

+ 7 - 3
web/src/main/java/seecoder/devcloud/web/service/impl/user/UserServiceImpl.java

@@ -1,6 +1,7 @@
 package seecoder.devcloud.web.service.impl.user;
 
 
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.codec.digest.DigestUtils;
 import org.gitlab4j.api.GitLabApiException;
 import org.springframework.beans.BeanUtils;
@@ -27,6 +28,7 @@ import seecoder.devcloud.web.service.user.MailService;
 import seecoder.devcloud.web.service.user.UserService;
 
 @Service
+@Slf4j
 public class UserServiceImpl implements UserService {
 	private static final Sort PAGE_SORT = Sort.by(Sort.Direction.ASC, "id");
 
@@ -65,7 +67,7 @@ public class UserServiceImpl implements UserService {
 		try {
 			gitlabUser = gitlabApi.createUser(username, email, realPassword);
 		} catch (GitLabApiException e) {
-			e.printStackTrace();
+			log.error(e.getMessage(), e);
 			throw new ServiceException("创建GitLab用户时发生异常[" + e.getMessage() + "]");
 		}
 		user = new User();
@@ -96,6 +98,7 @@ public class UserServiceImpl implements UserService {
 		try {
 			gitlabApi.deleteUser(userId);
 		} catch (Exception e) {
+			log.error(e.getMessage(), e);
 			throw new ServiceException("删除GitLab用户时发生异常[" + e.getMessage() + "]");
 		}
 		//todo 删除用户需要连带删除什么
@@ -119,8 +122,8 @@ public class UserServiceImpl implements UserService {
 	}
 
 	@Override
-	public void changePassword(int userId, ChangePasswordVO form) throws ServiceException {
-		User user = userMapper.findById(userId);
+	public void changePassword(ChangePasswordVO form) throws ServiceException {
+		User user = userMapper.findById(form.getUserId());
 
 		if (!user.getPassword().equals(DigestUtils.sha256Hex(form.getOrigin()))) {
 			throw new InvalidRequestException("原密码验证失败,如忘记密码请联系管理员重置");
@@ -140,6 +143,7 @@ public class UserServiceImpl implements UserService {
 			 */
 			gitlabApi.changePassword(user.getId(), password);
 		} catch (GitLabApiException e) {
+			log.error(e.getMessage(), e);
 			throw new ServiceException("修改GitLab用户密码时发生异常[" + e.getMessage() + "]");
 		}
 		user.setPassword(DigestUtils.sha256Hex(password));

+ 1 - 1
web/src/main/java/seecoder/devcloud/web/service/user/UserService.java

@@ -23,6 +23,6 @@ public interface UserService {
 
 	UserVO changeUserInfo(Integer userid, String nickName) throws ServiceException;
 
-	void changePassword(int userId, ChangePasswordVO form) throws ServiceException;
+	void changePassword(ChangePasswordVO form) throws ServiceException;
 
 }

+ 10 - 10
web/src/main/java/seecoder/devcloud/web/vo/Response.java

@@ -47,24 +47,24 @@ public class Response<T> implements Serializable {
         return new Response<T>(ResponseEnums.OK,data);
     }
 
-    public static Response notFound() {
-        return new Response(ResponseEnums.NOT_FOUND);
+    public static Response notFound(String message) {
+        return new Response(ResponseEnums.NOT_FOUND, message);
     }
 
-    public static Response badRequest() {
-        return new Response(ResponseEnums.BAD_REQUEST);
+    public static Response badRequest(String message) {
+        return new Response(ResponseEnums.BAD_REQUEST, message);
     }
 
-    public static Response forbidden() {
-        return new Response(ResponseEnums.FORBIDDEN);
+    public static Response forbidden(String message) {
+        return new Response(ResponseEnums.FORBIDDEN, message);
     }
 
-    public static Response unauthorized() {
-        return new Response(ResponseEnums.UNAUTHORIZED);
+    public static Response unauthorized(String message) {
+        return new Response(ResponseEnums.UNAUTHORIZED, message);
     }
 
-    public static Response serverInternalError() {
-        return new Response(ResponseEnums.SERVER_INTERNAL_ERROR);
+    public static Response serverInternalError(String message) {
+        return new Response(ResponseEnums.SERVER_INTERNAL_ERROR, message);
     }
 
 }

+ 2 - 0
web/src/main/java/seecoder/devcloud/web/vo/user/ChangePasswordVO.java

@@ -11,6 +11,8 @@ import javax.validation.constraints.Pattern;
 @FieldEquals(firstField = "password", secondField = "confirm", message = "两次输入的新密码不一致!")
 @AllArgsConstructor
 public class ChangePasswordVO {
+
+	private Integer userId;
 	@NotNull(message = "请填写原密码!")
 	private String origin;
 	@Pattern(regexp = "[0-9a-zA-Z_]{8,16}", message = "密码只能包含大小写字母、数字和下划线,且必须为8-16位!")