lbq
20 小时以前 076cd7a73eb60c86d43eda67c5625704576d44cc
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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;
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);
    }
 
    @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;
    }
}