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

feat(cli): add error reporting

上级 7e9664ef
......@@ -104,6 +104,20 @@ 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')
......
......@@ -3,6 +3,8 @@ const path = require('path')
const mkdirp = require('mkdirp')
const loaderUtils = require('loader-utils')
require('./error-reporting')
const hasOwnProperty = Object.prototype.hasOwnProperty
function hasOwn (obj, key) {
......
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')
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')
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
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册