Skip to content

Commit

Permalink
optimize: 捕获DS的各种未知异常,并将异常信息记录到日志文件中。
Browse files Browse the repository at this point in the history
  • Loading branch information
wangliang181230 committed Jan 27, 2025
1 parent 1a92762 commit fe060eb
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 37 deletions.
4 changes: 0 additions & 4 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ const log = require('./utils/util.log')

// 避免异常崩溃
process.on('uncaughtException', (err) => {
if (err.code === 'ECONNABORTED') {
// console.error(err.errno)
return
}
log.error('Process Uncaught Exception:', err)
})

Expand Down
62 changes: 38 additions & 24 deletions packages/gui/src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function setTray () {
click: () => {
log.info('force quit')
forceClose = true
quit()
quit('系统托盘图标-退出')
},
},
]
Expand All @@ -112,12 +112,12 @@ function setTray () {
// 当桌面主题更新时
if (isMac) {
nativeTheme.on('updated', () => {
console.log('i am changed')
log.info('i am changed')
if (nativeTheme.shouldUseDarkColors) {
console.log('i am dark.')
log.info('i am dark.')
tray.setImage(iconWhitePath)
} else {
console.log('i am light.')
log.info('i am light.')
tray.setImage(iconBlackPath)
// tray.setPressedImage(iconWhitePath)
}
Expand Down Expand Up @@ -149,23 +149,27 @@ function isLinux () {
return platform === 'linux'
}

function hideWin () {
function hideWin (reason = '') {
if (win) {
if (isLinux()) {
quit()
quit(`is linux, not hide win, do quit, ${reason}`)
return
}
win.hide()
if (isMac && hideDockWhenWinClose) {
app.dock.hide()
}
winIsHidden = true
} else {
log.warn('win is null, do not hide win')
}
}

function showWin () {
if (win) {
win.show()
} else {
log.warn('win is null, do not show win')
}
if (app.dock) {
app.dock.show()
Expand Down Expand Up @@ -204,7 +208,7 @@ function createWindow (startHideWindow, autoQuitIfError = true) {
log.error('创建窗口失败:', e)
dialog.showErrorBox('错误', `创建窗口失败: ${e.message}`)
if (autoQuitIfError) {
quit()
quit('创建窗口失败')
}
return false
}
Expand All @@ -231,7 +235,7 @@ function createWindow (startHideWindow, autoQuitIfError = true) {
}

if (startHideWindow) {
hideWin()
hideWin('startHideWindow')
}

win.on('closed', async (...args) => {
Expand All @@ -242,9 +246,9 @@ function createWindow (startHideWindow, autoQuitIfError = true) {

ipcMain.on('close', async (event, message) => {
if (message.value === 1) {
quit()
quit('ipc receive "close"')
} else {
hideWin()
hideWin('ipc receive "close"')
}
})

Expand All @@ -255,7 +259,7 @@ function createWindow (startHideWindow, autoQuitIfError = true) {
}
e.preventDefault()
if (isLinux()) {
quit()
quit('win close')
return
}
const config = DevSidecar.api.config.get()
Expand All @@ -265,16 +269,16 @@ function createWindow (startHideWindow, autoQuitIfError = true) {
win.webContents.send('close.showTip', closeStrategy)
} else if (closeStrategy === 1) {
// 直接退出
quit()
quit('win close')
} else if (closeStrategy === 2) {
// 隐藏窗口
hideWin()
hideWin('win close')
}
})

win.on('session-end', async (e, ...args) => {
log.info('win session-end:', e, ...args)
await quit()
await quit('win session-end')
})

const shortcut = (event, input) => {
Expand Down Expand Up @@ -302,7 +306,7 @@ function createWindow (startHideWindow, autoQuitIfError = true) {
}
win.webContents.executeJavaScript('config')
.then((value) => {
console.info('window.config:', value, ', key:', input.key)
log.info('window.config:', value, ', key:', input.key)
if (!value || (value.disableBeforeInputEvent !== true && value.disableBeforeInputEvent !== 'true')) {
shortcut(event, input)
}
Expand All @@ -315,6 +319,14 @@ function createWindow (startHideWindow, autoQuitIfError = true) {
// 监听渲染进程发送过来的消息
win.webContents.on('ipc-message', (event, channel, message, ...args) => {
console.info('win ipc-message:', event, channel, message, ...args)

// 记录日志
if (channel && channel.startsWith('[ERROR]')) {
log.error('win ipc-message:', channel.substring(7), message, ...args)
} else {
log.info('win ipc-message:', channel, message, ...args)
}

if (channel === 'change-showHideShortcut') {
registerShowHideShortcut(message)
}
Expand All @@ -324,9 +336,12 @@ function createWindow (startHideWindow, autoQuitIfError = true) {
}

async function beforeQuit () {
log.info('before quit')
return DevSidecar.api.shutdown()
}
async function quit () {
async function quit (reason) {
log.info('app quit:', reason)

if (tray) {
tray.displayBalloon({ title: '正在关闭', content: '关闭中,请稍候。。。' })
}
Expand Down Expand Up @@ -404,15 +419,15 @@ try {
// 禁止双开
const isFirstInstance = app.requestSingleInstanceLock()
if (!isFirstInstance) {
log.info('is second instance')
log.info('app quit: is second instance')
setTimeout(() => {
app.quit()
}, 1000)
} else {
app.on('before-quit', async () => {
log.info('before-quit')
if (process.platform === 'darwin') {
quit()
quit('before quit')
}
})
app.on('will-quit', () => {
Expand All @@ -433,7 +448,7 @@ try {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
quit()
quit('window-all-closed')
}
})

Expand Down Expand Up @@ -489,7 +504,7 @@ try {
e.preventDefault()
}
log.info('系统关机,恢复代理设置')
await quit()
await quit('系统关机')
})
})
}
Expand All @@ -501,19 +516,18 @@ try {
if (process.platform === 'win32') {
process.on('message', (data) => {
if (data === 'graceful-exit') {
quit()
quit('graceful-exit')
}
})
} else {
process.on('SIGINT', () => {
quit()
quit('SIGINT')
})
}
}
// 系统关机和重启时的操作
process.on('exit', () => {
log.info('进程结束,退出app')
quit()
quit('进程结束,退出app')
})

log.info('background.js finished')
Expand Down
3 changes: 2 additions & 1 deletion packages/gui/src/bridge/backend.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import autoStart from './auto-start/backend'
import fileSelector from './file-selector/backend'
import tongji from './tongji/backend'
import update from './update/backend'
import log from '../utils/util.log'

const modules = {
api, // 核心接口模块
Expand All @@ -14,7 +15,7 @@ const modules = {
export default {
install (context) {
for (const module in modules) {
console.log('install', module)
log.info('install module:', module)
modules[module].install(context)
}
},
Expand Down
22 changes: 19 additions & 3 deletions packages/gui/src/main.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import antd from 'ant-design-vue'
import Vue from 'vue'
import VueRouter from 'vue-router'
import { ipcRenderer } from 'electron'
import view from './view'
import App from './view/App.vue'
import DsContainer from './view/components/container'
Expand All @@ -25,9 +26,14 @@ try {
})
view.initApi(app).then(async (api) => {
// 初始化status
await view.initPre(Vue, api)
app.$mount('#app')
view.initModules(app, router)
try {
await view.initPre(Vue, api)
app.$mount('#app')
view.initModules(app, router)
} catch (e) {
console.error('view初始化出现未知异常:', e)
ipcRenderer.send('view初始化出现未知异常:', e)
}
})

// fix vue-router NavigationDuplicated
Expand All @@ -41,6 +47,16 @@ try {
}

console.info('main.js finished')
ipcRenderer.send('main.js finished')
} catch (e) {
console.error('页面加载出现未知异常:', e)
ipcRenderer.send('[ERROR] 页面加载出现未知异常:', e)
}

try {
window.onerror = (message, source, lineno, colno, error) => {
ipcRenderer.send(`[ERROR] JavaScript脚本异常:Error in ${source} at line ${lineno}: ${message}`, error)
}
} catch (e) {
console.error('监听 window.onerror 出现异常:', e)
}
10 changes: 9 additions & 1 deletion packages/gui/src/preload.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
window.ipcRenderer = require('electron').ipcRenderer
try {
window.ipcRenderer = require('electron').ipcRenderer

window.onerror = (message, source, lineno, colno, error) => {
window.ipcRenderer.send(`[ERROR] JavaScript脚本异常:Error in ${source} at line ${lineno}: ${message}`, error)
}
} catch (e) {
console.error('load electron.ipcRenderer error:', e)
}
2 changes: 1 addition & 1 deletion packages/mitmproxy/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function registerProcessListener () {
log.info('代理服务进程被关闭:', code, signal)
})
process.on('beforeExit', (code, signal) => {
console.log('Process beforeExit event with code: ', code, signal)
log.info('Process beforeExit event with code: ', code, signal)
})
process.on('SIGPIPE', (code, signal) => {
log.warn('sub Process SIGPIPE', code, signal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
} else {
log.info(`请求返回: 【${proxyRes.statusCode}${url}, cost: ${cost} ms`)
}
// console.log('request:', proxyReq, proxyReq.socket)
// log.info('request:', proxyReq, proxyReq.socket)

if (cost > MAX_SLOW_TIME) {
countSlow(isDnsIntercept, `代理请求成功但太慢, cost: ${cost} ms > ${MAX_SLOW_TIME} ms`)
Expand Down Expand Up @@ -246,7 +246,7 @@ module.exports = function createRequestHandler (createIntercepts, middlewares, e
const proxyRes = await proxyRequestPromise()

// proxyRes.on('data', (chunk) => {
// // console.log('BODY: ')
// // log.info('BODY: ')
// })
proxyRes.on('error', (error) => {
countSlow(null, `error: ${error.message}`)
Expand Down
2 changes: 1 addition & 1 deletion packages/mitmproxy/src/lib/proxy/tls/tlsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const pki = forge.pki
// const user = os.userInfo()
// username = user.username
// } catch (e) {
// console.log('get userinfo error', e)
// log.info('get userinfo error', e)
// }

utils.createCA = function (CN) {
Expand Down

0 comments on commit fe060eb

Please sign in to comment.