zhibing.pu
2024-08-19 b9522d131e168a92c88f23e2c0a3a2df079926b4
ruoyi-gateway/src/main/java/com/ruoyi/gateway/handler/GatewayExceptionHandler.java
@@ -19,38 +19,30 @@
 */
@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 = "服务未找到";
        }
        else if (ex instanceof ResponseStatusException)
        {
            ResponseStatusException responseStatusException = (ResponseStatusException) ex;
            msg = responseStatusException.getMessage();
        }
        else
        {
            msg = "内部服务器错误";
        }
        log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage());
        return ServletUtils.webFluxResponseWriter(response, msg);
    }
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 = "服务未找到";
      } else if (ex instanceof ResponseStatusException) {
         ResponseStatusException responseStatusException = (ResponseStatusException) ex;
         msg = responseStatusException.getMessage();
      } else {
         msg = "内部服务器错误";
      }
      log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage());
      return ServletUtils.webFluxResponseWriter(response, msg);
   }
}