網站首頁 Vue 正文
主題
在 vue2.6.x 及之后版本中(僅限于此API未被毀滅性更新前)使用全局 errorHandler 鉤子來進行 vue 組件中所拋錯誤的捕捉與處理。
現狀
vue2.6 之前,errorHandler 只能捕捉同步函數拋出的錯誤,而在實際開發中我們關心得更多的是調用接口時可能拋出的錯誤,ES7之后通常使用 async await 的方式進行接口調用,對于async函數中拋出的錯誤errorHandler并不能捕捉到。因此我們使用了裝飾器(Decorator)進行錯誤捕捉,方法可行,癢點在于需要在很多個組件中引入裝飾器,然后放置在每一個需要的位置(還有個近乎絕癥的“痛點”是 vetur 插件對“不規范使用decorator”的紅色警告)。
當前使用示例(盡可能簡化版):
// errorConfigs.js
import { debounce } from 'lodash';
const DEBOUNCE_TIME = 500;
const errorProcessorConfigs = [{
assert (error) {
return ErrorAssert.isUserInfoNotExist(error); // ErrorAssert為內部Error斷言庫,使用了webpack的ProvidePlugin聲明
},
processor: debounce(() => alert('用戶信息不存在'), DEBOUNCE_TIME)
}, {
assert (error) {
return ErrorAssert.isAppInfoNotExist(error);
},
processor: debounce(() => alert('應用信息不存在'), DEBOUNCE_TIME)
}]
// errorProcessor.js
import errorProcessorConfigs from '@src/configs/errorConfigs';
function errorProcessor(error) {
let errorProcessorConfig = errorProcessorConfigs.find(config => config.assert(error));
if (errorProcessorConfig !== undefined) {
errorProcessorConfig.processor(error);
}
throw error; // 處理完之后繼續將錯誤拋出以中斷程序流程
};
export default errorProcessor;
// errorCatcher.js
import errorProcessor from './errorProcessor';
function errorCatcher(target, name, descriptor) {
const originFunc = descriptor.value;
descriptor.value = async function () {
try {
return await originFunc.apply(this, arguments);
} catch (error) {
return errorProcessor(error);
}
};
return descriptor;
};
export default errorCatcher;
// xxx.vue
/* template */
<script>
import errorCatcher from '@services/errorCatcher';
import checkLogin from '@services/checkLogin';
import resources from '@services/resources';
export default {
@errorCatcher
async created () {
await checkLogin();
this.fetchUserInfo();
},
methods: {
@errorCatcher
async fetchUserInfo () {
const userInfo = await resources.user.fetch();
// ...
},
}
};
</script>
/* style */
可行性
官網說明:
從 2.6.0 起,這個鉤子也會捕獲 v-on DOM 監聽器內部拋出的錯誤。另外,如果任何被覆蓋的鉤子或處理函數返回一個 Promise 鏈 (例如 async 函數),則來自其 Promise 鏈的錯誤也會被處理。
測試:
// main.js
/* ... */
Vue.config.errorHandler = function (err, vm, info) {
console.error('error---', err)
console.info('vm---', vm)
console.info('info---', info)
}
/* ... */
// Home.vue
/* template */
<script>
export default {
async created () {
const error = new Error('test error');
error.code = -1;
throw error;
}
}
</script>
/* style */
console:
應用
// main.js
import Vue from 'vue';
import errorProcessor from '@services/errorProcessor';
Vue.config.errorHandler = errorProcessor;
/* ... */
// xxx.vue
/* template */
<script>
import checkLogin from '@services/checkLogin';
import resources from '@services/resources';
export default {
async created () {
await checkLogin();
this.fetchUserInfo();
},
methods: {
async fetchUserInfo () {
const userInfo = await resources.user.fetch();
// ...
},
}
};
</script>
/* style */
對比
裝飾器 | errorHandler | |
---|---|---|
優勢 | 1.使用靈活,可多個裝飾器組裝使用;2.裝飾器可以接收參數,進行更高階的應用; | 1.全局設置,一處設置處處受益,使用較為簡潔;2. 可以直接拿到 this,獲取更多信息,進行更多操作;3. info 可輔助定位錯誤源; |
劣勢 | 1.在每一個需要的函數處都需要使用裝飾器進行包裝,組件中也需要引入裝飾器模塊;2.裝飾器被babel編譯后細微的增加文件體積;3.編譯期生效,因此如果 errorProcessorConfigs想引入 router 就需要進行一些特殊處理(如將routes 配置放在 main.js 中添加,或者通過變量方式傳入) | 1.無法向錯誤處理中傳入參數;2.全局只能有一個,如果想針對不同場景下拋出的同一類 error 進行不同處理,則需要加入判斷邏輯,影響函數純粹性。3.捕捉錯誤條件限制于同步函數或函數返回promise鏈。4.watch中調用的異步函數暫時無法被捕捉 |
總結
裝飾器錯誤處理方式略顯繁重,但并不會被errorHandler完全取締,如果想讓errorHandler盡可能的捕捉到出現的錯誤,則可能需要對代碼進行一些更為嚴格的調整(構建promise鏈),可以根據實際的場景,將二者結合使用,以產生更大的受益(舉個例子:某些場景下的錯誤可以使用裝飾器處理并吞掉,不走全局處理)。
期待提建議和補充·~·
原文鏈接:https://blog.csdn.net/qq_39300332/article/details/87926877
相關推薦
- 2023-10-14 WIN32 預定義宏WIN32,_WIN32,_WIN64介紹使用
- 2022-01-09 el-tree同級節點可選擇 其他節點及父節點禁用
- 2023-06-18 Python實現兩種稀疏矩陣的最小二乘法_python
- 2022-09-03 python通過ElementTree操作XML_python
- 2022-06-26 Go語言開源庫實現Onvif協議客戶端設備搜索_Golang
- 2022-09-27 WIN10使用IIS部署ftp服務器詳細教程_FTP服務器
- 2023-11-11 flask python 設置定時任務 flask 周期性執行任務方案
- 2022-05-31 C語言中回調函數的含義與使用場景詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支