错误监听
onerror事件
可以监听到运行时的相关错误,但不会监听到元素的错误,因为不会向上冒泡
js
/**
* @description 运行时错误处理器
* @param {string} message 错误信息
* @param {string} source 发生错误的脚本URL
* @param {number} lineno 发生错误的行号
* @param {number} colno 发生错误的列号
* @param {object} error Error对象
*/
function err(message: Event | string, source?: string, lineno?: number, colno?: number, error?: Error): any {
console.log('message', message)
console.log('source', source)
console.log('lineno', lineno)
console.log('colno', colno)
console.log('error', error)
}
window.onerror = erraddEventListener("error")监听
js
window.addEventListener('error', function(e) {
const eventType = [].toString.call(e)
if (eventType === '[object Event]') { // 过滤掉运行时错误
// 上报加载错误
console.log('addError', e)
}
},
true
)全局promise异常拦截
它会拦截catch,慎用
js
window.addEventListener('unhandledrejection', function (event) {
console.log('unhandledrejection', event)
event.preventDefault()
})请求接口异常上报
修改xmr或者在axios、fetch做拦截
JStar