refactor: address code review feedback - fix normalizeApiBase regex, improve error code propagation
Co-authored-by: qinfendebingshuo <207279273+qinfendebingshuo@users.noreply.github.com>
This commit is contained in:
parent
3eaf3b2c83
commit
523e4281fc
|
|
@ -81,12 +81,16 @@ function fetchModels(apiBase, apiKey, timeoutMs) {
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('error', (err) => {
|
req.on('error', (err) => {
|
||||||
reject(new Error('API Base 不可访问: ' + err.message));
|
const wrapped = new Error('API Base 不可访问: ' + err.message);
|
||||||
|
wrapped.code = err.code;
|
||||||
|
reject(wrapped);
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on('timeout', () => {
|
req.on('timeout', () => {
|
||||||
req.destroy();
|
req.destroy();
|
||||||
reject(new Error('API Base 不可访问(请求超时)'));
|
const err = new Error('API Base 不可访问(请求超时)');
|
||||||
|
err.code = 'ETIMEDOUT';
|
||||||
|
reject(err);
|
||||||
});
|
});
|
||||||
|
|
||||||
req.end();
|
req.end();
|
||||||
|
|
@ -230,14 +234,15 @@ router.post('/detect-models', async (req, res) => {
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const errMsg = err.message || '模型检测失败';
|
const errMsg = err.message || '模型检测失败';
|
||||||
|
const errCode = err.code || '';
|
||||||
let code = 'DETECT_FAILED';
|
let code = 'DETECT_FAILED';
|
||||||
|
|
||||||
// 区分 DNS / 网络 / 超时错误
|
// 区分 DNS / 网络 / 超时错误(优先使用 Node.js 错误码)
|
||||||
if (/ENOTFOUND|getaddrinfo/.test(errMsg)) {
|
if (errCode === 'ENOTFOUND' || errCode === 'EAI_AGAIN' || /ENOTFOUND|getaddrinfo/.test(errMsg)) {
|
||||||
code = 'DNS_ERROR';
|
code = 'DNS_ERROR';
|
||||||
} else if (/ECONNREFUSED|ECONNRESET|EHOSTUNREACH|ENETUNREACH|socket hang up/.test(errMsg)) {
|
} else if (errCode === 'ECONNREFUSED' || errCode === 'ECONNRESET' || errCode === 'EHOSTUNREACH' || errCode === 'ENETUNREACH' || /ECONNREFUSED|ECONNRESET|EHOSTUNREACH|ENETUNREACH|socket hang up/.test(errMsg)) {
|
||||||
code = 'NETWORK_ERROR';
|
code = 'NETWORK_ERROR';
|
||||||
} else if (/timeout|ETIMEDOUT|请求超时/.test(errMsg)) {
|
} else if (errCode === 'ETIMEDOUT' || errCode === 'ESOCKETTIMEDOUT' || /timeout|ETIMEDOUT/.test(errMsg)) {
|
||||||
code = 'TIMEOUT';
|
code = 'TIMEOUT';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -195,7 +195,9 @@ async function streamApiKeyReply(text) {
|
||||||
if (d === '[DONE]') continue;
|
if (d === '[DONE]') continue;
|
||||||
try {
|
try {
|
||||||
var parsed = JSON.parse(d);
|
var parsed = JSON.parse(d);
|
||||||
var delta = parsed.choices && parsed.choices[0] && parsed.choices[0].delta && parsed.choices[0].delta.content;
|
var delta = parsed.choices && parsed.choices[0]
|
||||||
|
&& parsed.choices[0].delta
|
||||||
|
&& parsed.choices[0].delta.content;
|
||||||
if (delta) {
|
if (delta) {
|
||||||
full += delta;
|
full += delta;
|
||||||
streamEl.textContent = full;
|
streamEl.textContent = full;
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@
|
||||||
*/
|
*/
|
||||||
function normalizeApiBase(base) {
|
function normalizeApiBase(base) {
|
||||||
var normalized = base.replace(/\/+$/, '');
|
var normalized = base.replace(/\/+$/, '');
|
||||||
if (!normalized.match(/\/v\d+\b/) && !normalized.endsWith('/openai')) {
|
if (!normalized.match(/\/v\d+(\/|$)/) && !normalized.endsWith('/openai')) {
|
||||||
normalized += '/v1';
|
normalized += '/v1';
|
||||||
}
|
}
|
||||||
return normalized;
|
return normalized;
|
||||||
|
|
@ -185,12 +185,12 @@
|
||||||
/* 构建探测列表:用户端点优先,然后已知端点 */
|
/* 构建探测列表:用户端点优先,然后已知端点 */
|
||||||
var candidates = [];
|
var candidates = [];
|
||||||
if (apiBase) {
|
if (apiBase) {
|
||||||
var userNorm = normalizeApiBase(apiBase);
|
var normalizedUserBase = normalizeApiBase(apiBase);
|
||||||
candidates.push({ label: apiBase, base: userNorm });
|
candidates.push({ label: apiBase, base: normalizedUserBase });
|
||||||
}
|
}
|
||||||
for (var i = 0; i < KNOWN_ENDPOINTS.length; i++) {
|
for (var i = 0; i < KNOWN_ENDPOINTS.length; i++) {
|
||||||
var ep = KNOWN_ENDPOINTS[i];
|
var ep = KNOWN_ENDPOINTS[i];
|
||||||
if (!apiBase || ep.base !== normalizeApiBase(apiBase)) {
|
if (!apiBase || ep.base !== normalizedUserBase) {
|
||||||
candidates.push(ep);
|
candidates.push(ep);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue