接口调整
This commit is contained in:
@@ -8,12 +8,14 @@ import org.springframework.core.annotation.Order;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.reactive.resource.NoResourceFoundException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import static com.njcn.rdms.framework.common.exception.enums.GlobalErrorCodeConstants.INTERNAL_SERVER_ERROR;
|
||||
import static com.njcn.rdms.framework.common.exception.enums.GlobalErrorCodeConstants.NOT_FOUND;
|
||||
|
||||
/**
|
||||
* Gateway 的全局异常处理器,将 Exception 翻译成 CommonResult + 对应的异常编号
|
||||
@@ -27,6 +29,8 @@ import static com.njcn.rdms.framework.common.exception.enums.GlobalErrorCodeCons
|
||||
@Slf4j
|
||||
public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
|
||||
|
||||
private static final String CHROME_DEVTOOLS_RESOURCE_PATH = "/.well-known/appspecific/com.chrome.devtools.json";
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) {
|
||||
// 已经 commit,则直接返回异常
|
||||
@@ -37,7 +41,9 @@ public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
|
||||
|
||||
// 转换成 CommonResult
|
||||
CommonResult<?> result;
|
||||
if (ex instanceof ResponseStatusException) {
|
||||
if (ex instanceof NoResourceFoundException) {
|
||||
result = noResourceFoundExceptionHandler(exchange, (NoResourceFoundException) ex);
|
||||
} else if (ex instanceof ResponseStatusException) {
|
||||
result = responseStatusExceptionHandler(exchange, (ResponseStatusException) ex);
|
||||
} else {
|
||||
result = defaultExceptionHandler(exchange, ex);
|
||||
@@ -58,6 +64,24 @@ public class GlobalExceptionHandler implements ErrorWebExceptionHandler {
|
||||
return CommonResult.error(ex.getStatusCode().value(), ex.getReason());
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理 WebFlux 静态资源不存在异常
|
||||
*/
|
||||
private CommonResult<?> noResourceFoundExceptionHandler(ServerWebExchange exchange,
|
||||
NoResourceFoundException ex) {
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
String path = request.getPath().value();
|
||||
log.debug("[noResourceFoundExceptionHandler][uri({}/{}) 请求地址不存在]", request.getURI(), request.getMethod());
|
||||
return CommonResult.error(NOT_FOUND.getCode(), buildNoResourceMessage(path));
|
||||
}
|
||||
|
||||
private String buildNoResourceMessage(String path) {
|
||||
if (CHROME_DEVTOOLS_RESOURCE_PATH.equals(path)) {
|
||||
return "当前服务未提供浏览器调试探测资源";
|
||||
}
|
||||
return String.format("请求地址不存在:%s", path);
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理系统异常,兜底处理所有的一切
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user