异常消息具体化

This commit is contained in:
Wenchao Gong 2021-10-19 23:53:39 +08:00
parent e4ed70ee7e
commit 1317da09ca
9 changed files with 43 additions and 31 deletions

View File

@ -58,8 +58,12 @@ public class GlobalErrorWebExceptionHandler implements ErrorWebExceptionHandler
{
if(ex instanceof ResponseStatusException) {
String reason = ((ResponseStatusException) ex).getReason();
log.info(reason);
return reason;
if(log.isDebugEnabled()){
log.debug(ex.getMessage());
}
if(StrUtil.isNotBlank(reason)) {
return reason;
}
}
String message = ex.getMessage();
log.error(message, ex);

View File

@ -6,10 +6,10 @@ package com.ruoyi.gateway.constant;
* @date 2021-10-18
*/
public interface ErrorMessageConstant {
String AK_REQUIRE = "没有找到授权码";
String CONTENT_TYPE_NOT_SUPPORTED = "请求ContentType不支持";
String METHOD_NOT_SUPPORTED = "请求Method不支持";
String RATE_LIMIT = "调用次数过多";
String RULE_EXPIRED = "规则已过期";
String RULE_NOT_EXIST = "规则不存在";
String AK_REQUIRE = "没有找到授权码[%s]";
String CONTENT_TYPE_NOT_SUPPORTED = "请求ContentType[%s]不支持";
String METHOD_NOT_SUPPORTED = "请求Method[%s]不支持";
String RATE_LIMIT = "调用次数[>%d/%s]过多";
String RULE_EXPIRED = "规则已过期[%s]";
String RULE_NOT_EXIST = "授权码[%s]对应服务[%s]规则不存在";
}

View File

@ -10,8 +10,8 @@ import com.ruoyi.gateway.constant.ErrorMessageConstant;
*/
public class AkRequireException extends GatewayException {
public AkRequireException()
public AkRequireException(String ak)
{
super(ErrorMessageConstant.AK_REQUIRE);
super(String.format(ErrorMessageConstant.AK_REQUIRE, ak));
}
}

View File

@ -1,6 +1,7 @@
package com.ruoyi.gateway.exception;
import com.ruoyi.gateway.constant.ErrorMessageConstant;
import org.springframework.http.MediaType;
/**
* 网关异常-请求方法不支持
@ -10,8 +11,8 @@ import com.ruoyi.gateway.constant.ErrorMessageConstant;
*/
public class ContentTypeNotSupportedException extends GatewayException {
public ContentTypeNotSupportedException()
public ContentTypeNotSupportedException(MediaType contentType)
{
super(ErrorMessageConstant.CONTENT_TYPE_NOT_SUPPORTED);
super(String.format(ErrorMessageConstant.CONTENT_TYPE_NOT_SUPPORTED, contentType));
}
}

View File

@ -1,6 +1,7 @@
package com.ruoyi.gateway.exception;
import com.ruoyi.gateway.constant.ErrorMessageConstant;
import org.springframework.http.HttpMethod;
/**
* 网关异常-请求方法不支持
@ -10,8 +11,8 @@ import com.ruoyi.gateway.constant.ErrorMessageConstant;
*/
public class MethodNotSupportedException extends GatewayException {
public MethodNotSupportedException()
public MethodNotSupportedException(HttpMethod httpMethod)
{
super(ErrorMessageConstant.METHOD_NOT_SUPPORTED);
super(String.format(ErrorMessageConstant.METHOD_NOT_SUPPORTED, httpMethod));
}
}

View File

@ -3,6 +3,8 @@ package com.ruoyi.gateway.exception;
import com.ruoyi.gateway.constant.ErrorMessageConstant;
import org.springframework.http.HttpStatus;
import java.util.concurrent.TimeUnit;
/**
* 网关异常-限流异常
*
@ -11,8 +13,8 @@ import org.springframework.http.HttpStatus;
*/
public class RateLimitException extends GatewayException {
public RateLimitException()
public RateLimitException(long limit, TimeUnit timeUnit)
{
super(HttpStatus.TOO_MANY_REQUESTS, ErrorMessageConstant.RATE_LIMIT);
super(HttpStatus.TOO_MANY_REQUESTS, String.format(ErrorMessageConstant.RATE_LIMIT, limit, timeUnit.name()));
}
}

View File

@ -1,7 +1,10 @@
package com.ruoyi.gateway.exception;
import cn.hutool.core.date.DateUtil;
import com.ruoyi.gateway.constant.ErrorMessageConstant;
import java.util.Date;
/**
* 网关异常-规则已过期
*
@ -10,8 +13,8 @@ import com.ruoyi.gateway.constant.ErrorMessageConstant;
*/
public class RuleExpiredException extends GatewayException {
public RuleExpiredException()
public RuleExpiredException(Date endTime)
{
super(ErrorMessageConstant.RULE_EXPIRED);
super(String.format(ErrorMessageConstant.RULE_EXPIRED, DateUtil.formatDateTime(endTime)));
}
}

View File

@ -10,8 +10,8 @@ import com.ruoyi.gateway.constant.ErrorMessageConstant;
*/
public class RuleNotExistException extends GatewayException {
public RuleNotExistException()
public RuleNotExistException(String ak, String path)
{
super(ErrorMessageConstant.RULE_NOT_EXIST);
super(String.format(ErrorMessageConstant.RULE_NOT_EXIST, ak, path));
}
}

View File

@ -69,7 +69,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered {
} else if (HttpMethod.POST.equals(httpMethod)) {
return handlePostRequest(exchange, chain, route, request, accessKeyName, headerAk);
}
throw new MethodNotSupportedException();
throw new MethodNotSupportedException(httpMethod);
}
/**
@ -95,7 +95,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered {
} else if (MediaType.APPLICATION_FORM_URLENCODED.equals(mediaType)) {
return handlePostRequestFormUrlencoded(exchange, route, request, accessKeyName, headerAk, body);
}
return Mono.error(ContentTypeNotSupportedException::new);
return Mono.error(() -> new ContentTypeNotSupportedException(mediaType));
});
return GatewayUtils.modifyBody(exchange, chain, modifiedBody);
}
@ -116,13 +116,13 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered {
String headerAk, String body) {
if (StrUtil.isBlank(body)) {
Assert.notBlank(headerAk, AkRequireException::new);
Assert.notBlank(headerAk, () -> new AkRequireException(accessKeyName));
}
final List<String[]> srcParams = Arrays.stream(body.split("&")).map(param -> param.split("="))
.collect(Collectors.toList());
final IscRule rule = handleRule(headerAk, () -> srcParams.stream()
.filter(param -> param.length > 0 && accessKeyName.equals(param[0]))
.map(param -> param[1]).collect(Collectors.toList()), route);
.map(param -> param[1]).collect(Collectors.toList()), route, accessKeyName);
Supplier<Mono<String>> rateLimiterAfterSupplier = () -> {
removeParam(headerAk, request, accessKeyName, null);
final List<String[]> params = srcParams.stream().filter(param -> !accessKeyName.equals(param[0]))
@ -160,7 +160,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered {
String headerAk, String body) {
JSONObject jsonObj = StrUtil.isBlank(body) ? new JSONObject() : JSONUtil.parseObj(body);
final IscRule rule = handleRule(headerAk, () -> Collections.singletonList(jsonObj.get(accessKeyName,
String.class, true)), route);
String.class, true)), route, accessKeyName);
Supplier<Mono<String>> rateLimiterAfterSupplier = () -> {
removeParam(headerAk, request, accessKeyName, () -> jsonObj.remove(accessKeyName));
handleHiddenParams(route, jsonObj, (next, map) -> map.set(next.getKey(), next.getValue()));
@ -184,7 +184,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered {
private Mono<Void> handleGetRequest(ServerWebExchange exchange, GatewayFilterChain chain, Route route,
ServerHttpRequest request, String accessKeyName, String headerAk) {
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<>(request.getQueryParams());
final IscRule rule = handleRule(headerAk, () -> queryParams.get(accessKeyName), route);
final IscRule rule = handleRule(headerAk, () -> queryParams.get(accessKeyName), route, accessKeyName);
Supplier<Mono<Void>> rateLimiterAfterSupplier = () -> {
removeParam(headerAk, request, accessKeyName, () -> queryParams.remove(accessKeyName));
handleHiddenParams(route, queryParams, (next, map) -> {
@ -216,16 +216,17 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered {
* @param headerAk 头部AK
* @param valueSupplier valueList 生产者
* @param route 路由信息
* @param accessKeyName AK键名
* @return AK对应服务规则信息
*/
private IscRule handleRule(String headerAk, Supplier<List<String>> valueSupplier, Route route) {
private IscRule handleRule(String headerAk, Supplier<List<String>> valueSupplier, Route route, String accessKeyName) {
//获取AK
final String ak = GatewayUtils.getValue(headerAk, valueSupplier, AkRequireException::new);
final String ak = GatewayUtils.getValue(headerAk, valueSupplier, () -> new AkRequireException(accessKeyName));
//获取规则
final IscRule rule = GatewayUtils.getRequiredValue(() -> GatewayUtils.getRule(ak, route.getId()),
RuleNotExistException::new);
() -> new RuleNotExistException(ak, route.getId()));
//是否到期
GatewayUtils.isBefore(rule, RuleExpiredException::new);
GatewayUtils.isBefore(rule, () -> new RuleExpiredException(rule.getExpire()));
//设置AK 到ID 为了传参方便
rule.setId(ak);
return rule;
@ -307,7 +308,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered {
}
return rateLimiter(exchange, rule, route, index + 1, rateLimiterAfterSupplier);
}
return Mono.error(RateLimitException::new);
return Mono.error(() -> new RateLimitException(limit, timeUnit));
});
}
}