feat: 添加全局错误处理机制,捕获 Vue 和 JS 错误 (#2112)

This commit is contained in:
PiexlMax(奇淼
2025-10-09 13:58:35 +08:00
committed by GitHub
2 changed files with 56 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
function sendErrorTip(errorInfo) {
console.groupCollapsed(`捕获到错误: ${errorInfo.type}`);
console.log('错误类型:', errorInfo.type);
console.log('错误信息:', errorInfo.message);
console.log('调用栈:', errorInfo.stack);
if (errorInfo.component) {
console.log('组件名:', errorInfo.component.name)
console.log('组件地址:', errorInfo.component.__file)
}
if (errorInfo.vueInfo) console.log('Vue 信息:', errorInfo.vueInfo);
if (errorInfo.source) console.log('来源文件:', errorInfo.source);
if (errorInfo.lineno) console.log('行号:', errorInfo.lineno);
if (errorInfo.colno) console.log('列号:', errorInfo.colno);
console.groupEnd();
}
function initVueErrorHandler(app) {
app.config.errorHandler = (err, vm, info) => {
let errorType = 'Vue Error';
sendErrorTip({
type: errorType,
message: err.message,
stack: err.stack,
component: vm.$options || 'Unknown Vue Component',
vueInfo: info
});
};
}
function initJsErrorHandler() {
window.onerror = (message, source, lineno, colno, error) => {
let errorType = 'JS Error';
sendErrorTip({
type: errorType,
message: message,
stack: error ? error.stack : 'No stack available',
source: source,
lineno: lineno,
colno: colno
});
return false;
};
}
export function initErrorHandler(app) {
initVueErrorHandler(app)
initJsErrorHandler()
}

View File

@@ -15,8 +15,13 @@ import auth from '@/directive/auth'
import clickOutSide from '@/directive/clickOutSide'
import { store } from '@/pinia'
import App from './App.vue'
import { initErrorHandler } from '@/core/error-handel'
const app = createApp(App)
// 注入错误处理捕获
initErrorHandler(app)
app.config.productionTip = false
app