重构异常状态码机制

This commit is contained in:
click33
2022-10-31 02:00:38 +08:00
parent f21b6d8b07
commit d2665036a2
56 changed files with 832 additions and 229 deletions
@@ -0,0 +1,23 @@
package cn.dev33.satoken.error;
/**
* 定义 sa-token-spring-boot-starter 所有异常细分状态码
*
* @author kong
* @since: 2022-10-30
*/
public interface SaSpringBootErrorCode {
/** 企图在非 Web 上下文获取 Request、Response 等对象 */
public static final int CODE_20101 = 20101;
/** 对象转 JSON 字符串失败 */
public static final int CODE_20103 = 20103;
/** JSON 字符串转 Map 失败 */
public static final int CODE_20104 = 20104;
/** 默认的 Filter 异常处理函数 */
public static final int CODE_20105 = 20105;
}
@@ -14,6 +14,7 @@ import javax.servlet.ServletResponse;
import org.springframework.core.annotation.Order;
import cn.dev33.satoken.error.SaSpringBootErrorCode;
import cn.dev33.satoken.exception.BackResultException;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.exception.StopMatchException;
@@ -108,7 +109,7 @@ public class SaServletFilter implements Filter {
* 异常处理函数:每次[认证函数]发生异常时执行此函数
*/
public SaFilterErrorStrategy error = e -> {
throw new SaTokenException(e);
throw new SaTokenException(e).setCode(SaSpringBootErrorCode.CODE_20105);
};
/**
@@ -6,7 +6,8 @@ import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.error.SaSpringBootErrorCode;
import cn.dev33.satoken.exception.NotWebContextException;
/**
* SpringMVC相关操作
@@ -25,7 +26,7 @@ public class SpringMVCUtil {
public static HttpServletRequest getRequest() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(servletRequestAttributes == null) {
throw new SaTokenException("非Web上下文无法获取Request");
throw new NotWebContextException("非Web上下文无法获取Request").setCode(SaSpringBootErrorCode.CODE_20101);
}
return servletRequestAttributes.getRequest();
}
@@ -37,7 +38,7 @@ public class SpringMVCUtil {
public static HttpServletResponse getResponse() {
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if(servletRequestAttributes == null) {
throw new SaTokenException("非Web上下文无法获取Response");
throw new NotWebContextException("非Web上下文无法获取Response").setCode(SaSpringBootErrorCode.CODE_20101);
}
return servletRequestAttributes.getResponse();
}
@@ -5,6 +5,7 @@ import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.dev33.satoken.error.SaSpringBootErrorCode;
import cn.dev33.satoken.exception.SaJsonConvertException;
import cn.dev33.satoken.json.SaJsonTemplate;
@@ -31,7 +32,7 @@ public class SaJsonTemplateForJackson implements SaJsonTemplate {
try {
return objectMapper.writeValueAsString(obj);
} catch (JsonProcessingException e) {
throw new SaJsonConvertException(e);
throw new SaJsonConvertException(e).setCode(SaSpringBootErrorCode.CODE_20103);
}
}
@@ -45,7 +46,7 @@ public class SaJsonTemplateForJackson implements SaJsonTemplate {
Map<String, Object> map = objectMapper.readValue(jsonStr, Map.class);
return map;
} catch (JsonProcessingException e) {
throw new SaJsonConvertException(e);
throw new SaJsonConvertException(e).setCode(SaSpringBootErrorCode.CODE_20104);
}
}