forked from bugsnag/bugsnag-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
56 lines (47 loc) · 1.68 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
45
46
47
48
49
50
51
52
53
54
55
56
module.exports = class BugsnagVuePlugin {
constructor (Vue = window.Vue) {
if (!Vue) throw new Error('cannot find Vue')
this.Vue = Vue
this.name = 'vue'
}
load (client) {
const Vue = this.Vue
const prev = Vue.config.errorHandler
const handler = (err, vm, info) => {
const handledState = { severity: 'error', unhandled: true, severityReason: { type: 'unhandledException' } }
const event = client.Event.create(err, true, handledState, 1)
event.addMetadata('vue', {
errorInfo: info,
component: vm ? formatComponentName(vm, true) : undefined,
props: vm ? vm.$options.propsData : undefined
})
client._notify(event)
if (typeof console !== 'undefined' && typeof console.error === 'function') console.error(err)
if (typeof prev === 'function') prev.call(this, err, vm, info)
}
Vue.config.errorHandler = handler
return null
}
}
// taken and reworked from Vue.js source
const formatComponentName = (vm, includeFile) => {
if (vm.$root === vm) return '<Root>'
const options = typeof vm === 'function' && vm.cid != null
? vm.options
: vm._isVue
? vm.$options || vm.constructor.options
: vm || {}
let name = options.name || options._componentTag
const file = options.__file
if (!name && file) {
const match = file.match(/([^/\\]+)\.vue$/)
name = match && match[1]
}
return (
(name ? ('<' + (classify(name)) + '>') : '<Anonymous>') +
(file && includeFile !== false ? (' at ' + file) : '')
)
}
// taken and reworked from Vue.js source
const classify = module.exports.classify = str =>
str.replace(/(?:^|[-_])(\w)/g, c => c.toUpperCase()).replace(/[-_]/g, '')