Sfoglia il codice sorgente

通用错误拦截处理

gaodm 6 anni fa
parent
commit
db7614f7a6

+ 30 - 0
log-service/src/main/java/com/diagbot/exception/CommonExceptionHandler.java

@@ -0,0 +1,30 @@
+package com.diagbot.exception;
+
+import com.diagbot.dto.RespDTO;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.web.bind.annotation.ControllerAdvice;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.ResponseBody;
+
+
+/**
+ * @Description: 错误通用处理
+ * @author: gaodm
+ * @time: 2018/8/2 14:22
+ */
+@ControllerAdvice
+@ResponseBody
+public class CommonExceptionHandler {
+
+    @ExceptionHandler(CommonException.class)
+    public ResponseEntity<RespDTO> handleException(Exception e) {
+        RespDTO resp = new RespDTO();
+        CommonException taiChiException = (CommonException) e;
+        resp.code = taiChiException.getCode();
+        resp.msg = e.getMessage();
+
+        return new ResponseEntity(resp, HttpStatus.OK);
+    }
+
+}