optimize: 所有JSON解析失败的异常捕获及处理
parent
3ae6282870
commit
2a7550368b
|
@ -11,8 +11,14 @@ async function startup () {
|
||||||
const configPath = './user_config.json5'
|
const configPath = './user_config.json5'
|
||||||
if (fs.existsSync(configPath)) {
|
if (fs.existsSync(configPath)) {
|
||||||
const file = fs.readFileSync(configPath)
|
const file = fs.readFileSync(configPath)
|
||||||
const userConfig = jsonApi.parse(file.toString())
|
let userConfig
|
||||||
console.info('读取 user_config.json5 成功:', configPath)
|
try {
|
||||||
|
userConfig = jsonApi.parse(file.toString())
|
||||||
|
console.info(`读取和解析 user_config.json5 成功:${configPath}`)
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`读取或解析 user_config.json5 失败: ${configPath}, error:`, e)
|
||||||
|
userConfig = {}
|
||||||
|
}
|
||||||
DevSidecar.api.config.set(userConfig)
|
DevSidecar.api.config.set(userConfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -15,7 +15,13 @@ if (process.argv && process.argv.length > 3) {
|
||||||
|
|
||||||
const configJson = fs.readFileSync(configPath)
|
const configJson = fs.readFileSync(configPath)
|
||||||
log.info('读取 running.json by core 成功:', configPath)
|
log.info('读取 running.json by core 成功:', configPath)
|
||||||
const config = jsonApi.parse(configJson.toString())
|
let config
|
||||||
|
try {
|
||||||
|
config = jsonApi.parse(configJson.toString())
|
||||||
|
} catch (e) {
|
||||||
|
log.error(`running.json 文件内容格式不正确,文件路径:${configPath},文件内容: ${configJson.toString()}, error:`, e)
|
||||||
|
config = {}
|
||||||
|
}
|
||||||
// const scriptDir = '../../gui/extra/scripts/'
|
// const scriptDir = '../../gui/extra/scripts/'
|
||||||
// config.setting.script.defaultDir = path.join(__dirname, scriptDir)
|
// config.setting.script.defaultDir = path.join(__dirname, scriptDir)
|
||||||
// const pacFilePath = '../../gui/extra/pac/pac.txt'
|
// const pacFilePath = '../../gui/extra/pac/pac.txt'
|
||||||
|
|
|
@ -146,7 +146,12 @@ const configApi = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
readRemoteConfig (suffix = '') {
|
readRemoteConfig (suffix = '') {
|
||||||
|
try {
|
||||||
return jsonApi.parse(configApi.readRemoteConfigStr(suffix))
|
return jsonApi.parse(configApi.readRemoteConfigStr(suffix))
|
||||||
|
} catch (e) {
|
||||||
|
log.error(`读取远程配置失败,suffix: ${suffix}`, e)
|
||||||
|
return {}
|
||||||
|
}
|
||||||
},
|
},
|
||||||
readRemoteConfigStr (suffix = '') {
|
readRemoteConfigStr (suffix = '') {
|
||||||
if (get().app.remoteConfig.enabled !== true) {
|
if (get().app.remoteConfig.enabled !== true) {
|
||||||
|
@ -223,7 +228,12 @@ const configApi = {
|
||||||
const file = fs.readFileSync(configPath)
|
const file = fs.readFileSync(configPath)
|
||||||
log.info('读取 config.json 成功:', configPath)
|
log.info('读取 config.json 成功:', configPath)
|
||||||
const fileStr = file.toString()
|
const fileStr = file.toString()
|
||||||
userConfig = fileStr && fileStr.length > 2 ? jsonApi.parse(fileStr) : {}
|
try {
|
||||||
|
userConfig = jsonApi.parse(fileStr)
|
||||||
|
} catch (e) {
|
||||||
|
log.error(`config.json 文件内容格式不正确,文件路径:${configPath},文件内容: ${fileStr}, error:`, e)
|
||||||
|
userConfig = {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const config = configApi.set(userConfig)
|
const config = configApi.set(userConfig)
|
||||||
|
|
|
@ -479,7 +479,12 @@ function _getConfig () {
|
||||||
return {}
|
return {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
return jsonApi.parse(fs.readFileSync(configFilePath))
|
return jsonApi.parse(fs.readFileSync(configFilePath))
|
||||||
|
} catch (e) {
|
||||||
|
console.error('读取配置文件失败:', configFilePath, e)
|
||||||
|
return {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function _getRemoteSavePath (suffix = '') {
|
function _getRemoteSavePath (suffix = '') {
|
||||||
|
@ -516,7 +521,12 @@ function _readRemoteConfigStr (suffix = '') {
|
||||||
}
|
}
|
||||||
|
|
||||||
function _readRemoteConfig (suffix = '') {
|
function _readRemoteConfig (suffix = '') {
|
||||||
|
try {
|
||||||
return jsonApi.parse(_readRemoteConfigStr(suffix))
|
return jsonApi.parse(_readRemoteConfigStr(suffix))
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`读取远程配置失败,suffix: ${suffix}`, e)
|
||||||
|
return {}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function _getConfigFromFiles () {
|
function _getConfigFromFiles () {
|
||||||
|
|
|
@ -100,8 +100,12 @@ const localApi = {
|
||||||
},
|
},
|
||||||
save (setting = {}) {
|
save (setting = {}) {
|
||||||
const settingPath = _getSettingsPath()
|
const settingPath = _getSettingsPath()
|
||||||
|
try {
|
||||||
fs.writeFileSync(settingPath, jsonApi.stringify(setting))
|
fs.writeFileSync(settingPath, jsonApi.stringify(setting))
|
||||||
log.info('保存 setting.json 配置文件成功:', settingPath)
|
log.info('保存 setting.json 配置文件成功:', settingPath)
|
||||||
|
} catch (e) {
|
||||||
|
log.error('保存 setting.json 配置文件失败:', settingPath, e)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -7,7 +7,13 @@ const log = require('@docmirror/mitmproxy/src/utils/util.log') // 当前脚本
|
||||||
const configPath = process.argv[2]
|
const configPath = process.argv[2]
|
||||||
const configJson = fs.readFileSync(configPath)
|
const configJson = fs.readFileSync(configPath)
|
||||||
log.info('读取 running.json by gui bridge 成功:', configPath)
|
log.info('读取 running.json by gui bridge 成功:', configPath)
|
||||||
const config = jsonApi.parse(configJson.toString())
|
let config
|
||||||
|
try {
|
||||||
|
config = jsonApi.parse(configJson.toString())
|
||||||
|
} catch (e) {
|
||||||
|
log.error(`running.json 文件内容格式不正确,文件路径:${configPath},文件内容: ${configJson.toString()}, error:`, e)
|
||||||
|
config = {}
|
||||||
|
}
|
||||||
// const scriptDir = '../extra/scripts/'
|
// const scriptDir = '../extra/scripts/'
|
||||||
// config.setting.script.defaultDir = path.join(__dirname, scriptDir)
|
// config.setting.script.defaultDir = path.join(__dirname, scriptDir)
|
||||||
// const pacFilePath = '../extra/pac/pac.txt'
|
// const pacFilePath = '../extra/pac/pac.txt'
|
||||||
|
|
|
@ -5,11 +5,19 @@ if (JSON5.default) {
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
parse (str) {
|
parse (str, defaultValue) {
|
||||||
if (str == null || str.length < 2) {
|
if (str == null || str.length < 2) {
|
||||||
return {}
|
return defaultValue || {}
|
||||||
}
|
}
|
||||||
|
if (defaultValue != null) {
|
||||||
|
try {
|
||||||
return JSON5.parse(str)
|
return JSON5.parse(str)
|
||||||
|
} catch {
|
||||||
|
return defaultValue
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return JSON5.parse(str)
|
||||||
|
}
|
||||||
},
|
},
|
||||||
stringify (obj) {
|
stringify (obj) {
|
||||||
return JSON.stringify(obj, null, '\t')
|
return JSON.stringify(obj, null, '\t')
|
||||||
|
|
|
@ -62,13 +62,18 @@ function _loadFromFile (defaultConfig) {
|
||||||
const file = fs.readFileSync(configPath)
|
const file = fs.readFileSync(configPath)
|
||||||
log.info('读取 automaticCompatibleConfig.json 成功:', configPath)
|
log.info('读取 automaticCompatibleConfig.json 成功:', configPath)
|
||||||
const fileStr = file.toString()
|
const fileStr = file.toString()
|
||||||
config = fileStr && fileStr.length > 2 ? jsonApi.parse(fileStr) : defaultConfig
|
try {
|
||||||
|
config = jsonApi.parse(fileStr)
|
||||||
if (config.connect == null) {
|
if (config.connect == null) {
|
||||||
config.connect = defaultConfig.connect
|
config.connect = defaultConfig.connect
|
||||||
}
|
}
|
||||||
if (config.request == null) {
|
if (config.request == null) {
|
||||||
config.request = defaultConfig.request
|
config.request = defaultConfig.request
|
||||||
}
|
}
|
||||||
|
} catch (e) {
|
||||||
|
log.error('解析 automaticCompatibleConfig.json 成功:', configPath, ', error:', e)
|
||||||
|
return defaultConfig
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
|
|
Loading…
Reference in New Issue