Skip to content

Commit 3d08a01

Browse files
committed
emit errors to devtool instead
1 parent 234a3bf commit 3d08a01

File tree

3 files changed

+30
-17
lines changed

3 files changed

+30
-17
lines changed

src/index.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,14 @@ class Store {
132132
if (!isPromise(res)) {
133133
res = Promise.resolve(res)
134134
}
135-
return res.catch(err => {
136-
console.error(`[vuex] error in Promise returned from action "${type}":`)
137-
console.error(err)
138-
})
135+
if (store._devtoolHook) {
136+
return res.catch(err => {
137+
store._devtoolHook.emit('vuex:error', err)
138+
throw err
139+
})
140+
} else {
141+
return res
142+
}
139143
})
140144
}
141145

src/plugins/devtool.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
const hook =
1+
const devtoolHook =
22
typeof window !== 'undefined' &&
33
window.__VUE_DEVTOOLS_GLOBAL_HOOK__
44

55
export default function devtoolPlugin (store) {
6-
if (!hook) return
6+
if (!devtoolHook) return
77

8-
hook.emit('vuex:init', store)
8+
store._devtoolHook = devtoolHook
99

10-
hook.on('vuex:travel-to-state', targetState => {
10+
devtoolHook.emit('vuex:init', store)
11+
12+
devtoolHook.on('vuex:travel-to-state', targetState => {
1113
store.replaceState(targetState)
1214
})
1315

1416
store.subscribe((mutation, state) => {
15-
hook.emit('vuex:mutation', mutation, state)
17+
devtoolHook.emit('vuex:mutation', mutation, state)
1618
})
1719
}

test/unit/test.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,29 @@ describe('Vuex', () => {
108108
})
109109
})
110110

111-
it('capturing action Promise errors', done => {
112-
const spy = sinon.spy(console, 'error')
111+
it('detecting action Promise errors', done => {
113112
const store = new Vuex.Store({
114113
actions: {
115114
[TEST] () {
116115
return new Promise((resolve, reject) => {
117-
reject(new Error())
116+
reject('no')
118117
})
119118
}
120119
}
121120
})
122-
store.dispatch(TEST).then(() => {
123-
expect(spy).to.have.been.calledWith(`[vuex] error in Promise returned from action "${TEST}":`)
124-
spy.restore()
125-
done()
126-
})
121+
const spy = sinon.spy()
122+
store._devtoolHook = {
123+
emit: spy
124+
}
125+
const thenSpy = sinon.spy()
126+
store.dispatch(TEST)
127+
.then(thenSpy)
128+
.catch(err => {
129+
expect(thenSpy).not.to.have.been.called
130+
expect(err).to.equal('no')
131+
expect(spy).to.have.been.calledWith('vuex:error', 'no')
132+
done()
133+
})
127134
})
128135

129136
it('getters', () => {

0 commit comments

Comments
 (0)