前端性能监控之请求错误上报(Fetch请求)

1,521 阅读1分钟

前端请求错误上报

监控fetch请求可以在全局对fetch进行监听,并拦截fetch请求

const global = window;
const _gFetch = global.fetch;
global.fefch = function (...args) {
  return new Promise(resolve => {
    _gFetch.apply(this, args).then((response) => {
      if (response.status === 200) {
        resolve(response.json())
      } else {
      	// 这里进行错误上报,具体错误上报方式由业务决定
        console.log("出错了!!!!哈哈!!")
      }
    })
  })
}

这里通过使用Promise,在fetch成功后仍然可以通过.then方法获取服务端返回对数据.