Skip to content

Commit acd7249

Browse files
committed
feat: ensure errors in action subscribers do not break actions
1 parent 59f31af commit acd7249

File tree

1 file changed

+23
-8
lines changed

1 file changed

+23
-8
lines changed

src/store.js

+23-8
Original file line numberDiff line numberDiff line change
@@ -129,19 +129,34 @@ export class Store {
129129
return
130130
}
131131

132-
this._actionSubscribers
133-
.filter(sub => sub.before)
134-
.forEach(sub => sub.before(action, this.state))
132+
try {
133+
this._actionSubscribers
134+
.filter(sub => sub.before)
135+
.forEach(sub => sub.before(action, this.state))
136+
} catch (e) {
137+
if (process.env.NODE_ENV !== 'production') {
138+
console.warn(`[vuex] error in before action subscribers: `)
139+
console.error(e)
140+
}
141+
}
135142

136143
const result = entry.length > 1
137144
? Promise.all(entry.map(handler => handler(payload)))
138145
: entry[0](payload)
139146

140-
result.then(() => this._actionSubscribers
141-
.filter(sub => sub.after)
142-
.forEach(sub => sub.after(action, this.state)))
143-
144-
return result
147+
return result.then(res => {
148+
try {
149+
this._actionSubscribers
150+
.filter(sub => sub.after)
151+
.forEach(sub => sub.after(action, this.state))
152+
} catch (e) {
153+
if (process.env.NODE_ENV !== 'production') {
154+
console.warn(`[vuex] error in after action subscribers: `)
155+
console.error(e)
156+
}
157+
}
158+
return res
159+
})
145160
}
146161

147162
subscribe (fn) {

0 commit comments

Comments
 (0)