You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
2.2 KiB
63 lines
2.2 KiB
package com.ruoyi.gateway.handler; |
|
|
|
import com.ruoyi.gateway.constant.MessagesConstant; |
|
import org.springframework.cloud.gateway.support.NotFoundException; |
|
import org.slf4j.Logger; |
|
import org.slf4j.LoggerFactory; |
|
import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler; |
|
import org.springframework.context.annotation.Configuration; |
|
import org.springframework.core.annotation.Order; |
|
import org.springframework.http.server.reactive.ServerHttpResponse; |
|
import org.springframework.web.server.ResponseStatusException; |
|
import org.springframework.web.server.ServerWebExchange; |
|
import com.ruoyi.common.core.utils.ServletUtils; |
|
import reactor.core.publisher.Mono; |
|
|
|
/** |
|
* 网关统一异常处理 |
|
* |
|
* @author ruoyi |
|
*/ |
|
@Order(-1) |
|
@Configuration |
|
public class GatewayExceptionHandler implements ErrorWebExceptionHandler |
|
{ |
|
private static final Logger log = LoggerFactory.getLogger(GatewayExceptionHandler.class); |
|
|
|
@Override |
|
public Mono<Void> handle(ServerWebExchange exchange, Throwable ex) |
|
{ |
|
ServerHttpResponse response = exchange.getResponse(); |
|
|
|
if (exchange.getResponse().isCommitted()) |
|
{ |
|
return Mono.error(ex); |
|
} |
|
|
|
String msg; |
|
|
|
if (ex instanceof NotFoundException) |
|
{ |
|
/**服务未找到*/ |
|
msg =MessagesConstant.getMessages(exchange,MessagesConstant.MESSAGES_SERVICE_NOT_FOUND_ZH,MessagesConstant.MESSAGES_SERVICE_NOT_FOUND_EN); |
|
|
|
} |
|
else if (ex instanceof ResponseStatusException) |
|
{ |
|
ResponseStatusException responseStatusException = (ResponseStatusException) ex; |
|
msg = responseStatusException.getMessage(); |
|
} |
|
else |
|
{ |
|
/**内部服务器错误*/ |
|
//msg="内部服务器错误"; |
|
msg =MessagesConstant.getMessages(exchange,MessagesConstant.MESSAGES_INTERNAL_SERVER_ERROR_ZH,MessagesConstant.MESSAGES_INTERNAL_SERVER_ERROR_EN); |
|
} |
|
|
|
log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage()); |
|
|
|
return ServletUtils.webFluxResponseWriter(response, msg); |
|
} |
|
|
|
|
|
} |