optimize: 捕获所有文件保存失败的异常,并记录日志

pull/445/head
王良 2025-01-24 15:09:51 +08:00
parent 17cc3db33d
commit f44dc790c7
7 changed files with 74 additions and 29 deletions

View File

@ -86,8 +86,14 @@ const configApi = {
if (remoteConfig != null) {
const remoteSavePath = configLoader.getRemoteConfigPath(suffix)
try {
fs.writeFileSync(remoteSavePath, body)
log.info('保存远程配置文件成功:', remoteSavePath)
} catch (e) {
log.error('保存远程配置文件失败:', remoteSavePath, ', error:', e)
reject(new Error(`保存远程配置文件失败: ${e.message}`))
return
}
} else {
log.warn('远程配置对象为空:', remoteConfigUrl)
}
@ -153,8 +159,13 @@ const configApi = {
// 将差异作为用户配置保存到 config.json 中
const configPath = configLoader.getUserConfigPath()
try {
fs.writeFileSync(configPath, jsonApi.stringify(diffConfig))
log.info('保存 config.json 自定义配置文件成功:', configPath)
} catch (e) {
log.error('保存 config.json 自定义配置文件失败:', configPath, ', error:', e)
throw e
}
// 重载配置
const allConfig = configApi.set(diffConfig)
@ -206,14 +217,30 @@ const configApi = {
// 判断文件内容是否为空或空配置
const fileStr = fileOriginalStr.replace(/\s/g, '')
if (fileStr.length < 5) {
try {
fs.writeFileSync(configPath, '{}')
} catch (e) {
log.warn('简化用户配置文件失败:', configPath, ', error:', e)
}
return false // config.json 内容为空或为空json
}
// 备份用户自定义配置文件
fs.writeFileSync(`${configPath}.${Date.now()}.bak.json`, fileOriginalStr)
const bakConfigPath = `${configPath}.${Date.now()}.bak.json`
try {
fs.writeFileSync(bakConfigPath, fileOriginalStr)
log.info('备份用户配置文件成功:', bakConfigPath)
} catch (e) {
log.error('备份用户配置文件失败:', bakConfigPath, ', error:', e)
throw e
}
// 原配置文件内容设为空
try {
fs.writeFileSync(configPath, '{}')
} catch (e) {
log.error('初始化用户配置文件失败:', configPath, ', error:', e)
throw e
}
// 重新加载配置
configApi.load(null)

View File

@ -76,8 +76,13 @@ const serverApi = {
// fireStatus('ing') // 启动中
const basePath = serverConfig.setting.userBasePath
const runningConfigPath = path.join(basePath, '/running.json')
try {
fs.writeFileSync(runningConfigPath, jsonApi.stringify(serverConfig))
log.info('保存 running.json 运行时配置文件成功:', runningConfigPath)
} catch (e) {
log.error('保存 running.json 运行时配置文件失败:', runningConfigPath, ', error:', e)
throw e
}
const serverProcess = fork(mitmproxyPath, [runningConfigPath])
server = {
id: serverProcess.pid,

View File

@ -76,8 +76,13 @@ function loadLastModifiedTimeFromTxt (fileTxt) {
// 保存 国内域名白名单 内容到 `~/domestic-domain-allowlist.txt` 文件中
function saveDomesticDomainAllowListFile (fileTxt) {
const filePath = getDomesticDomainAllowListTmpFilePath()
try {
fs.writeFileSync(filePath, fileTxt.replaceAll(/\r\n?/g, '\n'))
log.info('保存 domestic-domain-allowlist.txt 文件成功:', filePath)
} catch (e) {
log.error('保存 domestic-domain-allowlist.txt 文件失败:', filePath, ', error:', e)
return
}
// 尝试解析和修改 domestic-domain-allowlist.txt 文件时间
const lastModifiedTime = loadLastModifiedTimeFromTxt(fileTxt)
@ -98,8 +103,6 @@ function saveDomesticDomainAllowListFile (fileTxt) {
})
})
}
return filePath
}
function formatDate (date) {

View File

@ -104,7 +104,7 @@ const localApi = {
fs.writeFileSync(settingPath, jsonApi.stringify(setting))
log.info('保存 setting.json 配置文件成功:', settingPath)
} catch (e) {
log.error('保存 setting.json 配置文件失败:', settingPath, e)
log.error('保存 setting.json 配置文件失败:', settingPath, ', error:', e)
}
},
},

View File

@ -76,7 +76,7 @@ function _saveConfigToFile () {
fs.writeFileSync(filePath, jsonApi.stringify(config))
log.info('保存 automaticCompatibleConfig.json 成功:', filePath)
} catch (e) {
log.error('保存 automaticCompatibleConfig.json 失败:', filePath, e)
log.error('保存 automaticCompatibleConfig.json 失败:', filePath, ', error:', e)
}
}

View File

@ -68,8 +68,13 @@ function formatDate (date) {
// 保存 pac 内容到 `~/pac.txt` 文件中
function savePacFile (pacTxt) {
const pacFilePath = getTmpPacFilePath()
try {
fs.writeFileSync(pacFilePath, pacTxt)
log.info('保存 pac.txt 文件成功:', pacFilePath)
} catch (e) {
log.error('保存 pac.txt 文件失败:', pacFilePath, ', error:', e)
return
}
// 尝试解析和修改 pac.txt 文件时间
const lastModifiedTime = loadPacLastModifiedTime(pacTxt)
@ -90,8 +95,6 @@ function savePacFile (pacTxt) {
})
})
}
return pacFilePath
}
// 异步下载 pac.txt ,避免影响代理服务的启动速度

View File

@ -250,7 +250,10 @@ utils.initCA = function ({ caCertPath, caKeyPath }) {
caKeyPath,
create: false,
}
} catch {
} catch (e0) {
log.info('证书文件不存在,重新生成:', e0)
try {
const caObj = utils.createCA(config.caName)
const caCert = caObj.cert
@ -262,6 +265,10 @@ utils.initCA = function ({ caCertPath, caKeyPath }) {
fs.writeFileSync(caCertPath, certPem)
fs.writeFileSync(caKeyPath, keyPem)
log.info('生成证书文件成功共2个文件:', caCertPath, caKeyPath)
} catch (e) {
log.error('生成证书文件失败:', caCertPath, caKeyPath, ', error:', e)
throw e
}
}
return {
caCertPath,