sleepyShen1989 / blog

MIT License
0 stars 0 forks source link

【实战】代码片段 #24

Open sleepyShen1989 opened 1 year ago

sleepyShen1989 commented 1 year ago

代码片段

常见错误捕获

// Vue错误处理
app.config.errorHandler = (err) => { 
    navigator.sendBeacon(url, {error: error.message, text: 'vue运行异常' })
}

// 错误事件监听
window.addEventListener('error', (error) => { 
    if (error.message) { 
        navigator.sendBeacon(url, {error: error.message, text: 'js执行异常' })
    } else { 
        navigator.sendBeacon(url, {error: error.filename, text: '资源加载异常' })
    } 
}, true)

// 请求错误监听
axios.interceptors.response.use(
    (response) => {
      if (response.code == 200) {
        return Promise.resolve(response);
      } else {
        return Promise.reject(response);
      }
    },
    (error) => {
      navigator.sendBeacon(url, {error: error, text: '请求错误异常' })
    }
  );