在我的 Vue2 应用程序中,我想找到一个可以捕获全局 Vue 错误并将其发送到日志/监控实例(例如 Sentry)的地方。
当我覆盖 Vue 的全局错误处理程序时,我不再获得控制台日志。是否可以附加到全局错误处理程序而不是覆盖它?
const plugin: PluginObject<never> = {
install(Vue: typeof _Vue): void {
Vue.config.errorHandler = (err, vm, info) => {
// my Sentry stuff
}
})
}
您需要存储对原始错误处理程序的引用,然后从自定义错误处理程序中调用它。这将允许您将错误发送到日志记录/监控实例并保留默认错误。
const plugin: PluginObject<never> = {
install(Vue: typeof _Vue): void {
// Store the original error handler
const originalErrorHandler = Vue.config.errorHandler;
Vue.config.errorHandler = (err, vm, info) => {
// Your Sentry or other logging/monitoring code
// ...
// Call the original error handler, if it exists
if (originalErrorHandler) {
originalErrorHandler.call(this, err, vm, info);
} else {
// If there wasn't an original error handler, you can log the error to the console
console.error(err);
}
};
}
};