From f2cf8752ebd7559e4d8b5424cc85bfeac4261eb3 Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Sat, 9 May 2020 14:00:41 +0800 Subject: [PATCH] feat(cli): add error reporting --- .../vue-cli-plugin-uni/lib/chain-webpack.js | 16 ++++++- packages/vue-cli-plugin-uni/lib/env.js | 2 + .../vue-cli-plugin-uni/lib/error-reporting.js | 44 +++++++++++++++++++ packages/vue-cli-plugin-uni/util/on-errors.js | 8 +++- packages/vue-cli-plugin-uni/util/stringify.js | 22 +++++++--- 5 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 packages/vue-cli-plugin-uni/lib/error-reporting.js diff --git a/packages/vue-cli-plugin-uni/lib/chain-webpack.js b/packages/vue-cli-plugin-uni/lib/chain-webpack.js index 1ff13f241..a0b5dcdc1 100644 --- a/packages/vue-cli-plugin-uni/lib/chain-webpack.js +++ b/packages/vue-cli-plugin-uni/lib/chain-webpack.js @@ -104,9 +104,23 @@ module.exports = function chainWebpack (platformOptions, vueOptions, api) { if (runByHBuilderX) { // 由 HBuilderX 运行时,移除进度,错误 webpackConfig.plugins.delete('progress') webpackConfig.plugins.delete('friendly-errors') + } else { + webpackConfig.plugin('friendly-errors') + .tap(args => { + if (global.__error_reporting__) { + args[0].onErrors = function (severity, errors) { + if (severity !== 'error') { + return + } + const error = errors[0] + global.__error_reporting__ && global.__error_reporting__(error.name, error.message || '') + } + } + return args + }) } if (process.env.BUILD_ENV === 'ali-ide') { webpackConfig.plugins.delete('progress') } } -} +} diff --git a/packages/vue-cli-plugin-uni/lib/env.js b/packages/vue-cli-plugin-uni/lib/env.js index 3d49f26cb..5cbfa1f17 100644 --- a/packages/vue-cli-plugin-uni/lib/env.js +++ b/packages/vue-cli-plugin-uni/lib/env.js @@ -2,6 +2,8 @@ const fs = require('fs') const path = require('path') const mkdirp = require('mkdirp') const loaderUtils = require('loader-utils') + +require('./error-reporting') const hasOwnProperty = Object.prototype.hasOwnProperty diff --git a/packages/vue-cli-plugin-uni/lib/error-reporting.js b/packages/vue-cli-plugin-uni/lib/error-reporting.js new file mode 100644 index 000000000..fb1c3e068 --- /dev/null +++ b/packages/vue-cli-plugin-uni/lib/error-reporting.js @@ -0,0 +1,44 @@ +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)) { + console.log('Error Reporting...') + // 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) + }) diff --git a/packages/vue-cli-plugin-uni/util/on-errors.js b/packages/vue-cli-plugin-uni/util/on-errors.js index c47019d8e..234479848 100644 --- a/packages/vue-cli-plugin-uni/util/on-errors.js +++ b/packages/vue-cli-plugin-uni/util/on-errors.js @@ -1,4 +1,10 @@ const stringify = require('./stringify') module.exports = function (errors) { - console.error(stringify(errors)) + const errMsg = stringify(errors, 'error') + if (typeof errMsg !== 'string') { + global.__error_reporting__ && global.__error_reporting__(errMsg.type, errMsg.msg) + console.error(errMsg.msg) + } else { + console.error(errMsg) + } } diff --git a/packages/vue-cli-plugin-uni/util/stringify.js b/packages/vue-cli-plugin-uni/util/stringify.js index 7f5598ddc..4ef5d544b 100644 --- a/packages/vue-cli-plugin-uni/util/stringify.js +++ b/packages/vue-cli-plugin-uni/util/stringify.js @@ -2,16 +2,17 @@ const path = require('path') const formatErrors = require('./format-errors') -module.exports = function stringify (errors) { - return (Array.from( +module.exports = function stringify (errors, type) { + let hasUnknownErr = false + const msg = Array.from( new Set( errors.map(err => { const formatError = formatErrors[err.name] - if (formatError) { const result = formatError(err) if (result) { if (typeof result === 'string') { + hasUnknownErr = true return result } else { if (err.module.resource) { @@ -26,11 +27,20 @@ module.exports = function stringify (errors) { } else if (result === false) { return '' // skip } + } else { + hasUnknownErr = true } return err.message }) ) - ) - .filter(msg => !!msg) - .join('\n')) + ).filter(msg => !!msg).join('\n') + + if (type === 'error' && hasUnknownErr) { + return { + type: 'onErrors', + msg + } + } + + return msg } -- GitLab