Skip to content

Commit 2a75503

Browse files
optimize: 所有JSON解析失败的异常捕获及处理
1 parent 3ae6282 commit 2a75503

File tree

8 files changed

+74
-19
lines changed

8 files changed

+74
-19
lines changed

packages/cli/src/index.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,14 @@ async function startup () {
1111
const configPath = './user_config.json5'
1212
if (fs.existsSync(configPath)) {
1313
const file = fs.readFileSync(configPath)
14-
const userConfig = jsonApi.parse(file.toString())
15-
console.info('读取 user_config.json5 成功:', configPath)
14+
let userConfig
15+
try {
16+
userConfig = jsonApi.parse(file.toString())
17+
console.info(`读取和解析 user_config.json5 成功:${configPath}`)
18+
} catch (e) {
19+
console.error(`读取或解析 user_config.json5 失败: ${configPath}, error:`, e)
20+
userConfig = {}
21+
}
1622
DevSidecar.api.config.set(userConfig)
1723
}
1824

packages/cli/src/mitmproxy.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@ if (process.argv && process.argv.length > 3) {
1515

1616
const configJson = fs.readFileSync(configPath)
1717
log.info('读取 running.json by core 成功:', configPath)
18-
const config = jsonApi.parse(configJson.toString())
18+
let config
19+
try {
20+
config = jsonApi.parse(configJson.toString())
21+
} catch (e) {
22+
log.error(`running.json 文件内容格式不正确,文件路径:${configPath},文件内容: ${configJson.toString()}, error:`, e)
23+
config = {}
24+
}
1925
// const scriptDir = '../../gui/extra/scripts/'
2026
// config.setting.script.defaultDir = path.join(__dirname, scriptDir)
2127
// const pacFilePath = '../../gui/extra/pac/pac.txt'

packages/core/src/config.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,12 @@ const configApi = {
146146
}
147147
},
148148
readRemoteConfig (suffix = '') {
149-
return jsonApi.parse(configApi.readRemoteConfigStr(suffix))
149+
try {
150+
return jsonApi.parse(configApi.readRemoteConfigStr(suffix))
151+
} catch (e) {
152+
log.error(`读取远程配置失败,suffix: ${suffix}`, e)
153+
return {}
154+
}
150155
},
151156
readRemoteConfigStr (suffix = '') {
152157
if (get().app.remoteConfig.enabled !== true) {
@@ -223,7 +228,12 @@ const configApi = {
223228
const file = fs.readFileSync(configPath)
224229
log.info('读取 config.json 成功:', configPath)
225230
const fileStr = file.toString()
226-
userConfig = fileStr && fileStr.length > 2 ? jsonApi.parse(fileStr) : {}
231+
try {
232+
userConfig = jsonApi.parse(fileStr)
233+
} catch (e) {
234+
log.error(`config.json 文件内容格式不正确,文件路径:${configPath},文件内容: ${fileStr}, error:`, e)
235+
userConfig = {}
236+
}
227237
}
228238

229239
const config = configApi.set(userConfig)

packages/core/src/config/index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,12 @@ function _getConfig () {
479479
return {}
480480
}
481481

482-
return jsonApi.parse(fs.readFileSync(configFilePath))
482+
try {
483+
return jsonApi.parse(fs.readFileSync(configFilePath))
484+
} catch (e) {
485+
console.error('读取配置文件失败:', configFilePath, e)
486+
return {}
487+
}
483488
}
484489

485490
function _getRemoteSavePath (suffix = '') {
@@ -516,7 +521,12 @@ function _readRemoteConfigStr (suffix = '') {
516521
}
517522

518523
function _readRemoteConfig (suffix = '') {
519-
return jsonApi.parse(_readRemoteConfigStr(suffix))
524+
try {
525+
return jsonApi.parse(_readRemoteConfigStr(suffix))
526+
} catch (e) {
527+
console.error(`读取远程配置失败,suffix: ${suffix}`, e)
528+
return {}
529+
}
520530
}
521531

522532
function _getConfigFromFiles () {

packages/gui/src/bridge/api/backend.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,12 @@ const localApi = {
100100
},
101101
save (setting = {}) {
102102
const settingPath = _getSettingsPath()
103-
fs.writeFileSync(settingPath, jsonApi.stringify(setting))
104-
log.info('保存 setting.json 配置文件成功:', settingPath)
103+
try {
104+
fs.writeFileSync(settingPath, jsonApi.stringify(setting))
105+
log.info('保存 setting.json 配置文件成功:', settingPath)
106+
} catch (e) {
107+
log.error('保存 setting.json 配置文件失败:', settingPath, e)
108+
}
105109
},
106110
},
107111
/**

packages/gui/src/bridge/mitmproxy.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,13 @@ const log = require('@docmirror/mitmproxy/src/utils/util.log') // 当前脚本
77
const configPath = process.argv[2]
88
const configJson = fs.readFileSync(configPath)
99
log.info('读取 running.json by gui bridge 成功:', configPath)
10-
const config = jsonApi.parse(configJson.toString())
10+
let config
11+
try {
12+
config = jsonApi.parse(configJson.toString())
13+
} catch (e) {
14+
log.error(`running.json 文件内容格式不正确,文件路径:${configPath},文件内容: ${configJson.toString()}, error:`, e)
15+
config = {}
16+
}
1117
// const scriptDir = '../extra/scripts/'
1218
// config.setting.script.defaultDir = path.join(__dirname, scriptDir)
1319
// const pacFilePath = '../extra/pac/pac.txt'

packages/mitmproxy/src/json.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,19 @@ if (JSON5.default) {
55
}
66

77
module.exports = {
8-
parse (str) {
8+
parse (str, defaultValue) {
99
if (str == null || str.length < 2) {
10-
return {}
10+
return defaultValue || {}
11+
}
12+
if (defaultValue != null) {
13+
try {
14+
return JSON5.parse(str)
15+
} catch {
16+
return defaultValue
17+
}
18+
} else {
19+
return JSON5.parse(str)
1120
}
12-
return JSON5.parse(str)
1321
},
1422
stringify (obj) {
1523
return JSON.stringify(obj, null, '\t')

packages/mitmproxy/src/lib/proxy/compatible/compatible.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,17 @@ function _loadFromFile (defaultConfig) {
6262
const file = fs.readFileSync(configPath)
6363
log.info('读取 automaticCompatibleConfig.json 成功:', configPath)
6464
const fileStr = file.toString()
65-
config = fileStr && fileStr.length > 2 ? jsonApi.parse(fileStr) : defaultConfig
66-
if (config.connect == null) {
67-
config.connect = defaultConfig.connect
68-
}
69-
if (config.request == null) {
70-
config.request = defaultConfig.request
65+
try {
66+
config = jsonApi.parse(fileStr)
67+
if (config.connect == null) {
68+
config.connect = defaultConfig.connect
69+
}
70+
if (config.request == null) {
71+
config.request = defaultConfig.request
72+
}
73+
} catch (e) {
74+
log.error('解析 automaticCompatibleConfig.json 成功:', configPath, ', error:', e)
75+
return defaultConfig
7176
}
7277
}
7378

0 commit comments

Comments
 (0)