소스 검색

添加错误捕获

tuonina 5 년 전
부모
커밋
a949499b42

+ 25 - 0
tuon-core/src/main/java/cn/tonyandmoney/tuon/core/error/ControllerExceptionHandler.java

@@ -28,4 +28,29 @@ public class ControllerExceptionHandler {
 
     }
 
+    /**
+     * 没有权限
+     *
+     * @return 没权限
+     */
+    @ExceptionHandler(ForbiddenException.class)
+    @ResponseStatus(HttpStatus.FORBIDDEN)
+    public BaseResp forbiddenError(ForbiddenException ex) {
+        logger.warn("server error: ", ex);
+        return BaseResp.error(ex);
+    }
+
+    /**
+     * 用户没有登录
+     *
+     * @param ex 错误
+     * @return
+     */
+    @ExceptionHandler(UnauthorizedException.class)
+    @ResponseStatus(HttpStatus.UNAUTHORIZED)
+    public BaseResp unauthorizedError(UnauthorizedException ex) {
+        logger.warn("server error: ", ex);
+        return BaseResp.error(ex);
+    }
+
 }

+ 16 - 0
tuon-core/src/main/java/cn/tonyandmoney/tuon/core/error/ForbiddenException.java

@@ -0,0 +1,16 @@
+package cn.tonyandmoney.tuon.core.error;
+
+/**
+ * 没有权限的错误
+ */
+public class ForbiddenException extends RuntimeException {
+
+    public ForbiddenException() {
+        super("您没有访问该资源的权限!");
+    }
+
+    public ForbiddenException(String msg) {
+        super(msg);
+    }
+
+}

+ 7 - 2
tuon-core/src/main/java/cn/tonyandmoney/tuon/core/error/SessionNoUserException.java

@@ -5,12 +5,17 @@ import org.springframework.web.server.WebSession;
 /**
  * 没有用户信息
  */
-public class SessionNoUserException extends RuntimeException {
+public class SessionNoUserException extends UnauthorizedException {
 
     private WebSession session;
 
     public SessionNoUserException(WebSession session) {
-        this.session =session;
+        this.session = session;
+    }
+
+    public SessionNoUserException(WebSession session,String message){
+        super(message);
+        this.session = session;
     }
 
     public void setSession(WebSession session) {

+ 17 - 0
tuon-core/src/main/java/cn/tonyandmoney/tuon/core/error/UnauthorizedException.java

@@ -0,0 +1,17 @@
+package cn.tonyandmoney.tuon.core.error;
+
+/**
+ * 用户么有登录
+ */
+public class UnauthorizedException extends RuntimeException {
+
+    public UnauthorizedException(String msg) {
+        super(msg);
+    }
+
+    public UnauthorizedException() {
+        super("您还没有登录!");
+    }
+
+
+}