Eric 2 rokov pred
rodič
commit
34702759fc

+ 21 - 15
cooleshow-common/src/main/java/com/yonge/cooleshow/common/controller/BaseController.java

@@ -1,14 +1,11 @@
 package com.yonge.cooleshow.common.controller;
 
-import java.net.URLEncoder;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.stream.Collectors;
-
-import javax.crypto.Mac;
-import javax.crypto.spec.SecretKeySpec;
-import javax.servlet.http.HttpServletRequest;
-
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.yonge.cooleshow.common.entity.HttpResponseResult;
+import com.yonge.toolset.base.exception.BizException;
+import com.yonge.toolset.base.exception.ThirdpartyException;
+import com.yonge.toolset.utils.http.HttpUtil;
 import org.apache.commons.codec.binary.Base64;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.lang3.exception.ExceptionUtils;
@@ -23,12 +20,13 @@ import org.springframework.web.bind.MethodArgumentNotValidException;
 import org.springframework.web.bind.annotation.ControllerAdvice;
 import org.springframework.web.bind.annotation.ExceptionHandler;
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONObject;
-import com.yonge.cooleshow.common.entity.HttpResponseResult;
-import com.yonge.toolset.base.exception.BizException;
-import com.yonge.toolset.base.exception.ThirdpartyException;
-import com.yonge.toolset.utils.http.HttpUtil;
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import javax.servlet.http.HttpServletRequest;
+import java.net.URLEncoder;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 @ControllerAdvice
 public class BaseController {
@@ -111,6 +109,14 @@ public class BaseController {
             if (e.getMessage().equals("205")) {
                 return failed(HttpStatus.RESET_CONTENT, e.getMessage());
             }
+
+            // 自定义错误码
+            if (e instanceof BizException) {
+                BizException bizException = (BizException) e;
+                return getResponseData(false, bizException.getCode(), null, bizException.getMessage());
+            }
+
+            // 默认返回错误码
             return failed(e.getMessage());
         } else if (e instanceof AccessDeniedException) {
             return failed("禁止访问");

+ 10 - 0
toolset/toolset-base/src/main/java/com/yonge/toolset/base/exception/BizException.java

@@ -4,6 +4,7 @@ import com.yonge.toolset.base.string.MessageFormatter;
 
 public class BizException extends RuntimeException {
 
+	private int code = 500;
 	/**
 	 * 
 	 */
@@ -17,6 +18,12 @@ public class BizException extends RuntimeException {
 		super(message);
 	}
 
+	public BizException(Integer code, String message) {
+		super(message);
+		// 统一错误码
+		this.code = code;
+	}
+
 	public BizException(Throwable cause) {
 		super(cause);
 	}
@@ -33,4 +40,7 @@ public class BizException extends RuntimeException {
 		super(MessageFormatter.arrayFormat(message, args), cause);
 	}
 
+	public int getCode() {
+		return code;
+	}
 }