error-reporting.js 1.1 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
function shouldReport (err = '') {
  try {
    const errMsg = err.toString()
    // 目前简单的上报逻辑为:错误信息中包含@dcloudio包名
    if (
      errMsg.includes('@dcloudio') &&
      !errMsg.includes('Errors compiling template')
    ) {
      return true
    }
  } catch (e) {}
  return false
}

// err:string|Error
function report (type, err) {
  if (shouldReport(err)) {
fxy060608's avatar
fxy060608 已提交
18
    // console.log('Error Reporting...')
fxy060608's avatar
fxy060608 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
    // const https = require('https')
    // const data = ...
    // const req = https.request({
    //   hostname: '',
    //   port: 8080,
    //   path: '/todos',
    //   method: 'POST',
    //   headers: {
    //     'Content-Type': 'application/json',
    //     'Content-Length': data.length
    //   }
    // })
    // req.write(data)
    // req.end()
  }
}

global.__error_reporting__ = report

process
  .on('unhandledRejection', (reason, p) => {
    global.__error_reporting__ && global.__error_reporting__('unhandledRejection', reason)
  })
  .on('uncaughtException', err => {
    global.__error_reporting__ && global.__error_reporting__('uncaughtException', err)
  })