zhou zhou
23 小时以前 10776dd6f7f9ef9e47419427fcb1b692ed73d54d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.vincent.rsf.openApi.feign.wms.fallback;
 
import com.vincent.rsf.framework.common.R;
import com.vincent.rsf.openApi.entity.phyz.InventoryQueryCondition;
import com.vincent.rsf.openApi.feign.wms.WmsServerFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
 
/**
 * WMS Server Feign客户端降级处理
 */
@Slf4j
@Component
public class WmsServerFeignClientFallback implements WmsServerFeignClient {
 
    @Override
    public R queryInventoryDetails(InventoryQueryCondition condition) {
        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);
    }
 
    /**
     * 过滤错误消息中的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;
    }
}