Browse Source

前端代码报错监控上报

jwyu 2 years ago
parent
commit
fc2a2ade5e
3 changed files with 44 additions and 1 deletions
  1. 13 0
      src/api/hzyb/common.js
  2. 5 1
      src/main.js
  3. 26 0
      src/views/hzyb/utils/reportErr.js

+ 13 - 0
src/api/hzyb/common.js

@@ -21,4 +21,17 @@ export const getWxConfig=(params)=>{
  */
 export const apiGetPoster=params=>{
     return post('/public/get_share_poster',{version:'3.0',...params})
+}
+
+/**
+ * 上传错误日志
+ */
+export const apiReportingErrInfo=params=>{
+    let obj={
+        ...params,
+        timestamp:new Date().toLocaleString(),
+        agent:'小程序H5',
+        page:window.location.href
+    }
+    return post('/public/wechat_warning',{content:JSON.stringify(obj)})
 }

+ 5 - 1
src/main.js

@@ -6,5 +6,9 @@ import 'normalize.css'
 import './style/common.scss'
 import '@vant/touch-emulator';//vant 
 import 'vant/lib/index.css';
+import reportErr from '@/views/hzyb/utils/reportErr'
 
-createApp(App).use(router).use(store).mount('#app')
+const app=createApp(App)
+reportErr(app)//设置全局错误上报
+
+app.use(router).use(store).mount('#app')

+ 26 - 0
src/views/hzyb/utils/reportErr.js

@@ -0,0 +1,26 @@
+// 上报错误(及时上报)
+import {apiReportingErrInfo} from '@/api/hzyb/common'
+
+const reportErr=(app)=>{
+    let errObj={}
+
+    app.config.errorHandler=(err, instance, info)=>{
+        errObj={
+            msg:err.message,
+            stack:err.stack
+        }
+        apiReportingErrInfo({errInfo:errObj})
+    }
+
+    window.addEventListener('error',(e)=>{
+        errObj={
+            msg:e.error.message,
+            stack:e.error.stack
+        }
+        apiReportingErrInfo({errInfo:errObj})
+    })
+}
+
+
+
+export default reportErr