Skip to content

Commit 8607ec3

Browse files
committed
Fixed mobxjs#1480: exception in reaction effects where not handled correctly by the error handling system
1 parent 52d1541 commit 8607ec3

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

‎src/core/reaction.ts‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,17 @@ export class Reaction implements IDerivation, IReactionPublic {
8888
if (shouldCompute(this)) {
8989
this._isTrackPending = true
9090

91-
this.onInvalidate()
92-
if (this._isTrackPending && isSpyEnabled()) {
93-
// onInvalidate didn't trigger track right away..
94-
spyReport({
95-
name: this.name,
96-
type: "scheduled-reaction"
97-
})
91+
try {
92+
this.onInvalidate()
93+
if (this._isTrackPending && isSpyEnabled()) {
94+
// onInvalidate didn't trigger track right away..
95+
spyReport({
96+
name: this.name,
97+
type: "scheduled-reaction"
98+
})
99+
}
100+
} catch (e) {
101+
this.reportExceptionInDerivation(e)
98102
}
99103
}
100104
endBatch()
@@ -135,6 +139,8 @@ export class Reaction implements IDerivation, IReactionPublic {
135139
return
136140
}
137141

142+
if (globalState.disableErrorBoundaries) throw error
143+
138144
const message = `[mobx] Encountered an uncaught exception that was thrown by a reaction or observer component, in: '${this}`
139145
console.error(message, error)
140146
/** If debugging brought you here, please, read the above message :-). Tnx! */

‎test/base/errorhandling.js‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,32 @@ test("it should be possible to handle global errors in reactions", () => {
708708
})
709709
})
710710

711+
test("it should be possible to handle global errors in reactions - 2 - #1480", () => {
712+
utils.supressConsole(() => {
713+
const a = mobx.observable.box(1)
714+
const errors = []
715+
const d2 = mobx.onReactionError(e => errors.push(e))
716+
717+
const d = mobx.reaction(
718+
() => a.get(),
719+
a => {
720+
if (a >= 2) throw a
721+
}
722+
)
723+
724+
a.set(2)
725+
a.set(3)
726+
727+
d2()
728+
a.set(4)
729+
730+
expect(errors).toEqual([2, 3])
731+
d()
732+
733+
checkGlobalState()
734+
})
735+
})
736+
711737
test("global error handling will be skipped when using disableErrorBoundaries - 1", () => {
712738
mobx.configure({ disableErrorBoundaries: true })
713739
try {

0 commit comments

Comments
 (0)