Sfoglia il codice sorgente

捕获运行时异常

zhoutg 6 anni fa
parent
commit
86e879963e

+ 10 - 4
user-service/src/main/java/com/diagbot/exception/CommonExceptionHandler.java

@@ -1,6 +1,7 @@
 package com.diagbot.exception;
 
 import com.diagbot.dto.RespDTO;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.http.HttpStatus;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.ControllerAdvice;
@@ -15,15 +16,20 @@ import org.springframework.web.bind.annotation.ResponseBody;
  */
 @ControllerAdvice
 @ResponseBody
+@Slf4j
 public class CommonExceptionHandler {
 
-    @ExceptionHandler(CommonException.class)
+    @ExceptionHandler(Exception.class)
     public ResponseEntity<RespDTO> handleException(Exception e) {
         RespDTO resp = new RespDTO();
-        CommonException taiChiException = (CommonException) e;
-        resp.code = taiChiException.getCode();
+        if(e instanceof CommonException) {
+            CommonException taiChiException = (CommonException) e;
+            resp.code = taiChiException.getCode();
+        } else {
+            resp.code = -1;
+        }
         resp.msg = e.getMessage();
-
+        log.error("【抛出异常】:" + e.getMessage());
         return new ResponseEntity(resp, HttpStatus.OK);
     }