提交 f2cf8752 编写于 作者: fxy060608's avatar fxy060608

feat(cli): add error reporting

上级 7e9664ef
...@@ -104,9 +104,23 @@ module.exports = function chainWebpack (platformOptions, vueOptions, api) { ...@@ -104,9 +104,23 @@ module.exports = function chainWebpack (platformOptions, vueOptions, api) {
if (runByHBuilderX) { // 由 HBuilderX 运行时,移除进度,错误 if (runByHBuilderX) { // 由 HBuilderX 运行时,移除进度,错误
webpackConfig.plugins.delete('progress') webpackConfig.plugins.delete('progress')
webpackConfig.plugins.delete('friendly-errors') 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') { if (process.env.BUILD_ENV === 'ali-ide') {
webpackConfig.plugins.delete('progress') webpackConfig.plugins.delete('progress')
} }
} }
} }
...@@ -2,6 +2,8 @@ const fs = require('fs') ...@@ -2,6 +2,8 @@ const fs = require('fs')
const path = require('path') const path = require('path')
const mkdirp = require('mkdirp') const mkdirp = require('mkdirp')
const loaderUtils = require('loader-utils') const loaderUtils = require('loader-utils')
require('./error-reporting')
const hasOwnProperty = Object.prototype.hasOwnProperty const hasOwnProperty = Object.prototype.hasOwnProperty
......
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)
})
const stringify = require('./stringify') const stringify = require('./stringify')
module.exports = function (errors) { 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)
}
} }
...@@ -2,16 +2,17 @@ const path = require('path') ...@@ -2,16 +2,17 @@ const path = require('path')
const formatErrors = require('./format-errors') const formatErrors = require('./format-errors')
module.exports = function stringify (errors) { module.exports = function stringify (errors, type) {
return (Array.from( let hasUnknownErr = false
const msg = Array.from(
new Set( new Set(
errors.map(err => { errors.map(err => {
const formatError = formatErrors[err.name] const formatError = formatErrors[err.name]
if (formatError) { if (formatError) {
const result = formatError(err) const result = formatError(err)
if (result) { if (result) {
if (typeof result === 'string') { if (typeof result === 'string') {
hasUnknownErr = true
return result return result
} else { } else {
if (err.module.resource) { if (err.module.resource) {
...@@ -26,11 +27,20 @@ module.exports = function stringify (errors) { ...@@ -26,11 +27,20 @@ module.exports = function stringify (errors) {
} else if (result === false) { } else if (result === false) {
return '' // skip return '' // skip
} }
} else {
hasUnknownErr = true
} }
return err.message return err.message
}) })
) )
) ).filter(msg => !!msg).join('\n')
.filter(msg => !!msg)
.join('\n')) if (type === 'error' && hasUnknownErr) {
return {
type: 'onErrors',
msg
}
}
return msg
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册