lbq
22 小时以前 076cd7a73eb60c86d43eda67c5625704576d44cc
rsf-open-api/src/main/java/com/vincent/rsf/openApi/feign/wms/fallback/WmsServerFeignClientFallback.java
@@ -1,5 +1,6 @@
package com.vincent.rsf.openApi.feign.wms.fallback;
import com.alibaba.fastjson.JSONObject;
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.openApi.entity.phyz.InventoryQueryCondition;
import com.vincent.rsf.openApi.feign.wms.WmsServerFeignClient;
@@ -15,7 +16,132 @@
    @Override
    public R queryInventoryDetails(InventoryQueryCondition condition) {
        log.error("调用WMS Server库存查询明细接口失败,触发降级处理");
        return R.error("服务调用失败,请稍后重试");
        return queryInventoryDetails(condition, null);
    }
    /**
     * 库存查询明细降级处理(带异常信息)
     * @param condition 查询条件
     * @param throwable 异常信息(可选)
     * @return 错误响应
     */
    public R queryInventoryDetails(InventoryQueryCondition condition, Throwable throwable) {
        log.error("调用WMS Server库存查询明细接口失败,触发降级处理", throwable);
        String errorMessage = filterErrorMessage(throwable);
        return R.error(errorMessage);
    }
    @Override
    public R queryInventorySummary(InventoryQueryCondition condition) {
        return queryInventorySummary(condition, null);
    }
    /**
     * 库存查询汇总降级处理(带异常信息)
     * @param condition 查询条件
     * @param throwable 异常信息(可选)
     * @return 错误响应
     */
    public R queryInventorySummary(InventoryQueryCondition condition, Throwable throwable) {
        log.error("调用WMS Server库存查询汇总接口失败,触发降级处理", throwable);
        String errorMessage = filterErrorMessage(throwable);
        return R.error(errorMessage);
    }
    @Override
    public R callForEmptyContainers(JSONObject condition) {
        return callForEmptyContainers(condition, null);
    }
    public R callForEmptyContainers(JSONObject condition, Throwable throwable) {
        log.error("调用WMS Server空托出库接口失败,触发降级处理", throwable);
        String errorMessage = filterErrorMessage(throwable);
        return R.error(errorMessage);
    }
    @Override
    public R emptyContainerWarehousing(JSONObject condition) {
        return emptyContainerWarehousing(condition, null);
    }
    public R emptyContainerWarehousing(JSONObject condition, Throwable throwable) {
        log.error("调用WMS Server空托入库接口失败,触发降级处理", throwable);
        String errorMessage = filterErrorMessage(throwable);
        return R.error(errorMessage);
    }
    @Override
    public R mesAddTask(JSONObject condition) {
        return mesAddTask(condition, null);
    }
    public R mesAddTask(JSONObject condition, Throwable throwable) {
        log.error("调用WMS Servermes下发agv运输任务接口失败,触发降级处理", throwable);
        String errorMessage = filterErrorMessage(throwable);
        return R.error(errorMessage);
    }
    @Override
    public R mesCancelTask(JSONObject condition) {
        return mesCancelTask(condition, null);
    }
    public R mesCancelTask(JSONObject condition, Throwable throwable) {
        log.error("调用WMS Servermes取消agv运输任务接口失败,触发降级处理", throwable);
        String errorMessage = filterErrorMessage(throwable);
        return R.error(errorMessage);
    }
    /**
     * 过滤错误消息中的URL,只保留错误类型
     * @param throwable 异常对象(可选)
     * @return 过滤后的完整错误消息(包含"查询失败:"前缀)
     */
    public static String filterErrorMessage(Throwable throwable) {
        if (throwable == null) {
            return "查询失败:服务调用失败,请稍后重试";
        }
        return filterErrorMessage(throwable.getMessage());
    }
    /**
     * 过滤错误消息中的URL,只保留错误类型
     * @param errorMessage 错误消息字符串(可选)
     * @return 过滤后的完整错误消息(包含"查询失败:"前缀)
     */
    public static String filterErrorMessage(String errorMessage) {
        if (errorMessage == null || errorMessage.isEmpty()) {
            return "查询失败:未知错误";
        }
        String filteredMessage = errorMessage;
        // 如果包含"executing",说明是HTTP请求错误,去掉URL部分
        if (filteredMessage.contains("executing")) {
            int executingIndex = filteredMessage.indexOf("executing");
            if (executingIndex > 0) {
                // 提取"executing"之前的部分(如"Read timed out")
                filteredMessage = filteredMessage.substring(0, executingIndex).trim();
            } else {
                // 如果"executing"在开头,使用默认错误消息
                filteredMessage = "请求超时";
            }
        }
        // 如果包含"http://"或"https://",也尝试去掉URL部分
        else if (filteredMessage.contains("http://") || filteredMessage.contains("https://")) {
            // 使用正则表达式去掉URL
            filteredMessage = filteredMessage.replaceAll("https?://[^\\s]+", "").trim();
            if (filteredMessage.isEmpty()) {
                filteredMessage = "请求失败";
            }
        }
        // 如果过滤后的消息为空,使用默认错误消息
        if (filteredMessage.isEmpty()) {
            filteredMessage = "未知错误";
        }
        // 返回包含"查询失败:"前缀的完整错误消息
        return "查询失败:" + filteredMessage;
    }
}