瀏覽代碼

修改BaseException的父异常类型,全局异常处理增加系统异常

Diors.Po 6 年之前
父節點
當前提交
342bc503b4

+ 1 - 1
core/src/main/java/com/mooctest/crowd/domain/exception/BaseException.java

@@ -8,7 +8,7 @@ import lombok.NoArgsConstructor;
  * @date 2019-07-25 00:15
  */
 @NoArgsConstructor
-public class BaseException extends Exception {
+public class BaseException extends RuntimeException {
     public BaseException(String msg){
         super(msg);
     }

+ 23 - 0
site/src/main/java/com/mooctest/crowd/site/controller/TestController.java

@@ -0,0 +1,23 @@
+package com.mooctest.crowd.site.controller;
+
+import com.mooctest.crowd.domain.exception.BaseException;
+import org.springframework.web.bind.annotation.*;
+
+/**
+ * @author: Diors.Po
+ * @Email: 171256175@qq.com
+ * @date 2019-07-26 22:25
+ */
+@RestController
+public class TestController {
+
+    @RequestMapping(value = "/exception/test/{type}", method = RequestMethod.GET)
+    public Object exceptionTest(@PathVariable("type") Integer type){
+        if (type == 1)
+            throw new BaseException();
+        else if (type == 2)
+            throw new RuntimeException();
+        return "Hello world!!!!";
+    }
+
+}

+ 9 - 0
site/src/main/java/com/mooctest/crowd/site/controller/advice/ExceptionAdvice.java

@@ -6,6 +6,7 @@ import com.mooctest.crowd.domain.exception.*;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
 import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
 import org.springframework.web.bind.annotation.ResponseStatus;
 import org.springframework.web.bind.annotation.RestControllerAdvice;
 
@@ -37,4 +38,12 @@ public class ExceptionAdvice {
         } else
             return new ResponseMessage(ResponseConstant.FAIL, ResponseConstant.REQUEST_FAILED);
     }
+
+    @ExceptionHandler(Exception.class)
+    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
+    @ResponseBody
+    public String handleSystemException(Exception e){
+        log.error("System Error", e);
+        return "There is a system error";
+    }
 }