@agentscope-ai/i18n 0.1.8 → 0.1.9-rc.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/utils/medusa.js +59 -19
- package/package.json +1 -1
package/lib/utils/medusa.js
CHANGED
|
@@ -11,26 +11,66 @@ const MEDUSA = {
|
|
|
11
11
|
host: 'http://mcms-portal.alibaba-inc.com',
|
|
12
12
|
};
|
|
13
13
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Fetch data from Medusa API with retry on failure.
|
|
16
|
+
* Retries up to `maxRetries` times with exponential backoff.
|
|
17
|
+
*
|
|
18
|
+
* Retryable conditions:
|
|
19
|
+
* - Network errors (ECONNREFUSED, ENOTFOUND, ETIMEDOUT, etc.)
|
|
20
|
+
* - HTTP 5xx server errors
|
|
21
|
+
* - Response is HTML instead of JSON (gateway/CDN error pages with 200 status)
|
|
22
|
+
*
|
|
23
|
+
* Non-retryable: HTTP 4xx client errors (404, 403, etc.)
|
|
24
|
+
*/
|
|
25
|
+
async function getMedusaData(url, maxRetries = 3) {
|
|
26
|
+
let lastError;
|
|
27
|
+
|
|
28
|
+
for (let attempt = 1; attempt <= maxRetries; attempt++) {
|
|
29
|
+
try {
|
|
30
|
+
const response = await axios.get(url, {
|
|
31
|
+
httpsAgent: new (require('https').Agent)({
|
|
32
|
+
rejectUnauthorized: false,
|
|
33
|
+
keepAlive: false,
|
|
34
|
+
}),
|
|
35
|
+
timeout: 30000,
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const { data } = response;
|
|
39
|
+
|
|
40
|
+
// Guard against gateway/CDN returning HTML error pages with 200 status.
|
|
41
|
+
// Valid Medusa responses are always JSON (object or array).
|
|
42
|
+
if (typeof data === 'string' && data.trimStart().startsWith('<')) {
|
|
43
|
+
const preview = data.substring(0, 200);
|
|
44
|
+
const err = new Error(
|
|
45
|
+
`Expected JSON response but received HTML (possible gateway error). Preview: ${preview}`,
|
|
46
|
+
);
|
|
47
|
+
err._retryable = true;
|
|
48
|
+
throw err;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return data;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
lastError = error;
|
|
54
|
+
|
|
55
|
+
const status = error.response?.status;
|
|
56
|
+
const isRetryable = error._retryable || !status || status >= 500;
|
|
57
|
+
|
|
58
|
+
if (attempt < maxRetries && isRetryable) {
|
|
59
|
+
const delay = Math.min(1000 * Math.pow(2, attempt - 1), 10000);
|
|
60
|
+
console.warn(
|
|
61
|
+
`[Retry ${attempt}/${maxRetries}] ${error.message}. Retrying in ${delay / 1000}s...`,
|
|
62
|
+
);
|
|
63
|
+
await new Promise((resolve) => setTimeout(resolve, delay));
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
console.error('获取 Medusa 数据失败:', error.message);
|
|
68
|
+
if (error.response) {
|
|
69
|
+
console.error('HTTP 状态码:', error.response.status);
|
|
70
|
+
console.error('响应数据:', error.response.data);
|
|
71
|
+
}
|
|
72
|
+
throw error;
|
|
32
73
|
}
|
|
33
|
-
throw error;
|
|
34
74
|
}
|
|
35
75
|
}
|
|
36
76
|
|