Skip to content

Commit

Permalink
feat: 简化错误捕捉插件代码
Browse files Browse the repository at this point in the history
  • Loading branch information
FairyEver committed May 8, 2020
1 parent 60349f0 commit 6628f1b
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions src/plugin/error/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { get, isObject } from 'lodash'
import store from '@/store'
import util from '@/libs/util'

Expand All @@ -6,50 +7,46 @@ export default {
function writeLog (logType) {
return (error, vm, info = '') => {
Vue.nextTick(() => {
// store 追加 log
store.dispatch('d2admin/log/push', {
message: `${info}: ${(typeof error === 'object') ? error.message : error}`,
message: `${info}: ${isObject(error) ? error.message : error}`,
type: logType,
meta: {
error,
vm
}
})
// 只在开发模式下打印 log
if (process.env.NODE_ENV === 'development') {
util.log.capsule('D2Admin', 'ErrorHandler', logType)
util.log.danger('>>>>>> 错误信息 >>>>>>')
console.log(info)
util.log.danger('>>>>>> Vue 实例 >>>>>>')
console.log(vm)
util.log.danger('>>>>>> Error >>>>>>')
console.log(error)
}
if (process.env.NODE_ENV !== 'development') return
util.log.capsule('D2Admin', 'ErrorHandler', logType)
util.log.danger('>>>>>> 错误信息 >>>>>>')
console.log(info)
util.log.danger('>>>>>> Vue 实例 >>>>>>')
console.log(vm)
util.log.danger('>>>>>> Error >>>>>>')
console.log(error)
})
}
}
if (process.env.NODE_ENV === 'development') {
Vue.config.warnHandler = writeLog('warning')
}
Vue.config.errorHandler = writeLog('danger')

window.onunhandledrejection = error => {
store.dispatch('d2admin/log/push', {
message: (('message' in error.reason) && error.reason.message) || 'Unknown error',
message: get(error, 'reason.message', 'Unknown error'),
type: 'danger',
meta: {
error: error.reason,
trace: 'stack' in error.reason && error.reason.stack
error: get(error, 'reason'),
trace: get(error, 'reason.stack')
}
})
}
window.onerror = (event, source, lineno, colno, error) => {
store.dispatch('d2admin/log/push', {
message: (error && error.message) || 'Unknown error',
message: get(error, 'message', 'Unknown error'),
type: 'danger',
meta: {
error,
trace: error.stack,
trace: get(error, 'stack'),
source: `${source}@${lineno}:${colno}`,
event: event
}
Expand Down

0 comments on commit 6628f1b

Please sign in to comment.