解决了config.js浏览器控制台报错的问题
This commit is contained in:
@@ -35,26 +35,17 @@ async function apiRequest(endpoint, method = 'GET', data = null) {
|
|||||||
const responseText = await response.text();
|
const responseText = await response.text();
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
// 尝试解析错误响应
|
// 优化错误响应处理
|
||||||
let errorData = {};
|
console.warn(`API请求失败: ${response.status}`);
|
||||||
|
|
||||||
|
// 尝试解析JSON,但如果失败,直接使用原始文本作为错误信息
|
||||||
try {
|
try {
|
||||||
// 首先检查响应文本是否为空或不是有效JSON
|
const errorData = JSON.parse(responseText);
|
||||||
if (!responseText || responseText.trim() === '') {
|
return { error: errorData.error || responseText || `请求失败: ${response.status}` };
|
||||||
console.warn('错误响应为空');
|
} catch (parseError) {
|
||||||
} else {
|
// 当响应不是有效的JSON时(如中文错误信息),直接使用原始文本
|
||||||
try {
|
console.warn('非JSON格式错误响应:', responseText);
|
||||||
errorData = JSON.parse(responseText);
|
return { error: responseText || `请求失败: ${response.status}` };
|
||||||
} catch (parseError) {
|
|
||||||
console.error('无法解析错误响应为JSON:', parseError);
|
|
||||||
console.error('原始错误响应文本:', responseText);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 直接返回错误信息,而不是抛出异常,让上层处理
|
|
||||||
console.warn(`API请求失败: ${response.status}`, errorData);
|
|
||||||
return { error: errorData.error || `请求失败: ${response.status}` };
|
|
||||||
} catch (e) {
|
|
||||||
console.error('处理错误响应时出错:', e);
|
|
||||||
return { error: `请求处理失败: ${e.message}` };
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -37,10 +37,18 @@ function initConfigPage() {
|
|||||||
async function loadConfig() {
|
async function loadConfig() {
|
||||||
try {
|
try {
|
||||||
showLoading(true);
|
showLoading(true);
|
||||||
const config = await api.getConfig();
|
const result = await api.getConfig();
|
||||||
populateConfigForm(config);
|
|
||||||
|
// 检查API返回的错误
|
||||||
|
if (result && result.error) {
|
||||||
|
showErrorMessage('加载配置失败: ' + result.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
populateConfigForm(result);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showErrorMessage('加载配置失败: ' + error.message);
|
// 捕获可能的异常(虽然apiRequest不应该再抛出异常)
|
||||||
|
showErrorMessage('加载配置失败: ' + (error.message || '未知错误'));
|
||||||
} finally {
|
} finally {
|
||||||
showLoading(false);
|
showLoading(false);
|
||||||
}
|
}
|
||||||
@@ -67,7 +75,8 @@ function populateConfigForm(config) {
|
|||||||
setElementValue('shield-local-rules-file', getSafeValue(shieldConfig.LocalRulesFile, 'data/rules.txt'));
|
setElementValue('shield-local-rules-file', getSafeValue(shieldConfig.LocalRulesFile, 'data/rules.txt'));
|
||||||
setElementValue('shield-update-interval', getSafeValue(shieldConfig.UpdateInterval, 3600));
|
setElementValue('shield-update-interval', getSafeValue(shieldConfig.UpdateInterval, 3600));
|
||||||
setElementValue('shield-hosts-file', getSafeValue(shieldConfig.HostsFile, 'data/hosts.txt'));
|
setElementValue('shield-hosts-file', getSafeValue(shieldConfig.HostsFile, 'data/hosts.txt'));
|
||||||
setElementValue('shield-block-method', getSafeValue(shieldConfig.BlockMethod, '0.0.0.0'));
|
// 使用服务器端接受的屏蔽方法值,默认使用NXDOMAIN
|
||||||
|
setElementValue('shield-block-method', getSafeValue(shieldConfig.BlockMethod, 'NXDOMAIN'));
|
||||||
}
|
}
|
||||||
|
|
||||||
// 工具函数:安全设置元素值
|
// 工具函数:安全设置元素值
|
||||||
@@ -98,10 +107,18 @@ async function handleSaveConfig() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
showLoading(true);
|
showLoading(true);
|
||||||
await api.saveConfig(formData);
|
const result = await api.saveConfig(formData);
|
||||||
|
|
||||||
|
// 检查API返回的错误
|
||||||
|
if (result && result.error) {
|
||||||
|
showErrorMessage('保存配置失败: ' + result.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
showSuccessMessage('配置保存成功');
|
showSuccessMessage('配置保存成功');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showErrorMessage('保存配置失败: ' + error.message);
|
// 捕获可能的异常(虽然apiRequest不应该再抛出异常)
|
||||||
|
showErrorMessage('保存配置失败: ' + (error.message || '未知错误'));
|
||||||
} finally {
|
} finally {
|
||||||
showLoading(false);
|
showLoading(false);
|
||||||
}
|
}
|
||||||
@@ -113,10 +130,18 @@ async function handleRestartService() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
showLoading(true);
|
showLoading(true);
|
||||||
await api.restartService();
|
const result = await api.restartService();
|
||||||
|
|
||||||
|
// 检查API返回的错误
|
||||||
|
if (result && result.error) {
|
||||||
|
showErrorMessage('服务重启失败: ' + result.error);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
showSuccessMessage('服务重启成功');
|
showSuccessMessage('服务重启成功');
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
showErrorMessage('重启服务失败: ' + error.message);
|
// 捕获可能的异常(虽然apiRequest不应该再抛出异常)
|
||||||
|
showErrorMessage('重启服务失败: ' + (error.message || '未知错误'));
|
||||||
} finally {
|
} finally {
|
||||||
showLoading(false);
|
showLoading(false);
|
||||||
}
|
}
|
||||||
@@ -172,7 +197,7 @@ function collectFormData() {
|
|||||||
LocalRulesFile: getElementValue('shield-local-rules-file') || './data/rules.txt',
|
LocalRulesFile: getElementValue('shield-local-rules-file') || './data/rules.txt',
|
||||||
UpdateInterval: updateInterval,
|
UpdateInterval: updateInterval,
|
||||||
HostsFile: getElementValue('shield-hosts-file') || './data/hosts.txt',
|
HostsFile: getElementValue('shield-hosts-file') || './data/hosts.txt',
|
||||||
BlockMethod: getElementValue('shield-block-method') || '0.0.0.0'
|
BlockMethod: getElementValue('shield-block-method') || 'NXDOMAIN'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user